PWM Application Examples with Arduino Nano

In this tutorial we will show different application examples of PWM(Pulse Width Modulation) using Arduino Nano. First we explain briefly about PWM, then explain how to generate PWM signal with Arduino Nano. Afterwards we show different application example of PWM which includes controlling brightness of a LED with software alone and using Potentiometer, control of motor and sound generation.

Pulse Width Modulation

PWM stands for Pulse Width Modulation which is a signalling technique where pulses of different widths are generated. The pulse widths are time duration over which voltage stays HIGH and LOW for a given duty cycle. The longer the pulse width the longer the output voltage. PWM signal is often referred as analog signal but in reality it is not real continuous analog signal rather they are square waves which are repeatedly turned on and off with varying pulse width at such a high rate which gives perception to human that they are continuous signal. Humans can see or detect flickering of signals upto around 400Hz. The PWM signal generated from Arduino Nano/Uno are 490Hz or 970Hz depending upon the pin used.


PWM signal are often specified in terms of Duty Cycle. Duty Cycle(%) specifies how long the pulse stays HIGH and LOW for given time period. For example, a PWM signal with 25% duty cycle and 0.1 second time period, the signal stays high for 0.025 seconds and stays low for 0.075 seconds. The frequency of the PWM signal is in this case 10Hz(1/0.1). Below picture shows PWM signal with duty cycle of 0%, 25%, 50%, 75% and 100%. The PWM signal has amplitude of 5V(HIGH) and 0V(LOW), frequency of 10Hz and time period of 0.1 second.

PWM signal with different duty cycle

The signal above was generated in Simulink using PWM generator block. 

PWM generator Simulink

For generating PWM signal with Arduino using matlab code see PWM - Programming Arduino using Matlab where analogPWMWrite() function is used.


Function for generating PWM signal with Arduino

With Arduino we can generate PWM signal using the analogWrite() function. 

The syntax is:

analogWrite(pin,value) 

The pin parameter is the pin number which must be capable of generating PWM signal. For Arduino Nano or Arduino UNO the PWM pins are 3,5,6,9,10 and 11. The pins 3, 9, 10 and 11 generates PWM frequency of 490Hz and pins 5 and 6 generates PWM frequency of 980Hz. 

The value parameter ranges from 0 to 255 corresponding to 0% and 100% duty cycle. For 5V supply, value of 0 means 0V and 255 means 5V. Any number between 0 and 255 corresponds to voltage between 0V and 5V.


PWM example 1: Control Brightness of LED

In the first example application of PWM we will show how to to slowly increase and decrease brightness of a LED. For this a LED is connected to digital PWM pin 10 via a current limiting resistor of 220Ohm. The ground of Arduino and LED must be common.

The wiring diagram is shown below.

wiring diagram PWM controlled LED


Program code

const int led = 10;

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


void loop(){
   for(int k=0; k <=255; k++){
      analogWrite(led, k);
      delay(5);
      }

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

Below is breadboard demonstration picture of PWM for LED brightness control with Arduino Nano.

PWM with Arduino Nano application example of LED brightness control

PWM Application Example 2: Control brightness of LED using Potentiometer

The second application we will show is how to control the LED brightness with POT(potentiometer). Dimming a lamp light using a knob is an example where this is used. Here a 10 KOhm potentiometer is connected to the analog pin A0 of Arduino Nano. The two ends of the potentiometer is connected to 5V and 0V of power supply and the middle is connected to analog pin A0. By rotating the knob we generate voltage between 0V and 5V which is read by the Arduino Nano via the pin A0. This voltage is converted by the internal ADC(Analog to Digital Converter) to values 0 to 1023 by arduino Nano. This value ranges from 0 to 1023 because ADC of Arduino Nano is 10 bits. So the quantization levels is from 0 to 2^10-1(=1023).

Wiring Diagram of Potentiometer controlled LED

wiring diagram of potentiometer controlled LED

Program code for Potentiometer controlled LED

const int pot = 0;
const int led = 10;


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

void loop(){
	int p = analogRead(pot);
	p = map(p, 0, 1023, 0, 255);
	analogWrite(led, p);
}

The following picture shows PWM with Potentiometer(POT) controlled LED using Arduino Nano on a breadboard:

PWM with POT controlled LED using Arduino Nano on a breadboard


PWM Application Example 3: PWM Controlled DC motor

The third application example of using PWM is the control of a DC motor. For small voltage and current rated DC motors like CD/DVD drive motors we can directly attached the DC motor to the Arduino Nano. But for higher rated DC motors we must use diode protection and capacitive decoupling and transistors like in the previous tutorials Speed control of DC motor with PWM using Arduino. For even more current hungry DC motors you would need DC motor drivers like L293D as shown in the previous tutorial DC motor control using L293D Motor Shield and Arduino.

For our simple PWM controlled DC motor we will just hook up the small DC motor to the Arduino Nano. The following is the schematic diagram of interfacing DC motor with Arduino Nano for PWM control.

schematic PWM with POT controlled DC motor

Program code for PWM with POT for DC motor control using Arduino Nano

const int pot = 0;
const int led = 10;


void setup () {
	pinMode(pot, INPUT);
	pinMode(motor, OUTPUT);
}

void loop(){
   int p = analogRead(pot);
   p = map(p, 0, 1023, 0, 255);
   analogWrite(motor, p);
}

The following picture shows how to connect Arduino Nano with Potentiometer and DC motor on a breadboard.

Arduino Nano with Potentiometer and DC motor on a breadboard


PWM Application Example 4: PWM Controlled Sound


In the fourth example of PWM application we show here how to control sound from a Buzzer using POT and Arduino Nano. The wiring diagram of interfacing Piezo Buzzer and Potentiometer with Arduino Nano is shown below.

schematic of PWM controlled sound

Program code for controlling sound using PWM with POT and Arduino Nano is provided below.

const int pot = 0;
const int buzz = 10;


void setup () {
	pinMode(pot, INPUT);
	pinMode(buzz, OUTPUT);
}

void loop() {
   int p = analogRead(pot);
   p = map(p, 0, 1023, 0, 255);
   analogWrite(buzz, p);
}

The following picture shows buzzer and POT connected to Arduino Nano.

PWM controlled sound

Video Demonstration

The following video demonstrates LED brightness control. DC motor control and buzzer sound control using Potentiometer.



Conclusion

In this tutorial we showed how to control LED brightness, how to control a DC motor and how to control sound from a buzzer using Potentiometer generated PWM signal from Arduino Nano. 

If you have any other ideas of PWM application please leave in comment section.

Post a Comment

Previous Post Next Post