issue107:python
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 | ||
| issue107:python [2016/04/11 08:43] – auntiee | issue107:python [2016/04/12 19:05] (Version actuelle) – erlevo | ||
|---|---|---|---|
| Ligne 11: | Ligne 11: | ||
| So get your Pi and your breadboard and we’ll start working.** | 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, | + | Bon retour dans le monde assez fou de Python Programming, dans la vraie vie. Avant de commencer, |
| - | 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' | + | 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' |
| - | Bon, ça suffit. Maintenant | + | Bon, ça suffit. Maintenant |
| Le LED mystère | Le LED mystère | ||
| + | Dans les deux derniers articles, vous avez appris à allumer et à éteindre des LED avec un programme. C' | ||
| + | Allez chercher votre Pi, votre carte d' | ||
| - | The Wiring | + | **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. | You will need a Raspberry Pi, a breadboard, two LEDs - one Red and one White, two 220 Ohm resistors and 3 jumper wires. | ||
| Ligne 27: | Ligne 29: | ||
| 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. | 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. | + | 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 | + | Le câblage |
| + | |||
| + | Il vous faut un Raspberry Pi, une carte d' | ||
| + | |||
| + | Pour cet exemple de câblage, j' | ||
| + | |||
| + | Pour éviter de s' | ||
| + | |||
| + | **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. | I won’t explain the code just yet. Just put into the editor as it is. We will discuss it in a bit. | ||
| Ligne 41: | Ligne 51: | ||
| 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? | 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. | + | 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. | + | Le code |
| + | |||
| + | Je n' | ||
| + | |||
| + | Une fois le code entré correctement, | ||
| + | |||
| + | La révélation | ||
| + | |||
| + | Si vous avez prêté attention aux articles depuis le début, vous avez sans doute compris ce que fait le code. Sinon, ne vous tracassez pas : une explication suivra. | ||
| + | |||
| + | Les LED ne sont ni allumées ni éteintes, mais, à la place, elles basculent rapidement entre allumé et éteint. Puisque j'ai bien dit plus tôt que l'on ne peut émettre (ou lire) un voltage On/Off (ou 1/0 ou High/Low), comment est-ce possible ? | ||
| + | |||
| + | Nous utilisons un truc appelé PWM ou Pulse Width Modulation (modulation de largeur d' | ||
| + | |||
| + | **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. | 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. | 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. | + | 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.** |
| + | |||
| + | Si nous envoyons un Low sur la sortie GPIO de la LED, cela fait zéro volt. La LED ne reçoit rien sur l' | ||
| + | |||
| + | Cette fois-ci, nous varions le laps de temps où le signal GPIO est High et Low. Si nous le faisons lentement, les LED clignoteraient en réponse au voltage. Dans le cas de cette version, nous basculons entre les deux états très rapidement, tout en changeant le laps de temps pendant lequel il est On comparé à Off, ce qui s' | ||
| + | |||
| + | Comme vous pouvez le voir, le signal est On pendant environ 80 % du temps et Off pendant 20 %, ce qui ferait un rapport cyclique de 80 %. En faisant cela rapidement, la LED réagit en s' | ||
| - | Now, let’s start taking apart the code. | + | **Now, let’s start taking apart the code. |
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| Ligne 70: | Ligne 100: | ||
| We next turn the Red LED on (100%) and the white LED to 0 volts. | We next turn the Red LED on (100%) and the white LED to 0 volts. | ||
| + | |||
| + | pause_time = 0.05 | ||
| + | |||
| + | print(" | ||
| + | |||
| + | Maintenant, examinons le code en détail. | ||
| + | |||
| + | import RPi.GPIO as GPIO | ||
| + | from time import sleep | ||
| + | |||
| + | Comme toujours, nous commençons par les imports. Nous importons la bibliothèque GPIO et, cette fois-ci, nous importons la fonction sleep (sommeil) de la bibliothèque time (temps). Vous allez rapidement comprendre pourquoi. | ||
| + | |||
| + | GPIO.setmode(GPIO.BCM) | ||
| + | GPIO.setup(25, | ||
| + | GPIO.setup(24, | ||
| + | white = GPIO.PWM(25, | ||
| + | red = GPIO.PWM(24, | ||
| + | |||
| + | Dans ces cinq lignes, nous avons réglé le mode GPIO sur BCM et configuré le pins GPIO 24 (pin physique 9) et 25 (pin physique 11) comme pins de sortie. Nous avons déjà fait cela. Maintenant, nous réglons les valeurs du PWM à un rapport cyclique de 100 %. | ||
| + | |||
| + | white.start(0) # Démarrer la LED blanche avec un rapport cyclique de 0% (off) | ||
| + | |||
| + | red.start(100) # La rouge allumée en permanence (100%) | ||
| + | |||
| + | Ensuite, nous allumons la LED rouge (100%) et réglons la LED blanche à 0 volts. | ||
| pause_time = 0.05 | pause_time = 0.05 | ||
| Ligne 75: | Ligne 130: | ||
| print(" | print(" | ||
| - | We set the pause_time variable to 0.05 seconds. This makes it fast enough to (hopefully) not allow for a flicker. | + | **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. | 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. | ||
| Ligne 84: | Ligne 139: | ||
| Next time, we will start to examine a different GPIO library. Until then, have fun. | Next time, we will start to examine a different GPIO library. Until then, have fun. | ||
| + | |||
| + | Nous réglons la variable pause_time à 0,05 seconde. Cela la rend assez rapide pour (je l' | ||
| + | |||
| + | Dans le segment de code suivant, nous faisons les boucles. L' | ||
| + | |||
| + | Remarquez que nous l' | ||
| + | |||
| + | Ainsi, vous savez maintenant que nous avons la possibilité de contourner les règles dans notre intérêt. | ||
| + | |||
| + | La prochaine fois, nous commencerons notre examen d'une différente bibliothèque GPIO. En attendant, amusez-vous bien. | ||
issue107/python.1460356993.txt.gz · Dernière modification : 2016/04/11 08:43 de auntiee
