Hi, I'm Becky Stern.
And today we're going to talk about using code libraries with Arduino
and some of the fun devices they can control.
One of the Arduino community's biggest superpowers
is code libraries: bundles of specialized programing
that empower you to leverage complex computing tools using simple commands.
Arduino recognizes these add on modules,
and then you can use the commands within your program.
Arduino libraries exists for all sorts of complex tasks,
like controlling large numbers of LEDs, reading sensors, creating sounds,
manipulating data, writing to displays and many more.
There are a bunch of libraries that come with the Arduino software,
but anyone can publish a library and there's thousands more you can install.
We'll start with my favorite, smart adjustable LEDs,
each of which contain a WS2812 chip.
These are commonly called NeoPixels, which is the Adafruit brand name.
Each pixel’s chip communicates with the Arduino board
and other pixels in the strip.
It can't light up without a controller
to control the strip.
Some additional Arduino functions are required
and we can get them by installing the code library.
To install libraries, I’ve opened up the library manager
Under the Sketch menu > Include Library > Manage Libraries,
then searching for NeoPixel and clicking install.
Now I'm all set to use the NeoPixel library in my Arduino sketches.
You can use the library manager to install the new libraries as you need them.
Take a close look at these pixels.
Each one 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 the light produced.
For this reason there are also RGBW varieties
that include a white led inside their package 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 an earlier episode.
You provide a number from 0 to 255 to represent the brightness of each led.
The functions in the NeoPixel library
take pixel numbers and these color values as arguments
then translate them into the commands to send along the pixels.
I've soldered wires to the power, ground, and input pins on my pixels
and with my Arduino board unplugged, it's time to hook up the circuit.
The red wire goes to five volts
and the black wire goes to ground
and the input goes to a pin on my microcontroller.
The example uses pin six.
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.
The code examples can be found in the examples menu
at the bottom in a submenu with the library’s name.
I'm opening up the example called streandtest.
This first section instantiates the NeoPixel strip
and sets up the configurable bits of the program
Like the pins connected to the NeoPixel strip,
the number of pixels and the global brightness level.
#define statements are similar to variable declarations,
but are used for information that does not change
while the program is running, like the PIN number or the number of LEDs.
They take up less memory than regular variable declarations.
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.
The main loop just calls some external functions,
for instance colorWipe(); which is defined down below the main loop.
It takes two arguments: a color and a speed value in milliseconds.
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,
it just controls the pixels.
So ‘void’ is used at the beginning of the definition.
Next is the name of the function, which is totally up to you.
Then in parentheses are the arguments the function takes.
In this case, a 32 bit integer called “color”
and an unsigned integer called “wait.”
Inside the function, these local variables are used to reference information
You passed to it from the main loop or from another function.
The function itself steps through all of the pixels in the strip
using a for loop and a NeoPixel library function called strip.NumPixels();
coloring and showing each one before moving on to the next.
The other functions in the strandtest program work the same way
and use clever color math to achieve some stunning light patterns.
You can easily modify colorWipe(); to start coding up your own unique animation.
I'll just copy the whole function definition,
change the name of the function, and then I'll start playing around with the code,
being 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 create a new one.
Functions are a handy tool to separate out a chunk of code
you want to access repeatedly and make your code easier to read.
Once you're comfortable installing libraries and looking up
how to use their built-in functions through the included examples
and the libraries 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 just opened up to you,
check out my guide on translating your Arduino project idea into code.
Basically, you get each individual input and output
working using library example code.
Then build up a new program from bits of other samples.
Another thing I want to show you 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.
So if you want to install the ESP board support for instance,
you need to first paste the URL for that ESP boards
into this field in the Arduino preferences.
Then go to the boards manager and you'll be able to see them in the list.
You can add multiple boards manager URLs
by separating them with a comma in this field.
Sometimes compatible boards will also require the installation
of a driver as well.
So be sure to read the documentation carefully
whenever you are getting ready to work with a new board.
I've put some links to resources in the description.
Leave your advice
about Arduino libraries in the comments so we can all learn together.
Check out the playlist with the rest of the series and subscribe
to be sure you don't miss the next one.