issue171:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue171:micro-ci_micro-la [2021/08/04 14:39] – d52fr | issue171:micro-ci_micro-la [2021/08/06 10:16] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
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:// | ||
+ | |||
+ | 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 | **Setting up your breadboard | ||
Ligne 10: | Ligne 16: | ||
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.** | ||
+ | |||
+ | 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, | **While the connections for the RPi Pico and the ESP32/8266 are very similar, I will include one of each. In the illustrations, | ||
Ligne 16: | Ligne 28: | ||
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.** | ||
+ | |||
+ | 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 | **Raspberry Pi Pico | ||
Ligne 24: | Ligne 42: | ||
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/8266 | ||
+ | |||
+ | Les connexions de la plaque d' | ||
**The Code | **The Code | ||
Ligne 38: | Ligne 64: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 52: | Ligne 92: | ||
pixels.show()** | 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.fill(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. | **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. | ||
Ligne 71: | Ligne 125: | ||
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.** | ||
+ | |||
+ | À 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. | **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 88: | Ligne 161: | ||
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.** | ||
+ | |||
+ | 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 = [ | **colors = [ | ||
Ligne 102: | Ligne 192: | ||
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** | ||
+ | |||
+ | 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 | **# LED Clear | ||
Ligne 120: | Ligne 224: | ||
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.** | 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 | ||
+ | |||
+ | pixels = ws2812b(num_leds, | ||
+ | |||
+ | pixels.brightness(100) | ||
+ | |||
+ | pixels.fill(0, | ||
+ | |||
+ | pixels.show() | ||
+ | |||
+ | ESP32/ | ||
+ | |||
+ | Mon idée initiale était de réécrire le programme flash présenté pour le Pico afin qu'il fonctionne sur les microcontrôleurs ESP. Cependant, je me suis souvenu que j' | ||
**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. | ||
Ligne 134: | Ligne 256: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 148: | Ligne 284: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 183: | Ligne 333: | ||
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. | ||
+ | |||
+ | 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. | **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. | ||
Ligne 191: | Ligne 375: | ||
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.1628080744.txt.gz · Dernière modification : 2021/08/04 14:39 de d52fr