The site for those who love tinkering with Arduino, Raspberry Pi & Co.

Arduino and PWM (Pulse Width Modulation).

by

The analog of digital

In this article we deal with PWM (Pulse Width Modulation), which allows us to modulate the digital signal and gives us the possibility to use it as if it were an analog signal.

Let's learn PWM

duty cycle 25%
Example of PWM signal with duty cycle 25%

PWM is a type of pulse modulation, which, thanks to the action of the board's internal timer, allows us to change the duty cycle of our output signal.

The duty cycle is the percentage of time that the signal stays at a high value over a period.

Although this signal generates a square wave with a high (5 volt) and a low (0 volt) value, it can be used to simulate an analog output signal.

This type of modulation is foreseen because on the Arduino board the analog pins work only in input and do not give us the possibility of being used as output.

PWM has the purpose of making up for this lack. Also, PWM is only usable for an output signal and cannot be used for an input signal.

In fact, if we tried to read the input signal we would not receive the duty cycle value, but only the value that that pin has at that particular moment.

Let's test ourselves!

Arduino Uno pinout

The PWM pins are recognizable directly on the board. Generally, next to the pin number on the board there is also a symbol (tilde ~).

Furthermore, to better check the functions supported by the various pins, it is advisable to google the rel
ative pinout of your card.

Below I present an example of pinout of the Arduino Uno board. 

To drive the PWM pin you need the analogWrite function, which as we have already said, the output signal is not strictly analog but pretends to be.

Let's do a usual test and take:

  1. An Arduino Uno board;
  2. A breadboard;
  3. One led;
  4. A 220 ohm resistor;
  5. Wiring required for connection.

Once the material has been found, following the following scheme, we build the basis of our project.

board with led at pin 11

Now let's load the program onto the board.

#define PWM_PIN 9

int value = 0;

void setup () {
  Serial.begin (9600);
  pinMode (PWM_PIN, OUTPUT);
}

void loop () {
  / * analogWrite, as the second parameter accepts values from 0 to 
   * 255. 
  * /
  analogWrite (PWM_PIN, value);
  value ++;
  / * alternatively, in the more compact version we can
   *  to write:
   * value = (value + 1)> 255? 0: value + 1;
  * /
  if (value> 255) {
    value = 0;
  }
  delay (50);
}

Once we have tested the following code, we can see that as the value entered in the analogWrite increases, the intensity level of the LED also increases. Conversely, decreasing this value also decreases the intensity of the LED. Another thing we must take into consideration is that the values for the PWM are 256, from 0 for a duty cycle of 0% up to 255 for a duty cycle of 100%.

Uses and Customs of PWM

PWM is not only used to regulate LED intensity! It has a wider range of applications that can come in handy from home automation to robotics and beyond. Signal modulation of this type can be used to regulate the speed of an engine, for example to make a toy car go faster or slower. It is also used in servo motors, which for example are used in robotics. In general, this type of modulation is widely used in the world of automation to drive different types of actuators.


PWM hadware to try

Ti è rimasta una domanda o un dubbio?

Iscriviti e chiedi nella community

Leave a Reply

Your email address will not be published. Required fields are marked *