What Is PWM And How Is It Used In Arduino

Table of contents:

What Is PWM And How Is It Used In Arduino
What Is PWM And How Is It Used In Arduino

Video: What Is PWM And How Is It Used In Arduino

Video: What Is PWM And How Is It Used In Arduino
Video: Arduino Tutorial 8: Understanding Pulse Width Modulation (PWM) and the Arduino Analog Write Command 2024, November
Anonim

Let's figure out what is behind the PWM acronym, how it works, what it is for and how we can use it in working with Arduino.

PWM signal
PWM signal

Necessary

  • - Arduino;
  • - Light-emitting diode;
  • - 200 Ohm resistor;
  • - computer.

Instructions

Step 1

Arduino digital pins can only give two values: logic 0 (LOW) and logic 1 (HIGH). That's why they are digital. But Arduino has "special" conclusions, which are designated PWM. They are sometimes denoted with a wavy line "~" or circled or somehow distinguished from others. PWM stands for "Pulse-width modulation" or Pulse Width Modulation, PWM.

A pulse width modulated signal is a pulse signal of a constant frequency, but a variable duty cycle (the ratio of the pulse duration to its repetition period). Due to the fact that most physical processes in nature have some inertia, then sharp voltage drops from 1 to 0 will be smoothed out, taking some average value. By setting the duty cycle, you can change the average voltage at the PWM output.

If the duty cycle is 100%, then all the time at the digital output of the Arduino there will be a logic voltage of "1" or 5 volts. If you set the duty cycle to 50%, then half of the time at the output will be logical "1", and half - logical "0", and the average voltage will be 2.5 volts. And so on.

In the program, the duty cycle is set not as a percentage, but as a number from 0 to 255. For example, the command "analogWrite (10, 64)" will tell the microcontroller to send a signal with a duty cycle of 25% to digital PWM output # 10.

Arduino pins with pulse width modulation function operate at a frequency of about 500 Hz. This means that the pulse repetition period is about 2 milliseconds, which is measured by the green vertical strokes in the figure.

It turns out that we can simulate an analog signal at the digital output! Interesting, right ?!

How can we use this? There are a lot of applications! For example, these are LED brightness control, motor speed control, transistor current control, sound extraction from a piezo emitter …

Understanding Pulse Width Modulation
Understanding Pulse Width Modulation

Step 2

Let's take a look at the most basic example - controlling the brightness of an LED using PWM. Let's put together a classic scheme.

Circuit for demonstrating PWM in Arduino
Circuit for demonstrating PWM in Arduino

Step 3

Let's open the "Fade" sketch from the examples: File -> Samples -> 01. Basics -> Fade.

Opening a sample for demonstrating PWM in Arduino
Opening a sample for demonstrating PWM in Arduino

Step 4

Let's change it a bit and load it into the Arduino memory.

PWM demonstration sketch
PWM demonstration sketch

Step 5

We turn on the power. The LED gradually increases in brightness, and then gradually decreases. We have simulated an analog signal at the digital output using pulse width modulation.

Recommended: