Maker.io main logo

Arduino Code Libraries with Becky Stern

2023-02-14 | By bekathwia

Arduino

One of the Arduino community’s biggest superpowers is code libraries– bundles of specialized ‎programming that empower you to leverage complex computing tools using simple commands. ‎Arduino recognizes these add-on modules so you can use their commands within your program.

Arduino Code Libraries with Becky Stern

Arduino libraries exist for all sorts of complex tasks. You can use them to control large numbers of ‎LEDs, read sensors, create sounds, manipulate data, write to displays, and much more. Some ‎come built-in with the Arduino software, like the library that allows you to control servo motors. But ‎anyone can publish a library, and there are thousands more you can install.

The board I recommend starting with is the Arduino Uno (1050-1024-ND) or compatibles such as ‎the Adafruit Metro (1528-1214-ND), Sparkfun Redboard (1568-1977-ND), or Seeeduino (102010026-ND).

To build the circuits in this guide, you will need:

Arduino Code Libraries with Becky Stern

Let’s start with smart addressable LEDs, a series of controllable LED lights commonly referred to ‎as NeoPixels, which is the Adafruit brand name. Each pixel contains a WS2812 chip which ‎communicates with the Arduino board and other pixels on its strip to determine whether to light up ‎and what color–it can't light up without a controller. To control the strip, some additional Arduino ‎functions are required, which you can get by installing the code library.

Arduino Code Libraries with Becky Stern

To install libraries, open the Library Manager under Sketch> Include Library>Manage Libraries.

Arduino Code Libraries with Becky Stern

Then search for “NeoPixel” and click Install when you locate the “Adafruit NeoPixel” library. Now ‎you’re all set to use the NeoPixel library in your Arduino sketches! You can use the Library ‎Manager to install new libraries as you need them.

Arduino Code Libraries with Becky Stern

Each pixel in a NeoPixel strip contains a very small RGB LED, which can theoretically create any ‎color of light. However, generating pure white poses a challenge for RGB LEDs and often leaves ‎users disappointed in the tinted or poorly mixed quality of light produced.

Arduino Code Libraries with Becky Stern

For this reason, there are also RGBW varieties that include a white LED inside their packages as ‎well. Coding for these pixels will include four brightness values to describe a pixel color: red, green, ‎blue, and white.

This is similar to analogWrite(); in the example from earlier in this series: You provide a number ‎from 0-255 to represent the brightness of the LED. The functions in the NeoPixel library take pixel ‎numbers and these color values as arguments and then translate them into the commands to send ‎along the pixels.

Arduino Code Libraries with Becky Stern

To assemble your circuit, solder wires to the power, ground, and input pins on the pixels and hook ‎up the circuit with the Arduino unplugged.

Arduino Code Libraries with Becky Stern

Connect the red 5V wire to the breadboard's power bus and the black ground wire to the ‎breadboard's ground bus. The white data wire connects to Arduino pin 6 since that’s the pin the ‎included code examples use.

Arduino Code Libraries with Becky Stern

Be sure you are connecting the data line to the input end of the strip, as it won’t work if you connect ‎to the output. Power and ground can be connected anywhere along the strip.

Arduino Code Libraries with Becky Stern

The code examples can be found by going to File>Examples and scrolling to the bottom, where ‎you’ll find a submenu with the library’s name. Open the example called “strandtest.”

Arduino Code Libraries with Becky Stern

The first section instantiates the NeoPixel strip and sets up the configurable bits of the program, like ‎the pins connected to the NeoPixel strip and button, the number of pixels, and the global brightness ‎level.

Arduino Code Libraries with Becky Stern

#define statements are similar to variable declarations but are used for information that does not ‎change while the program is running. They take up less memory than regular variable declarations.

Arduino Code Libraries with Becky Stern

strip.show(); is used any time you want the pixels to change. Setting the pixel colors and showing ‎the latest changes are broken up into two separate commands.

Arduino Code Libraries with Becky Stern

The main loop just calls other functions. The first is colorWipe(), which takes two arguments: a ‎color, and a speed value in milliseconds.

Arduino Code Libraries with Becky Stern

To understand what colorWipe() is doing, find where the function is defined, below the main loop. ‎The function definition starts with what type of data the function will return or send back to the main ‎program. In this case, the function returns nothing, so the word void is used at the start of the ‎definition. Next is the name of the function, which is “colorWipe” in this case. When you’re defining ‎your own functions, you can name them whatever you want!

In the parentheses after the function’s name, you’ll find the arguments the function takes. In this ‎case, there is a 32-bit unsigned integer called "color" and an 8-bit unsigned integer called "wait." ‎Inside the function, these local variables are used to reference the information you passed to it ‎from the main loop (or from another function).

Arduino Code Libraries with Becky Stern

The function itself steps through all the pixels in the strip using a for loop and a NeoPixel library ‎function called strip.numPixels();, coloring and showing each one before moving to the next. The ‎other functions in the strandtest program work the same way and use clever color math to achieve ‎stunning light patterns.

Arduino Code Libraries with Becky Stern

You can easily modify colorWipe() to start coding up your own unique animation. Copy and paste ‎the whole function definition, change the name of the function, and then you can play around with ‎the code. Be sure to call the new function from inside the main loop. If you find an effect you like, ‎you can stop editing that function and move on to a new one. Functions are handy for separating ‎out a chunk of code you want to access repeatedly, and they make your code easier to read.

Arduino Code Libraries with Becky Stern

Once you’re comfortable installing libraries and looking up how to use their built-in functions, ‎through the included examples and the library’s documentation, the Arduino world really is your ‎oyster. Moving beyond the basic inputs and outputs we covered in previous episodes, libraries let ‎you easily talk to devices that communicate with data protocols like i2c or SPI. These devices use ‎fewer wires to send and receive complex information.

If you’re feeling overwhelmed by all the new possibilities that opened up to you, check out my ‎guide on translating your Arduino project idea into code. Basically, you can start by getting each ‎individual input and output to work using library example code, and then build up a new program ‎from bits of other samples.

Arduino Code Libraries with Becky Stern

Another thing that is useful to know at this point in your Arduino journey is how to install additional ‎boards. Just like the Library Manager under the Sketch menu, there’s a Boards Manager under the ‎Tools menu. But unlike libraries, the Boards Manager needs to know where to look for the new ‎board info.

Arduino Code Libraries with Becky Stern

So if you’re installing ESP32 support, for instance, you’d first need to paste the URL for the ESP32 ‎boards info into this field in your Arduino settings. Then the ESP32 Arduino boards will become ‎visible in the boards manager, and you can install them from there. Then restart your Arduino IDE. ‎You can add multiple Boards Manager URLs by separating them with a comma. Sometimes ‎compatible boards will also require the installation of a driver as well, so be sure to read the ‎documentation carefully whenever you’re getting ready to work with a new board.

Check out the rest of this series for even more information on building with Arduino.

制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
¥224.67
Details
制造商零件编号 2488
METRO ATMEGA328 W/HDR EVAL BRD
Adafruit Industries LLC
¥142.45
Details
制造商零件编号 DEV-15123
REDBOARD QWIIC ATMEGA328 EVAL BD
SparkFun Electronics
¥175.01
Details
制造商零件编号 102010026
SEEEDUINO V4.3 ATMEGA328P DEV BD
Seeed Technology Co., Ltd
¥74.24
Details
制造商零件编号 FIT0096
BREADBRD TERM STRIP 3.20X2.00"
DFRobot
¥24.43
Details
制造商零件编号 1957
JUMPER WIRE M TO M 6" 28AWG
Adafruit Industries LLC
¥15.87
Details
制造商零件编号 1311
HOOK-UP 22AWG SOLID - 6 X 25FT
Adafruit Industries LLC
¥133.82
Details
制造商零件编号 2541
ADDRESS LED STRIP SERIAL RGB 1M
Adafruit Industries LLC
¥125.97
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum