Modifications

Sauter à la navigation Sauter à la recherche
4 902 octets supprimés ,  20 février 2016 à 17:03
Contenu remplacé par « {{MicroPython-Hack-Prepare-NAV}} {{MicroPython-Hack-fading-core}} {{MicroPython-Hack-fading-TRAILER}} »
Ligne 1 : Ligne 1 :  
{{MicroPython-Hack-Prepare-NAV}}
 
{{MicroPython-Hack-Prepare-NAV}}
   −
{{traduction}}
+
{{MicroPython-Hack-fading-core}}
 
  −
== Contrôler la luminosité ==
  −
{{bloc-etroit|text=En plus de pouvoir allumer et éteindre une LED, il est également possible d'en contrôler la luminosité en utilisant un signal PWM (voir ci-dessous). L'utilisation d'un signal PWM est une technique très répandue pour obtenir une sortie ''variable'' sur une broche digital. Cela permet d'estomper la luminosité d'une LED:}}
  −
 
  −
[[Fichier:MicroPython-Hack-fading.jpg]]
  −
 
  −
== Composants nécessaires ==
  −
Vous aurez besoin:
  −
 
  −
* {{pl|66|LED standard 5mm}}
  −
* {{pl|45|Résistance de 100 Ohm}}
  −
* {{pl|34|Des fils}}
  −
* {{pl|53|Breadboard}} (optionnel mais facilite beaucoup les choses)
  −
 
  −
== C'est quoi PWM? ==
  −
PWM est l'acronyme anglais de "pulse width modulation" que l'on traduit par "modulation par largeur d'impulsions".
  −
 
  −
PWM est un terme fort répandu sur le net, Arduino et en hacking électronique... raison de laquelle nous allons le préserver tel quel.
  −
 
  −
La modulation par largeur d'impulsion (MLI en français) est une technique utilisée pour contrôler la puissance envoyée à un périphérique. Nous l'utiliserons dans [[Rasp-Hack-L293|ce tutoriel pour contrôler]] la quantité d'énergie alimentant le moteur et par conséquent sa vitesse de rotation.
  −
 
  −
Le graphique ci-dessous montre le signal PWM tel qu'il est envoyé par la broche PWM du Raspberry Pi
  −
 
  −
{{ADFImage|Rasp-Hack-L293-PWM-01.jpg|450px}}
  −
 
  −
Chaque 1/500 ième de seconde, la sortie PWM produit une impulsion. La longueur de cette impulsion (dans le temps) contrôle la quantité d'énergie qui alimente le moteur. Comme vous pouvez le constater sur le graphique la longueur de l'impulsion peut varier de 0 à 100%.
  −
 
  −
Sans impulsion, le moteur ne fonctionne pas, une courte impulsion le fera tourner lentement. Si l'impulsion est active pendant cinquante pour cent du cycle, le moteur recevra la moitié de la puissance qu'il recevrait avec des impulsions maximales (constante dans le temps).
  −
 
  −
== Branchement ==
  −
For this tutorial, we will use the {{fname|X1}} pin. Connect one end of the resistor de 100 Ohms to {{fname|X1}}, and the other end to the anode of the LED, which is the longer leg. Connect the cathode of the LED to ground.
  −
 
  −
[[Fichier:MicroPython-Hack-fading-01.png]]
  −
 
  −
By examining the Quick reference for the pyboard, we see that X1 is connected to channel 1 of timer 5 (TIM5 CH1). Therefore we will first create a Timer object for timer 5, then create a TimerChannel object for channel 1:
  −
 
  −
<nowiki>from pyb import Timer
  −
from time import sleep
  −
 
  −
# timer 5 will be created with a frequency of 100 Hz
  −
tim = pyb.Timer(5, freq=100)
  −
tchannel = tim.channel(1, Timer.PWM, pin=pyb.Pin.board.X1, pulse_width=0)</nowiki>
  −
 
  −
Brightness of the LED in PWM is controlled by controlling the pulse-width, that is the amount of time the LED is on every cycle. With a timer frequency of 100 Hz, each cycle takes 0.01 second, or 10 ms.
  −
 
  −
 
  −
To achieve the fading effect shown at the beginning of this tutorial, we want to set the pulse-width to a small value, then slowly increase the pulse-width to brighten the LED, and start over when we reach some maximum brightness:
  −
 
  −
<nowiki># maximum and minimum pulse-width, which corresponds to maximum
  −
# and minimum brightness
  −
max_width = 200000
  −
min_width = 20000
  −
 
  −
# how much to change the pulse-width by each step
  −
wstep = 1500
  −
cur_width = min_width
  −
 
  −
while True:
  −
  tchannel.pulse_width(cur_width)
  −
 
  −
  # this determines how often we change the pulse-width. It is
  −
  # analogous to frames-per-second
  −
  sleep(0.01)
  −
 
  −
  cur_width += wstep
  −
 
  −
  if cur_width > max_width:
  −
    cur_width = min_width</nowiki>
  −
 
  −
== Effet de battement ==
  −
If we want to have a breathing effect, where the LED fades from dim to bright then bright to dim, then we simply need to reverse the sign of {{fname|wstep}} when we reach maximum brightness, and reverse it again at minimum brightness. To do this we modify the {{fname|while}} loop to be:
  −
 
  −
<nowiki>while True:
  −
  tchannel.pulse_width(cur_width)
  −
 
  −
  sleep(0.01)
  −
 
  −
  cur_width += wstep
  −
 
  −
  if cur_width > max_width:
  −
    cur_width = max_width
  −
    wstep *= -1
  −
  elif cur_width < min_width:
  −
    cur_width = min_width
  −
    wstep *= -1</nowiki>
  −
 
  −
== Exercice avancé ==
  −
You may have noticed that the LED brightness seems to fade slowly, but increases quickly. This is because our eyes interprets brightness logarithmically ([http://www.telescope-optics.net/eye_intensity_response.htm la loi de Weber]), while the LED’s brightness changes linearly, that is by the same amount each time. How do you solve this problem? (Hint: what is the opposite of the logarithmic function?)
  −
 
  −
== Addendum ==
  −
We could have also used the digital-to-analog converter (DAC) to achieve the same effect. The PWM method has the advantage that it drives the LED with the same current each time, but for different lengths of time. This allows better control over the brightness, because LEDs do not necessarily exhibit a linear relationship between the driving current and brightness.
      
{{MicroPython-Hack-fading-TRAILER}}
 
{{MicroPython-Hack-fading-TRAILER}}
29 917

modifications

Menu de navigation