How to Drive NeoPixels using Circuit Playground Express
2018-11-21 | By Maker.io Staff
The Circuit Playground Express is a small yet powerful dev board developed by Adafruit that contains multiple LEDs, sensors, buttons, and GPIO. In this how-to article, we will learn how to use the Circuit Playground Express with NeoPixel rings using Microsoft’s MakeCode editor!
MakeCode
MakeCode is a programming platform designed by Microsoft that can be used to program a number of educational development boards, including the Circuit Playground Express and the micro:bit. One of the major advantages of MakeCode is that it has a Blocks programming method. This allows us to create programs using puzzle pieces instead of complex lines of code. However, if you wish to program using code, then you still have the option to switch to Javascript! The first step in using MakeCode is to visit the MakeCode website. MakeCode is an online environment (i.e. no need for any downloads and installations).
If all goes to plan and the website is working correctly, then you should see a layout near identical to the MakeCode environment for the micro:bit (see previous articles).
Create a NeoPixel Object
The first step is to create a NeoPixel object that will be used to interface with the NeoPixel ring. Start by going to Loops and drop the “on start” block into the development area. Then navigate to Light > NeoPixel and then drag the “set strip to create strip” block into the on start block.
However, this “set strip” block does not contain enough information for us to use our NeoPixel ring, so we need to expand it. First, click the plus icon next to “create strip” which will expand to include the pin we want to use on the board as the output signal for the ring. Set the output pin to be A1. Then click the plus symbol again to expand the options further to include the number of pixels on the display. The NeoPixel ring has 24 LEDs so we will enter 24.
The next block that needs adding is the mode set that can be found in the NeoPixels block menu. This can be found by searching for the block in the search bar at the top. The “strip set mode RGB (GRB Format)” needs to be selected so that the Circuit Playground Express can correctly communicate with the NeoPixel ring. The last block that we will include in the “on start” piece is “strip set brightness to 10”. This is done because NeoPixels are very bright when used on high brightness settings!
Displaying a Rainbow Animation
Now that our NeoPixel object has been configured, it’s time to display some LEDs! The first test that we will run is a simple animation that makes a multicolored rainbow that slowly rotates. The first block that we will need to include in our program is a “forever” block that continuously loops and executes any code inside forever. The forever loop is found under Loops in the menu on the left of the window.
To show the multicolored rainbow animation, place the “strip show animation” block into the forever loop. It’s located under Light > NeoPixel.
Connecting the NeoPixel Ring
The NeoPixel ring requires only three connections to the Circuit Playground Express for it to work correctly.
The VIN pin connects to the Circuit Playground Express 3.3 V pin, the data pin connects to A1, and the GND pin connects to GND. For this project I used solid core wires that were stripped on one side and connected to an alligator clip on the other side.
Downloading the program to the Circuit Playground Express
Before we use the Circuit Playground Express, we need to download our code from MakeCode. To do this, click the “Download” button and then open the folder which the file is downloaded to. In my case, this folder is the generic “Downloads” folder in Windows 7.
Getting programs made using MakeCode onto the Circuit Playground Express is very easy. The first step requires you to plug the board into your PC via a USB micro b cable and then press the reset button. If the lights change to red, wait until all the lights turn green. If nothing happens, keep pressing the reset button until all the lights turn red and wait until they turn green. When the lights turn green, the board will be in program mode. You should see a flash drive prompt on your computer.
Open the flash drive to reveal the folder and then drag the downloaded “.uf2” file into the folder. The Circuit Playground Express will then automatically program itself and reset. The program should now run and the NeoPixel ring should display a slowly changing rainbow pattern.
Display specific pixels
Animations are fun, but setting specific pixels is more practical for projects. This last example will make the NeoPixel ring display a single LED that rotates around the display by taking advantage of a “for loop” and some simple NeoPixel blocks.
In our main forever loop, we start by placing down a “for loop” that will count between the numbers 0 to 23 (24 counts in total). Inside the “for loop”, the first block executed is “strip clear”. This instruction clears the entire NeoPixel display so that no LEDs are displaying anything (think of this as a clear display). The next instruction sets the pixel at index to be green. Remember that the value of index increments on each loop!
The second to last instruction is “strip show” and this MUST be called after all pixel setting functions have been done. This instruction sends the new commands to all the NeoPixels so they can update themselves with the new image. The last instruction is a pause, helping slow down the animation on the LED display so that the LED does not rotate around the ring too fast.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum