Outils pour utilisateurs

Outils du site


issue123:python

In the last few days, I have seen at least two projects on the web that deal with Raspberry Pi producing music. I’ve seen many over the years, but with two showing up on my virtual doorstep, I took it as a sign. With music being one of my hobbies, I decided that we would start a project that would look into using a RPi as a controller, based on a project called the Music Box. More information can be found at http://www.recantha.co.uk/blog/?p=14818 . With that in mind, one of the libraries that his project uses is the GPIOZERO library. I’ve mentioned this in the past, but have never really dealt with it in any depth. So, I thought a good starting point would be to spend this month in examining this library in detail by doing some simple LED type projects. That way, when we get to the actual hardware and software portion, we will all know some of the commands from its API. All of these projects are taken from the GPIOZERO documentation (http://gpiozero.readthedocs.io/en/stable/index.html ). There is a huge amount that the GPIOZERO library offers, and we will only be scratching the surface, so after you do these three projects, head over there and see some of the other things you can do. You’ll be glad you did.

Au cours des derniers jours, j'ai vu au moins deux projets sur le Web qui traitent de la production de musique par le Raspberry Pi. J'en ai vu pas mal au fil des ans, mais, puisque ces deux se sont pointés devant ma porte virtuelle, je l'ai pris pour un signe. La musique étant l'un de mes violons d'Ingres, j'ai décidé de commencer un projet qui examinerait l'utilisation du RPi comme contrôleur, basé sur un projet appelé Music Box (boîte à musique). Vous trouverez plus de détails sur http://www.recantha.co.uk/blog/?p=14818.

En gardant cela à l'esprit, l'une des bibliothèques utilisées par son projet est la bibliothèque GPIOZERO. Je l'ai déjà mentionné par le passé, mais je ne l'ai jamais traité en profondeur. J'ai donc pensé qu'un bon point de départ serait d'utiliser mon article pour examiner cette bibliothèque en détail, en faisant quelque projets simples de type LED. De cette façon, quand nous arriverons à la véritable partie matérielle et logicielle, on connaîtra certaines des commandes de son API.

J'ai trouvé tous ces projets dans la documentation de GPIOZERO (http://gpiozero.readthedocs.io/en/stable/index.html ). La bibliothèque GPIOZERO en propose une quantité énorme, et nous n'allons en prendre que trois ; après avoir fait ces trois projets, allez-y pour voir quelques-unes des autres possibilités. Vous ne le regretterez pas.

GPIOZERO is a collection of classes to make accessing some of the most common input and output devices like LEDs and buttons super easy. It also gives easy support for things like Analogue-to-Digital converters, proximity sensors, motion detectors and so much more. Traffic Signal For this project, we’ll need 3 LEDs (one each of Green, Yellow/Amber, and Red), 3 x 220 ohm resistors, a breadboard, and some jumpers. The cathodes of the LEDS are all connected to the ground buss. The anode of the green LED is connected to GPIO pin 4 (physical pin 7) through a resistor, the anode of the yellow LED is connected to GPIO pin 3 (physical pin 5) through another resistor, and the anode of the red LED is connected to GPIO pin 2 (physical pin 3) through the third resistor. The Fritzing diagram is provided below. The code is very simple (next page, top right). The line “lights = TrafficLights(2, 3, 4)” initializes the class with the GPIO pin numbers of the RED, AMBER and GREEN LEDs respectively. The rest of the code is pretty much self-explanatory, using a ‘while True’ loop, turning on and off the LEDs in a sequence that pretty much emulates a traffic signal. You can change the timing of the lights by changing the value in the sleep statements.

GPIOZERO est une collection de classes pour faciliter au maximum l'accès à certains des dispositifs d'entrée et de sortie les plus courants, tels que des LED ou des boutons. Il prend facilement en charge des choses comme des convertisseurs analogiques/numériques, des capteurs de proximité, des détecteurs de mouvements et bien plus encore.

Feux de signalisation

Pour ce projet, nous aurons besoin de 3 LED (une verte, une orange/ambré et une rouge), 3 résistances de 220 Ω, une plaque d'essai et quelques cavaliers.

Les cathodes des LED sont toutes connectées au bus de masse. L'anode de la LED verte est connectée à la broche GPIO 4 (broche physique 7) par une résistance, l'anode de la LED orange est connectée à la broche GPIO 3 (broche physique 5) en passant par une autre résistance, et l'anode de la LED rouge est connectée à la broche GPIO 2 (broche physique 3) en passant par la troisième résistance.

Vous trouverez le diagramme Fritzing ci-dessous.

Le code est très simple (page suivante, en haut à droite).

La ligne « lights = TrafficLights(2,3,4) » initialise la classe avec les numéros des broches GPIO pour, respectivement, la LED ROUGE, ORANGE et VERTE. Le reste du code est assez explicite, l'utilisation d'une boucle « while True » et l'allumage et l'extinction des LED dans une séquence inspirée par les feux de signalisation. Vous pouvez changer la temporisation des diodes en changeant la valeur dans les déclarations sleep.

Button LED For this project, we’ll need one LED of any colour, one 220 ohm resistor, a button, a breadboard,, and a number of jumpers. The cathode of the LED is, like the last project, connected to the ground buss and the anode is connected to GPIO pin 17 (physical pin 11) through the resistor. One side of the button is connected to GPIO pin 2 (physical pin 3) and the other side is connected to the ground buss. The code for this project is even simpler (bottom right). Notice in this project we don’t have a while True loop that keeps the code going. That’s because we are using the pause routine from the signal library. The button functions are referred to as callbacks that are “triggered” when the specified action (pressed or released) happens.

LED par bouton-poussoir

Il faut, pour ce projet, une LED de n'importe quelle couleur, une résistance de 220 Ω, un bouton-poussoir, une plaque d'essai et de nombreux cavaliers.

Comme dans le projet précédent, la cathode de la LED est connectée au bus de masse et l'anode est connectée à la broche GPIO 17 (broche physique 11) par la résistance. Un côté du bouton est connecté à la broche GPIO 2 (broche physique 3) et l'autre est connecté au bus de la mise à la terre.

Le code en est encore plus simple (en bas à droite).

Remarquez que ce projet ne comporte pas de boucle while True qui entretient le code. C'est parce que nous utilisons la routine pause de la bibliothèque signal. Les fonctions du bouton s'appellent des callbacks (des rappels) qui sont « déclenchés » lors d'une action spécifiée (appuyer sur ou relâcher).

Potentiometer We’ve talked before about the fact the RPi doesn’t have any analogue inputs. In this project we will be using a MCP3008 Analogue to Digital converter chip to handle the heavy lifting for us. While on a hardware basis, this project is more complicated than the previous two examples, the code is deceptively simple. For this project, we’ll need 5 LEDs, 5 220 ohm resistors, a 10K potentiometer, a MCP3008 Analogue-to-Digital converter chip, a breadboard, and a bunch of jumpers. The MCP3008 is a 8-channel (input) analogue-o-digital converter that uses only 4 pins to interface on the RPi side – thanks to SPI (which we discussed a while back). In this example, we will connect the wiper of the potentiometer to channel 0 (the first channel) or pin 1 of the MCP3008 ADC. If we needed more potentiometers, we would connect the wipers of them to other channel pins. The outputs of the MCP3008 are connected as follows… • GPIO 11 (physical pin 23 SPI CE0) → MCP3008 pin 13 (CLK) • GPIO 9 (physical pin 21 SPI MISO) → MCP3008 pin 12 (Dout) • GPIO 10 (physical pin 19 SPI MOSI) → MCP3008 pin 11 (Din) • GPIO 8 (physical pin 24 SPI SCLK) → MCP3008 pin 10 (CS (Chip Select))

Potentiomètre

Nous avons déjà parlé du fait que le RPi n'a pas d'entrées analogiques. Dans ce projet, nous utiliserons un circuit de conversion analogique/numérique MCP3008 pour faire le travail laborieux à notre place.

Pour ce qui concerne le matériel, ce projet est plus compliqué que les deux précédents, mais le code paraît simple.

Pour ce projet, il faut 5 LED, 5 résistances de 220 Ω, un potentiomètre 10 K, un circuit de conversion analogique/numérique MCP3008, une plaque d'essai et un tas de cavaliers.

Le MCP3008 est un convertisseur analogique/numérique à 8 canaux d'entrée qui n'utilise que 4 broches pour l'interface côté RPi, grâce à SPI (dont nous avons parlé il y a quelque temps). Dans cet exemple, nous connecterons le contact mobile du potentiomètre au canal 0 (le premier) ou à la broche 1 du convertisseur MCP3008. S'il nous fallait davantage de potentiomètres, nous connecterions leur contact mobile à d'autres broches de canaux. Les sorties du MCP3008 sont connectées comme suit : • GPIO 11 (broche physique 23 SPI CE0) → MCP3008 broche 13 (CLK). • GPIO 9 (broche physique 21 SPI MISO) → MCP3008 broche 12 (Dout). • GPIO 10 (broche physique 19 SPI MOSI) → MCP3008 brouche 11 (Din). • GPIO 8 (broche physique 24 SPI SCLK) → MCP3008 broche 10 (CS (Chip Select)).

The anodes of the LEDs are all connected through resistors to the RPi GPIO pins, and the cathodes are all connected to the ground buss. The GPIO pins are 5, 6, 13, 19, 26 which are physical pins 29,31,33,35 and 37. The MCP3008 is connected to the 3.3VDC output of the RPi on pins 16 and 15, and to ground on pins 14 and 9. As I said earlier, the code is pretty simple and straightforward (above). I selected this example because we will be using the MCP3008 in our next project. Again, we are using the signal.pause routine to continually loop the program until we interrupt it with a <Ctrl><C>. The LEDBarGraph class provides a simple way to have a value displayed by a number of LEDs (in our case 5 LEDs, but can be just about any number). By using the “pwm=True” parameter, the LEDs will fade on or off in response to the input value, which can be between -1 and 1. Positive values cause the LEDs to light from left to right, and negative values from right to left. Next month we will be starting the Music Box. Until then, enjoy playing with the GPIOZERO library.

Les anodes des LED sont toutes connectées aux broches GPIO du RPi en passant par des résistances et les cathodes sont toutes connectées au bus de masse. Les broches GPIO sont 5, 6, 13, 19, 26, autrement dit les broches physiques 29, 31, 33, 35 et 37. Le MCP3008 est connecté à la sortie 3,3VDC du RPi sur les broches 16 et 15 et à la masse sur les broches 14 et 9.

Comme j'ai dit plus tôt, le code est assez simple (ci-dessus).

J'ai choisi cet exemple parce que nous utiliserons le MCP3008 dans le projet suivant. À nouveau, nous utilisons la routine signal.pause pour créer une boucle continue jusqu'à ce que nous l'interrompions avec CTRL+C.

La classe LEDBarGraph fournit une façon simple de faire afficher une valeur par de nombreuses LED (dans notre cas, 5 LED, mais à peu près n'importe quel nombre irait). En utilisant le paramètre « pwm=True », les LED s'allument ou s'éteignent en fondu en réponse à la valeur d'entrée, qui se trouve entre -1 et 1. Les valeurs positives font que les LED s'allument de gauche à droite et les valeurs négatives, de droite à gauche.

Le mois prochain, nous commencerons la boîte à musique. En attendant, amusez-vous bien avec la bibliothèque GPIOZERO.

issue123/python.txt · Dernière modification : 2017/08/10 10:55 de andre_domenech