Pulse Width Modulation in Arduino

Shawn Hymel
A free video tutorial from Shawn Hymel
Engineering Superhero
4.6 instructor rating • 1 course • 10,548 students

Learn more from the full course

Arduino Programming and Hardware Fundamentals with Hackster

Learn electrical engineering basics to build circuits and program Arduino to make wearables, robots, and IoT devices

06:47:50 of on-demand video • Updated February 2020

  • Master the fundamentals of Arduino programming with C/C++
  • Build functioning circuits on a breadboard
  • Control sensors, robots, and Internet of Things (IoT) devices using Arduino
  • Write programs that perform basic math, light up LEDs, and control motors
  • Design circuits and write code for your own project
English [Auto] Alright we know how to turn Ellies on and off. But what about dimming them. Well there's one real way to dim Ltds. And then there's another way we can trick our eyes into thinking that they're going actually dimming an LCD consists of changing the amount of current flowing through it. Let's say we have a red L D with a 220 ohm current limiting resistor connected to a 5 volt power supply. We get about $14 amps of current flowing through the LCD and it would be fairly bright. Now let's say we replaced that resistor with a 470 ohm resistor. This would reduce the current to about 6 Milli amps which would make the LCD much dimmer. This works well but it's hard to make this happen from a microcontroller that can really only control voltage programmable resistors do exist but it means a more complicated circuit. So there's a way we can fool our eyes into thinking an LCD is dimmer without using extra hardware. If you want to follow a long connect a 220 or 330 ohm resistor in series with an L D. Connect a wire from pin 3 on the Arduino to the resistor and connect a wire from the negative pin of the LCD to ground create a simple blinking program with pin 3 as the pin. So at the top Konst it l d pin equals 3. Then in set up pin mode L.E. deepen Khama output and in loop digital right Ellie the pin come a high delay 500 digital right LCD pin come a low delay 500 upload that to your Arduino. If you look at the LCD it should be blinking at a rate of once per second. Now change the argument to 50 milliseconds in both delay functions and upload that to your Arduino the LCD should be blinking much more quickly now. Finally change the delay arguments to 5 milliseconds. What do you think will happen. At this point the LCD is blinking so fast that your eyes can't see it. The LCD appears to be simply on. It takes 10 milliseconds for the LCD to run through one cycle of turning on and off. Right now it's 5 milliseconds on in 5 milliseconds off so we can say that it's at 50 percent duty cycle. If we were to change the time on to one millisecond and time off to 9 milliseconds it would still take 10 milliseconds for the LCD to blink once but we'd be at a 10 percent duty cycle upload this and take a look at your LCD now the LCD should appear much dimmer than before and that's how we trick our eyes into thinking we're seeing a dimmer LCD this technique of toggling opin and varying the percentage of time it's on versus off is known as pulse with modulation and is abbreviated P W.M. a p w m signal with 0 percent duty cycle is always off one with 25 percent is on only 25 percent of the time. 50 percent means the pin is on and off equal amounts for each cycle and 75 percent means that the signal is high for three fourths of the time. What do you think a 100 percent duty cycle would mean controlling brightness is a great way to add interesting effects to your LCD display. The bad thing about the way we're doing it is our code can't do something else while we're trying to control the LCD. However the good news is that Arduino has a built in function that will handle PWO for us while our code does something else. We'll look at that next.