How to use Arduino PWM to control signal strength | applied electronics engineering

# How to use Arduino PWM to control signal strength

By Applied Electronics - Tuesday, January 17, 2017 No Comments
The PWM(Pulse Width Modulation) pins in arduino can be used to send out signal at various strength by transmitting pulses with various duty cycle. This then makes it possible to emulate analog signal. That is although it is not a true analog signal(continuously varying signal strength) the PWM digital signals allows us to behave like analog signal.

PWM signals are essentially digital signals and are shown with tilde sign ~  on the digital signal pins(3,5,6,9,10,11). By connecting to the load like LED we can control the amount of signal or length of the pulse that is transmitted to the LED. Hence we achieve control of signal strength in this way.

To illustrate the idea we connect a LED to the 10th pin which is one of the PWM pin on the arduino. One the other side of the pin we connect a resistor 220Ohm. Then we write program or sketch that sends out PWM signals with varying pulse length using for loop. The function that is used to send out PWM is the analogWrite( ) function. This function analogWrite() accepts two arguments- one is the pin to control and the other is the value to write to this pin. By changing the value to be written to the 10th pin using for loop we control the voltage across the LED. With arduino uno, the value that you can use range from 0 to 255, that is from 0 to 2^8-1 because you can write 8 bit signal to the PWM pins.

The circuit schematic is shown below(in proteus).

The code is below.

const int led = 10;

void setup() {
pinMode(led, OUTPUT);
}

void loop() {

for (int i =0; i<256; i++)
{
analogWrite(led, i);
delay(10);
}

for(int i=255; i>= 0; i--){
analogWrite(led, i);
delay(10);
}
}

See the video demonstration:

This is explained in the book Exploring Arduino: Tools and Techniques for Engineering Wizardry.