Outils pour utilisateurs

Outils du site


issue171:micro-ci_micro-la

Ceci est une ancienne révision du document !


This month, we will be talking about using the WS2812 Integrated Light Source on the Raspberry Pi Pico, and on the ESP32 and ESP8266 microcontrollers. The sharp-eyed among you who are good and loyal readers, might be thinking that, last month, I said we would be talking about NeoPixels. You are correct. I did. And we are. NeoPixels is a brand name that belongs to Adafruit. It stands for the individually addressable RGB color LEDs; strips all based on the WS2812 and WS2811 LED with drivers that use a single wire protocol. So, TECHNICALLY, not all WS2812 LEDs are NeoPixel displays. The ESP32 and ESP8266 microcontrollers both have a driver that is included in the distribution of MicroPython. For the Raspberry Pi Pico, the communications to the LEDs needs to be done via PIO programming - Raspberry Pi’s assembly language. Luckily, there is a library available for the Pico to handle the communications. You can find it at: https://github.com/benevpi/pico_python_ws2812b. This library works very well, but has been forked and updated. However, the new libraries don’t work correctly from the Thonny IDE, and it’s a real pain to write your code, and then try to run your file outside of the IDE. We’ll use the ws2812b library for now.

Setting up your breadboard NeoPixel type devices take a good bit of power. If you are using only a single LED, then it’s not too big of a deal, but if you have 8, 24, 30 or 60 LEDs, you will need an external power source of about 5 volts DC. I say about 5 volts, since 5 volts is the absolute maximum voltage you should provide to the NeoPixel type devices. The current requirement of multiple LEDs is surprising, and if you attempt to use the USB power to drive the LEDs, it will quickly become too high for the PC to provide. I use a 3 x AA rechargeable battery pack for my setup, with NIMH (Nickel Metal Hydride) batteries. This provides about 4.6 volts (1.2 volts each) of power when the batteries are fully charged. This is low enough to provide enough, but not too much, power.

While the connections for the RPi Pico and the ESP32/8266 are very similar, I will include one of each. In the illustrations, I will use a WS2812 8-LED stick. This stick has a fairly common set of connections that will easily work for the stick and most any flat strip, no matter the length and number of LEDs. All devices will have at least 3 input pins. Ground, +5 volt DC, and a pin for data in. No matter if you are using an 8-LED stick or a 1 metre 60-LED weatherproof strip, the connections are basically the same. As you can see in the image below, there are four inputs on each side of the stick. On the left, there are 2 for ground, one for data in and one for the +5 volt power. The right side has the same pins with the exception of Data Out, which is used (if needed) to add another stick. One other thing to note is that the breadboard images show a 3 x AAA battery pack. It should actually be a 3 x AA battery pack. I couldn’t find the actual image in Fritzing.

Raspberry Pi Pico For the Pico, we put the 3 x AA battery pack to the + and minus rails of the bread board. The data-in pin of the LED stick is connected to physical pin 21, which is GPIO 16. The Ground and +5 volt pins of the stick are also connected to the power rails of the breadboard. Finally, be sure to connect the ground rail to a ground pin of the Pico. It is important that all devices share a common ground reference. ESP32/8266 The ESP series breadboard connections are very similar to the Pico. The main difference is where the ground and data pins are. You can use any ground pin for the ground, and any GPIO pin for the data pin. In the case of this image, I decided to use pin # 27 (GPIO 16) as the data pin and pin # 38 as the ground pin. You can use whatever pins you wish.

The Code Raspberry Pi Pico First, download the repository from the github site above. Save it to a convenient folder and unzip it. Then copy the file ws2812b.py to the pico as well as the example files. We’ll look at the flash.py example first. import time from ws2812b import ws2812b Now we’ll take a look at the first two lines of code. The first sets the number of LEDs that you have on your device. Be sure that it matches the number of LEDs that you really have. Many stick type displays have only 8 LEDs. Some strips have 30 to 60 on them. The ring that I have has 24 LEDs.

The second line instantiates the ws2812b class into the variable pixels. Notice that the first parameter is the number of LEDs, the second is the state machine that will be used. This is normally either a 0 or a 1. The next parameter is the GPIO pin number. The last is the delay that you want to use before resetting the LED set. It should be safe to use 0, unless you need to do a lot of processing. num_leds = 24 pixels = ws2812b(num_leds, 0, 16, delay=0) Next comes the brightness value. This can be useful if you are like me, and the LEDs blind you because they are so bright. The next two lines fill every LED on your device to 10,10,10 (R,G,B values) and then the information is sent out to the LED device with the pixels.show() command. pixels.brightness(30) pixels.fill(10,10,10) pixels.show()

At this point, we set up a simple loop (above) and for each LED that you have set up at the beginning of the code, which sets the color. Then it sends the data out and pauses for a little bit. Now, the code might look fairly easy until you get to the set_pixel line. Here (below) is the actual function from the library. It makes it much easier to understand what’s happening. You can see that the set_pixel function takes 4 parameters. The pixel number, and the red, green and blue values. So going back to the loop above, we can step through the code and see the values that are being sent to the LED device. I’ll show only 7 values, which should give you a good idea of what happens. 0 0 3 6 1 1 4 7 2 2 5 8 3 3 6 9 4 4 7 0 5 5 8 1 6 6 9 2 … When we run the program, all the LEDs will blink different colors in a quickly changing pattern.

The other example program I want to point out is the fireflies.py program. It is supposed to simulate the flitting of fireflies on a summer’s evening. I have to admit that it is more impressive on a 1 metre 30+ LED strip than on the 8-LED strip. import time import ws2812b import random numpix = 24 # Number of NeoPixels # Pin 16 is where NeoPixels are connected strip = ws2812b.ws2812b(numpix, 0, 16) Remember to set the number of LEDs that your device has as well as the GPIO pin that the device is connected to.

colors = [ [232, 100, 255], # Purple [200, 200, 20], # Yellow [30, 200, 200], # Blue [150,50,10], [50,200,10], ] Here (top right) max_len and min_len is the maximum and minimum length of time the LEDs are on. The num_flashes refers to the number of “fireflies” that are active at one time. This is the display function that handles the glowing and flitting of the virtual flies (which are actually beetles in real life). Shown bottom right. Most of the demo programs that I have found have similar loops as the above two programs. And they have a similar while loop that, when stopped, the LEDs are still active. So I wrote the following short program to clear the LEDs when I’m done with the demo. I call it led_clear.py

# LED Clear from ws2812b import ws2812b num_leds = 24 pixels = ws2812b(num_leds, 0, 16, delay=0) pixels.brightness(100) pixels.fill(0,0,0) pixels.show() ESP32/ESP8266 My initial thought was to rewrite the flash program presented for the Pico to work on the ESP microcontrollers. However, I remembered that I had a great demo from randomnerdtutorials.com that would provide a better example of how to use the library. So, instead of flash, I present neopixel1.py (top right). The same code should run unmodified on the ESP8266, since they share the same library.

Notice that the library for the neopixel devices is named “neopixel”. The variable n is the number of LEDs the device has, and the variable p is the GPIO pin that will be used. So the variable np is the neopixel object. You need to address each pixel individually and pass the RGB values as a tuple. You can see this in the clear function presented directly below. def clear(): for i in range(n): np[i] = (0, 0, 0) np.write() The next function (middle right) is called bounce, which, as you might expect, causes a single LED to move through each available position and, when it gets to the starting point, it reverses direction.

The set_color function, again, does as the name suggests. It sets all the pixels (defined in n) to the RGB color at the brightness you desire. def set_color(r, g, b, brightness): for i in range(n): np[i] = (int(r*brightness), int(g*brightness), int(b*brightness)) np.write() The cycle function (bottom right) will cause a single LED to light and walk through the strip at the defined color and brightness, at a defined speed. The next function, wheel (next page, top right), is a support function used by the rainbow_cycle function below.

Now that we have all of the functions defined, we’ll walk through each of them, one at a time. We’ll print to the REPL terminal which function is being run. print('Clear') clear() print('Set Color') set_color(0,102,.5,230) time.sleep(2) clear() print('Cycle') cycle(0,102,230,.5,200) print('Bounce') bounce(0,255,250,.5,200) clear() time.sleep(2) print('Rainbow Cycle') rainbow_cycle(.5,5) clear() print('Program Ends')

It should be fairly easy to understand how the program actually works. I’ve included on the github repository a port of the fireflies program for the ESP called fireflies1.py. As an added benefit, I’ve added a small call to the Touchpad function we discussed previously that will break the forever loop when the touchpad is detected. All of the code and image files can be found at https://github.com/gregwa1953/FCM171_MicroThisMicroThat . I sincerely hope that I have inspired you to get some NeoPixel type devices and play with them. After all (as if I needed to give you a reason), Christmas, Hanukkah, and the rest of the festive holiday season celebrations are less than 6 months from now. Just picture what these little devices can do for your decorations this year and how jealous your neighbors will be. Until next time, as always; stay safe, healthy, positive and creative!

issue171/micro-ci_micro-la.1628080744.txt.gz · Dernière modification : 2021/08/04 14:39 de d52fr