issue171:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue171:micro-ci_micro-la [2021/08/01 19:22] – créée auntiee | issue171:micro-ci_micro-la [2021/08/06 10:16] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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. | + | **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, | 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, | ||
- | 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:// | + | 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:// |
- | Setting up your breadboard | + | Ce mois-ci, nous allons parler de l' |
+ | |||
+ | NeoPixels est une marque qui appartient à Adafruit. Elle désigne les LEDs de couleur RVB adressables individuellement ; des bandes toutes basées sur les LEDs WS2812 et WS2811 avec des pilotes qui utilisent un protocole à fil unique. Donc, TECHNIQUEMENT, | ||
+ | |||
+ | Les microcontrôleurs ESP32 et ESP8266 ont tous deux un pilote qui est inclus dans la distribution de MicroPython. Pour le Raspberry Pi Pico, la communication avec les LEDs doit se faire via la programmation PIO - le langage d' | ||
+ | |||
+ | **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. | 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. | + | 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, | + | Configurer votre planche d' |
+ | |||
+ | Les dispositifs de type NeoPixel prennent une bonne quantité d' | ||
+ | |||
+ | J' | ||
+ | |||
+ | **While the connections for the RPi Pico and the ESP32/8266 are very similar, I will include one of each. In the illustrations, | ||
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. | 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. | + | 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 | + | Bien que les connexions pour le RPi Pico et l' |
+ | |||
+ | Comme vous pouvez le voir sur l' | ||
+ | |||
+ | Une autre chose à noter, c'est que les images de la plaque d' | ||
+ | |||
+ | **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. | 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. | ||
Ligne 23: | Ligne 41: | ||
ESP32/8266 | 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 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.** |
+ | |||
+ | Raspberry Pi Pico | ||
+ | |||
+ | Pour le Pico, nous avons placé le pack de 3 piles AA sur les rangées + et - de la plaque d' | ||
+ | |||
+ | ESP32/ | ||
+ | |||
+ | Les connexions de la plaque d' | ||
- | The Code | + | **The Code |
Raspberry Pi Pico | Raspberry Pi Pico | ||
Ligne 37: | Ligne 63: | ||
from ws2812b import ws2812b | 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. | + | 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. | + | Le code |
+ | |||
+ | Raspberry Pi Pico | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | Nous allons d' | ||
+ | |||
+ | import time | ||
+ | |||
+ | from ws2812b import ws2812b | ||
+ | |||
+ | Maintenant nous allons regarder les deux premières lignes de code. La première définit le nombre de LEDs que vous avez sur votre appareil. Assurez-vous que cela correspond au nombre de LEDs que vous avez réellement. Beaucoup d' | ||
+ | |||
+ | **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 | num_leds = 24 | ||
Ligne 46: | Ligne 86: | ||
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. | 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, | ||
+ | |||
+ | pixels.show()** | ||
+ | |||
+ | La deuxième ligne instancie la classe ws2812b dans la variable pixels. Remarquez que le premier paramètre est le nombre de LED, le second est la machine d' | ||
+ | |||
+ | num_leds = 24 | ||
+ | |||
+ | pixels = ws2812b(num_leds, | ||
+ | |||
+ | Ensuite vient la valeur de la luminosité. Cela peut être utile si vous êtes comme moi et que les LED vous aveuglent parce qu' | ||
pixels.brightness(30) | pixels.brightness(30) | ||
Ligne 53: | Ligne 107: | ||
pixels.show() | 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. | + | **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. | 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. | ||
Ligne 70: | Ligne 124: | ||
… | … | ||
- | When we run the program, all the LEDs will blink different colors in a quickly changing pattern. | + | 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. | + | À ce stade, nous mettons en place une simple boucle (en haut) qui, pour chaque LED que vous avez configurée au début du code, définit la couleur. Ensuite, elle envoie les données et s' |
+ | |||
+ | Le code peut sembler assez simple jusqu' | ||
+ | |||
+ | Vous pouvez voir que la fonction set_pixel prend 4 paramètres. Le numéro du pixel, et les valeurs rouge, verte et bleue. | ||
+ | |||
+ | En revenant à la boucle ci-dessus, nous pouvons parcourir le code et voir les valeurs qui sont envoyées au dispositif de LED. Je ne vais montrer que 7 valeurs, ce qui devrait vous donner une bonne idée de ce qui se passe. | ||
+ | |||
+ | 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 | ||
+ | ... | ||
+ | |||
+ | Lorsque nous exécutons le programme, toutes les LEDs clignotent de différentes couleurs dans un schéma qui change rapidement. | ||
+ | |||
+ | **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. | ||
Ligne 87: | Ligne 160: | ||
strip = ws2812b.ws2812b(numpix, | strip = ws2812b.ws2812b(numpix, | ||
- | Remember to set the number of LEDs that your device has as well as the GPIO pin that the device is connected to. | + | Remember to set the number of LEDs that your device has as well as the GPIO pin that the device is connected to.** |
- | colors = [ | + | L' |
+ | |||
+ | |||
+ | import time | ||
+ | |||
+ | import ws2812b | ||
+ | |||
+ | import random | ||
+ | |||
+ | numpix = 24 # Nombre de NeoPixels | ||
+ | |||
+ | # La broche 16 est celle où les NeoPixels sont connectées | ||
+ | |||
+ | strip = ws2812b.ws2812b(numpix, | ||
+ | |||
+ | N' | ||
+ | |||
+ | **colors = [ | ||
[232, 100, 255], # Purple | [232, 100, 255], # Purple | ||
[200, 200, 20], # Yellow | [200, 200, 20], # Yellow | ||
Ligne 101: | Ligne 191: | ||
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. | 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 | + | 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 | + | colors = [ |
+ | [232, 100, 255], # violet | ||
+ | [200, 200, 20], # Jaune | ||
+ | [30, 200, 200], # Bleu | ||
+ | [150, | ||
+ | [50, | ||
+ | ] | ||
+ | |||
+ | Ici (en haut à droite) max_len et min_len sont les durées maximale et minimale d' | ||
+ | |||
+ | Il s'agit de la fonction d' | ||
+ | |||
+ | La plupart des programmes de démonstration que j'ai trouvés ont des boucles similaires à celles des deux programmes ci-dessus. Et ils ont une boucle while similaire qui fait que, lorsqu' | ||
+ | |||
+ | **# LED Clear | ||
from ws2812b import ws2812b | from ws2812b import ws2812b | ||
+ | num_leds = 24 | ||
+ | |||
+ | pixels = ws2812b(num_leds, | ||
+ | |||
+ | pixels.brightness(100) | ||
+ | |||
+ | pixels.fill(0, | ||
+ | |||
+ | pixels.show() | ||
+ | |||
+ | ESP32/ | ||
+ | |||
+ | 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.** | ||
+ | |||
+ | # Extinction des LED | ||
+ | |||
+ | from ws2812b import ws2812b | ||
num_leds = 24 | num_leds = 24 | ||
Ligne 120: | Ligne 241: | ||
ESP32/ | ESP32/ | ||
- | 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 | + | Mon idée initiale était de réécrire le programme |
- | 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. | + | **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. | 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. | ||
Ligne 134: | Ligne 255: | ||
np.write() | 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 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. | + | Remarquez que la bibliothèque pour les dispositifs neopixel est nommée « neopixel ». La variable n est le nombre de LED que possède le dispositif, et la variable p est la broche GPIO qui sera utilisée. |
+ | |||
+ | La variable np est donc l' | ||
+ | |||
+ | def clear() : | ||
+ | |||
+ | for i in range(n) : | ||
+ | |||
+ | np[i] = (0, 0, 0) | ||
+ | |||
+ | np.write() | ||
+ | |||
+ | La fonction suivante (au milieu à droite) s' | ||
+ | |||
+ | **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, | def set_color(r, | ||
Ligne 148: | Ligne 283: | ||
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 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. | + | 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. | + | La fonction set_color, encore une fois, fait ce que son nom suggère. Elle définit, pour tous les pixels (définis dans n), la couleur RVB et la luminosité que vous souhaitez. |
+ | |||
+ | def set_color(r, | ||
+ | |||
+ | for i in range(n) : | ||
+ | |||
+ | np[i] = (int(r*luminosité), | ||
+ | |||
+ | np.write() | ||
+ | |||
+ | La fonction cycle (en bas à droite) permet d' | ||
+ | |||
+ | La fonction suivante, wheel (page suivante, en haut à droite), est une fonction de support utilisée par la fonction rainbow_cycle ci-dessous. | ||
+ | |||
+ | **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(' | print(' | ||
Ligne 183: | Ligne 332: | ||
print(' | print(' | ||
+ | ** | ||
+ | Maintenant que nous avons défini toutes les fonctions, nous allons les passer en revue, une par une. Nous allons indiquer au terminal REPL quelle fonction est exécutée. | ||
- | 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. | + | print(' |
+ | |||
+ | clear() | ||
+ | |||
+ | print(' | ||
+ | |||
+ | set_color(0, | ||
+ | |||
+ | time.sleep(2) | ||
+ | |||
+ | clear() | ||
+ | |||
+ | print(' | ||
+ | |||
+ | cycle(0, | ||
+ | |||
+ | print(' | ||
+ | |||
+ | bounce(0, | ||
+ | |||
+ | clear() | ||
+ | |||
+ | time.sleep(2) | ||
+ | |||
+ | print(' | ||
+ | |||
+ | rainbow_cycle(.5, | ||
+ | |||
+ | clear() | ||
+ | |||
+ | print(' | ||
+ | |||
+ | **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:// | All of the code and image files can be found at https:// | ||
Ligne 191: | Ligne 374: | ||
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. | 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! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Il devrait être assez facile de comprendre comment le programme fonctionne vraiment. J'ai inclus dans le dépôt github un portage du programme fireflies pour l'ESP appelé fireflies1.py. En prime, j'ai ajouté un petit appel à la fonction Touchpad dont nous avons parlé précédemment qui interrompra la boucle éternelle lorsqu' | ||
+ | |||
+ | L' | ||
+ | |||
+ | J' | ||
+ | |||
+ | Jusqu' |
issue171/micro-ci_micro-la.1627838540.txt.gz · Dernière modification : 2021/08/01 19:22 de auntiee