WHAT’S HOT NOW

Code

Arduino

General

Electronics

» » » How to use pulse width modulation on the ARDUINO.

 HOW TO USE ARDUINO PULSE WIDTH MODULATION



There are just two voltages that the Arduino can output: 0 volts and 5 volts. However, a lot of equipment needs to be powered by a voltage range between 0 and 5 volts, including LEDs, servos, and motors. Fortunately, the Arduino is able to replicate any voltage between 0 volts and 5 volts via pulse width modulation.
We'll discover how pulse width modulation functions in this post, as well as how to create pulse width modulation signals using an Arduino Uno.

modulation of pulse width

A pulse width modulation signal consists of a series of brief, high-frequency current pulses. The voltage rapidly changes from 5 volts to 0 volts while the signal appears to be a square wave:



There are 500 of these cycles per second on the Arduino because the pulse width modulation frequency is approximately 500 Hz. Each cycle lasts only 2 milliseconds on average.
The pulse width is the length of the peak point of the signal. The cycle duration of a signal is the length of time between its highest and lowest points. The duty cycle is the ratio of the cycle duration to the pulse width duration: it is the signal intensity.

We may adjust the apparent voltage to any value between 0 and 5 volts by adjusting the duty cycle:



A duty cycle of 50%, for instance, results in a pulse width that is 50% of the cycle, making the apparent voltage 50% of 5 volts, or 2.5 volts. This corresponds to a value of 127 for analogWrite.

Only Arduino pins with a "" next to them can produce pulse width modulation signals:

USE ANALOGWRITE() TO GENERATE PULSE WIDTH MODULATION SIGNALS

A pulse width modulation signal can be created using the analogWrite() method. Pin and value are the two arguments required.

analogWrite(pin, value);

The pin argument indicates which pin will be used to generate the pulse width modulation signal. The analogWrite value that represents the duty cycle of the pulse width modulation signal is the value argument. Use this formula to determine the analogWrite value that will result in a pulse width modulation signal with a particular apparent voltage:


PROJECT example

Let's construct a demonstration project that automatically adjusts an LED's brightness using pulse width modulation.
You will require the following components to complete the project:
  • Arduino Uno
  • 220 Ohm resistor
  • Jumper wires
  • One LED
  • Breadboard


Follow the LED-based diagram to connect the limiting resistor and the international electronic art Arduino.


The current limiting resistor can have any value from 220 Ohms up to 1,000 Ohms.

PULSE WIDTH MODULATION PROGRAMMING FOR THE ARDUINO
int ledPin = 6; int brightness = 0; int fadeAmount = 5; void setup() { pinMode(ledPin, OUTPUT); } void loop() { analogWrite(ledPin, brightness); brightness = brightness + fadeAmount; if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; } delay(30); }

EXPLANATION OF THE CODE



To save the pin number attached to the LED, we declare an int variable named ledPin at the top of the sketch. The analogWrite values will be stored in an int variable named brightness, which will cycle between 0 and 255. Additionally, we declare the variable fadeAmount, which will be used to regulate how quickly the LED turns on and off.

We configure the ledPin as an output in the setup() section by using the pinMode() function.
The pulse width modulation signal is produced by the analogWrite() method found in the loop() section. We use the ledPin variable as the first argument since it contains the pin number for the device that will provide the pulse width modulation signal. The analogWrite value, which controls the duty cycle, is the second argument. The brightness variable that we created to hold that value is utilized as the second argument.

We need an if statement based on equality that stipulates that if the brightness is a lot less compared to or equal to zero, or if brightness is a lot more than or equal to 255, then execute the code in the body. In the body of the if statement, we have fadeAmount - fadeAmount;.

HOW THE PROGRAM WORKS

The program's brightness is initially set to zero:

int brightness = 0;


Brightness is equal to 0 in the first loop () cycle, so our pulse width modulation signal has a duty cycle of 0 and is sent to the ledPin (pin 6), and the LED goes off completely.

analogWrite(ledPin, brightness);

On the second line, the fadeAmount variable is added to the brightness variable. We declared it equal to 5 at the top of the sketch, so brightness will now equal 5.

brightness = brightness + fadeAmount;


The if statement's condition now inquires, "Is brightness less than or equal to 0, or more than or equal to 255? ”:

if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; }

The code inside the if statement is not executed, and the program jumps to the first line of code after the if statement, which is a delay of 30 milliseconds.

delay(30);

The program now goes back to the beginning of the loop() section. The analogWrite() function now writes an analogWrite value of 5 to the LED because the brightness variable's current value is 5.

Brightness is increased by fadeAmount once more on the following line, bringing it to a total of 10. The if clause determines whether 10 is higher than or equal to 255 or less than or equal to 0. The code inside the if statement is again skipped because it isn't. Every time the loop() section is invoked, the software increases the brightness value by 5 and repeats the process.

But as soon as the brightness setting reaches 255, the LED will be at maximum brightness. When the Arduino gets to the if statement, it checks to see if 255 is greater than or equal to 255 and then moves on to the if statement's body:
face amount = -fadeAmount;

As a result, the value kept in fadeAmount is changed from 5 to -5.

When the program reaches brightness = brightness + fadeAmount in the following iteration of the loop, 5 will be deducted from brightness because fadeAmount is now equal to -5. As a result, brightness drops from 255 to 250. The program continues to loop, deducting 5 from brightness with each cycle until brightness reaches 0.

If the reach of brightness is left unmodified at 0, the condition of the before statement will be true, and the program will enter the body of the if statement. Since fadeAmount is still -5, the expression will look like this:

fadeAmount = -(-5);

The face amount will be equal to positive 5 since the value of -5 (minus) is equivalent to positive 5 (plus). The brightness variable will now be increased by 5 each time the loop is executed. This causes the LED to turn on and dim again in an unending cycle, changing the value of the brightness cycle from 0 to 255.

The value of -5 (minus) is equal to positive 5 (plus), meaning that face amount will equal positive 5. Now 5 will be added to the brightness variable every time through the loop. This changes value in the brightness cycle from 0 to 255, making the LED light up and become dimmer once again in an endless loop.




Tutorials World

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply