Arduino > Sortie analogique (modulation de largeur d'impulsion ou «PWM»)

1.  Description

Arduino permet de synthétiser un signal analogique grâce à la modulation de largeur d'impulsions(«Pulse Width Modulation(PWM)»).

Le principe général du PWM est qu'en appliquant une succession d'états discrets (0 et 5V) pendant des durées bien choisies, on peut obtenir en moyenne sur une certaine durée n'importe quelle valeur intermédiaire:

«Pulse Width Modulation (PWM) is a fancy term for describing a type of digital signal. Pulse width modulation is used in a variety of applications including sophisticated control circuitry. A common way we use them here at SparkFun is to control dimming of RGB LEDs or to control the direction of a servo motor. We can accomplish a range of results in both applications because pulse width modulation allows us to vary how much time the signal is high in an analog fashion. While the signal can only be high (usually 5V) or low (ground) at any time, we can change the proportion of time the signal is high compared to when it is low over a consistent time interval.» source 

2.  «Duty-cycle»

«When the signal is high, we call this “on time”. To describe the amount of “on time” , we use the concept of duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform.

If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. Here is a graph that illustrates these three scenarios:

100% duty cycle would be the same as setting the voltage to 5 Volts (high). 0% duty cycle would be the same as grounding the signal. » source 

3.  analogWrite()

Les broches qui peuvent produire une sortie analogique sont marquées du mot PWM ou du symbole «~».

Pour activer une sortie analogique, il suffit de faire un appel à la commande analogWrite(broche,valeur). La valeur doit se situer entre 0 et 255 et détermine le rapport entre le HIGH (5V) et le LOW (0V) tel que démontré dans le schéma suivant:

Pour arrêter la sortie analogique il faut envoyer la commande digitalWrite(broche,LOW).

pinMode(11,OUTPUT);
analogWrite(11,127); //Activer une modulation a 50% (127/255) sur la broche 11
digitalWrite(11,LOW); //Arreter la modulation

4.  Exemples

5.  Utilisation avancé

5.1  Filtrer le PWM