Saturday, January 14, 2023

What is PWM? How to control voltage using Arduino?

 


In this blog we will try to understand what PWM is and how it works before we start doing some exciting stuff. PWM stands for Pulse Width Modulation. Which in simple words means we reduce the power delivered by an electric signal by chopping it down into discrete part. It is just switching On and Off of the power supply at a very fast rate resulting in controllable average voltage. Don’t spin your head around let’s see an example:

Image source: https://docs.arduino.cc/learn/microcontrollers/analog-output 

Assume we have a supply of 5 Volts

In the first case when duty cycle is 50% means the switching is done in such a way that 50% of time it is on and other 50% is OFF which will give us around => 0.5*5V= 2.5 V

In the same way in second case 75% duty cycle which means 75% of the time it remains On and other 25 % of the time it remains OFF which will give us around => 0.75*5V= 3.75 V 

And for the last case we have a duty cycle of 25%. Try to find out how much output voltage we will get out of it and write your answer in comment section.


Just to be a little clear here a 100% duty cycle has 256 resolutions (switching) which means when we say 50% duty cycle it means 128 resolutions. We can control the switching at as low as 1 resolution using PWM which means around 0.4 % duty cycle trust me that’s a lot of control. 


Now we will try to do it practically using an Arduino UNO just to see what it means and how it looks. You just need to know very basics of Arduino which I have explained in my last blog post. So, an Arduino can output a voltage of 5 Volts max in general. Which means when we send a command of ON on digital pins, we output 5 Volt from that pin. 

Photo by Harrison Broadbent on Unsplash


But maybe you have noticed there is tilde “ ~ “ sign on some of the digital pins and that denotes that the particular digital pin can be used for PWM. Just for little clarification Digital pin number 2, 5, 6, 9, 10 and 11 are PWM pins. Let’s start with pin number 11. So, we need to connect our Arduino UNO with a computer first which has Arduino IDE installed in it. If you haven’t done that, please refer to this blog post make sure you follow all those steps in the video to get your first program running on Arduino IDE. After you are good to go just initiate the pin by using:

void setup(){

  pinMode(11, OUTPUT);

  }

Now we need to write in the loop for this time we gonna use analogWrite instead of digitalWrite because analog write gives us opportunity to send analog values between 0 and 255 resolutions. So the syntax is:

 analogWrite(pin_number, resolution)

resolution can take any value between 0 (always OFF) to 255 (always ON). Now we can try following code after void setup():

 void loop(){

  analogWrite(11,20);

  delay(500);

  analogWrite(11,80);

  delay(500);

  analogWrite(11,120);

  delay(500);

  analogWrite(11,160);

  delay(500);

  analogWrite(11,200);

  delay(500);  

  analogWrite(11,255);

  delay(500);

}

What we are trying to do in this code is increase the resolution from 20 to 255 to see how the led lights up to see the effects in reality just insert a LED between pin number 11 and GND as shown in image below.


And on Arduino UNO click on upload and see how the LED reacts to the code.

Complete code: 

void setup(){

  pinMode(11,OUTPUT);

  } 

void loop(){

  analogWrite(11,20);

  delay(500);

  analogWrite(11,80);

  delay(500);

  analogWrite(11,120);

  delay(500);

  analogWrite(11,160);

  delay(500);

  analogWrite(11,200);

  delay(500);  

  analogWrite(11,260);

  delay(500);

}


If you want to do it in a little smart way just add a loop so we don’t have to re-write the commands again and again. To do so we can simply use: 

int i;

void setup(){

  pinMode(11,OUTPUT);

  } 

void loop(){


    i = 0;

    while ( i <= 255 ){

        analogWrite(11, i );

        delay(10);

        i = i + 1;

    }


    i = 255;

    while ( i >= 0 ){

        analogWrite(11, i );

        delay(10);

        i = i - 1;

    }

}

If you have any doubts regarding these lines of code, just write in the comment section I will try my best to help you out there. So, this was very basics of how to control voltage using Arduino UNO's PWM pins.


Stay tuned :)  


2 comments:

  1. PWM, or Pulse Width Modulation, controls power delivery by rapidly switching signals on and off, much like how celebrities mats sundin sjukdom manage their energy and time for optimal performance.

    ReplyDelete

Program Arduino with Python | PyFirmata |

Before we dive deep into this topic it is very important to understand very basic of Arduino- like how to upload a program from Arduino IDE ...