From the course: Learning Arduino: Pulse Width Modulation

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Faking PWM on a non-PWM pin

Faking PWM on a non-PWM pin - Arduino Tutorial

From the course: Learning Arduino: Pulse Width Modulation

Start my 1-month free trial

Faking PWM on a non-PWM pin

- Although you can output variable PWM signals to the specified PWM pins, you can also fake very basic PWM output on the other digital-output pins. This isn't something I would suggest doing for most projects, but it is a helpful step in understanding how PWM works. By creating a loop to very quickly toggle the power on and off on a non-PWM pin, I can see what PWM is doing. For this sketch, I have an LED plugged into pin 9 with a resistor in series. I also have an LED plugged into pin 7. In the Arduino IDE, I am going to select file, examples, basics, BareMinimum. First, I'll instantiate constant integer variables to call the pins. Next, I'll instantiate pin 7 as output. In the loop, I'll call the analogWrite function for pin 9, and set the value to 127. Now, I'm going to add a few lines of code for pin 7. With a goal of matching the brightness of pin 9, first, I'll use the digitalWrite function to set pin 7 to an output of high or five volts, then I'll pause the program with a delay…

Contents