Ceci est une ancienne révision du document !
Welcome back to the crazy world of Python Programming in the real world. Before we get started, I need to make a confession. Last time I goofed. The images in part 63 are wrong. The LEDs are backwards from what they should be. Brian Kelly noted this and was brave enough to point out the old man’s errors. Thank you Brian. If you follow the text, you should be good to go. Secondly, I have to apologize for not making it last month (FCM#106). I’m having more medical issues that are keeping me from sitting for too long. Hopefully this will be taken care of soon. Enough of that. Now for this month’s offering. The Mystery LED In the last two articles, we learned how to turn on and off LEDs programmatically. That was simple enough. This is digital output as opposed to analog output. The RPi, unlike the Arduino, cannot do analog I/O. So we are limited to turning a GPIO pin (and in this case, a LED) either on or off. This time we will be using that knowledge to do something pretty interesting. So get your Pi and your breadboard and we’ll start working.
Bon retour dans le monde assez fou de Python Programming dans la vraie vie. Avant de commencer, j'ai besoin de vous avouer quelque chose. La dernière fois, j'ai fait une grosse faute : les images dans la partie 63 sont erronées. Les LED sont à l'inverse de ce qu'ils devraient être. Brian Kelly l'a remarqué et était assez courageux de signaler les erreurs du vieillard que je suis. Merci, Brian. Si vous suivez le texte, cela devrait aller.
Ensuite, je dois vous présenter des excuses, car je n'ai pas pu faire mon article le mois dernier (dans le FCM n° 106). J'ai encore des problèmes de santé qui m'empêchent de rester assis pendant longtemps. En principe, cela devrait être corriger bientôt.
Bon, ça suffit. Maintenant pour ce que je vous propose ce mois-ci.
Le LED mystère
The Wiring
You will need a Raspberry Pi, a breadboard, two LEDs - one Red and one White, two 220 Ohm resistors and 3 jumper wires.
I’ve used the original Pi for this wiring image example. If you have a Pi B+ or 2B (or even the brand new 3), the pins at this point are exactly the same.
Just to avoid confusion (on my side), the Cathodes (Negative side) of the LEDS are connected to the resistors going to ground, and the Anodes (Positive side) are connected through the jumper wires to the Pi pins. The positive side of the LED is usually marked by the longer lead and the negative side is the one that has the flat spot on the base of the LED.
The Code
I won’t explain the code just yet. Just put into the editor as it is. We will discuss it in a bit.
Once you have the code entered correctly, then run it and see what happens.
The Reveal
If you have been paying attention over all these years, you probably have figured out what the code is doing. If you can’t figure it out, don’t feel bad. We’ll jump into the explanation.
Instead of the LEDs being either on or off, they pulse on and off. Since I said earlier, we can only send out (or read) a On/Off voltage (or 1/0, or High/Low), so how can this be?
We are using a trick called PWM or Pulse Width Modulation. We are still living with the rules, but we are bending them to our benefit. The pictures below, taken from my oscilloscope connected to the project, should help explain a bit clearer. We will be concerned with only one LED at this point.
If we send out a Low to the GPIO pin to the LED it's zero volts. The LED is getting nothing on the Anode, so it is off. In the last two articles, when we turned the LED on by sending the Anode of the LED a High So we have in the first instance a zero, and in the second a 1. Just like we have assumed… either Off or On.
This time we vary the amount of time that the GPIO signal is high and low. If we do it slowly, the LED would simply flash on and off in response to the voltage. In the case of this version, we are switching it on and off very quickly and at the same time, changing the amount of time it is on compared to off, which is called the duty cycle.
You can see that the signal is on for about 80% of the time and off for about 20%, which would be a 80% duty cycle. By doing this quickly, the LED reacts by dimming a bit from the 100% on all the time. As the program does its loop, it changes the duty cycle and makes the high longer or shorter depending on what part of the loop it is. In the picture above, we have a duty cycle of about 5%. In this case the LED is turned on for such a short time, that it is extremely dim and for all intents and purposes it is off.
Now, let’s start taking apart the code.
import RPi.GPIO as GPIO from time import sleep
As always, we start with our imports. We import the GPIO library, and this time, we import the sleep function from the time library. You will understand the reason for that shortly.
GPIO.setmode(GPIO.BCM) GPIO.setup(25,GPIO.OUT) GPIO.setup(24,GPIO.OUT) white = GPIO.PWM(25,100) red = GPIO.PWM(24,100)
In these five lines, we set the GPIO mode to BCM, and set the GPIO pins 24 (physical pin 9) and 25 (physical pin 11) to be output pins. We have done this before. Now we set the values for the PWM to 100% duty cycle.
white.start(0) # start white led on 0 percent duty cycle (off)
red.start(100) # red fully on (100%)
We next turn the Red LED on (100%) and the white LED to 0 volts.
pause_time = 0.05
print(“Program Starting…Press CTRL+C to exit”)
We set the pause_time variable to 0.05 seconds. This makes it fast enough to (hopefully) not allow for a flicker.
In the next block of code, we do our loops. The first loop is to make the white LED get “brighter” and the red LED to get “dimmer”. The second is to reverse the process. Just using the first loop as an example, we use a FOR LOOP to set the value of i and then we set the duty cycle for the white LED to i and that of the red LED to 100-i.
Notice that we have wrapped this with a TRY…EXCEPT set. This allows us to continue to run until the user enters CTRL+C. When that happens, we fall out of the TRY side so we can do our clean up code.
So now you know that we can bend the rules to our use.
Next time, we will start to examine a different GPIO library. Until then, have fun.
