Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

how to generate PWM signals using Arduino

how to  generate PWM signals using Arduino

Experiment 8

In this tutorial, we will learn how to program the Arduino to generate PWM signals

Hardware required

 1 * Arduino UNO

1 * USB Cable

1 * LED

1 * 220Ω Resistor

1 * Breadboard

Several jumper wires 

Principle of operation

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signals witched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration  of"on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED. In the following figure, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

Keyfunction :

●analogWrite()

Writes an analog value (PWM wave) to a pin. Can be used to light an LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(),the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). You do not need to call pinMode() to set the pin as an output before calling analogWrite(). 

Syntax :

analogWrite(pin, value)

Parameters:

pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on).

Returns :

nothing

Experiment code

//File name:   Experiment 8

//Description:  program Arduino to generate PWM signals  .

//Website: www.okuelectronics.com

//E-mail: support@okuelectronics.com

//Author: Okudzeto osei prince

//Date: 2019/08/08 

int ledpin=5; //definition digital 11 pins as pin to control the LED

void setup ()

{

  pinMode(ledpin,OUTPUT); //Set digital 5 port mode, the OUTPUT for the output

}

void loop()

{

   for (int a=0; a<=255;a++)  //Loop, PWM control of LED brightness increase

   {

     analogWrite(ledpin,a);   //PWM output value a (0~255)

     delay(15);                //The duration of the current brightness level. 15ms           

   }

   for (int a=255; a>=0;a--)  //Loop, PWM control of LED brightness Reduced

   {

     analogWrite(ledpin,a);   //PWM output value a (255~0)

     delay(15);                //The duration of the current brightness level. 15ms 

   }

   delay(100);                //100ms delay

}


 Compile the program and upload to Arduino UNO board. Now, you should see the LED lights up and gets gradually brighter, and then slowly turns dimmer. The process repeats circularly, and with the particular rhythm it looks like animals' breath


Add Comment