Ceci est une ancienne révision du document !
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 traite de la production de musique par le Raspberry Pi. J'en ai vu pas mal au fil des ans, mais, puisque 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é la 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 passe, mais je ne l'ai jamais traité en profondeur. J'ai donc pensé que 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.
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.
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.
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))
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.