Fun with Arduino 15 LED dimmer with Pulse Width Modulation, analogWrite()

We have used analogRead() to measure the voltage on an analog input pin. Arduino also has the opposite function available: analogWrite(). This name is misleading though. The analog inputs are ‘real’ … the applied voltage is measured by a 10 bit A/D converter. But the Arduino does not have a D/A converter on board. An ‘analog’ output signal is created, or rather ‘simulated’, by switching a digital output with a high frequency.

PWM_smallA slow load like a motor, or in the case of light, the human eye + brain, sees the average value. This method is called PWM, Pulse Width Modulation. If we connect a power FET module, we can control the brightness of a 12V LED strip. Or we can control the speed of a DC motor. There are many applications where PWM can be put to use.

UNO_PWM_smallThe outputs marked with a ~ symbol are the ones that can be used with the analogWrite() statement to generate a PWM signal. Pins 3, 9, 10, 11 run at a frequency of 490 Hz, pins 5 and 6 run at 980 Hz. This is not very high, most dedicated pwm controllers use 20 or 40 kHz … but it is high enough not to be seen as flicker and also a DC motor will run on it. The value range of analogWrite() is 0-255.

Read on below the video …

 

 

It is surprisingly easy to make a LED dimmer in Arduino software. If we have our potentiometer connected, all we need to do is map the potentiometer input range of 0-1023 to the 0-255 range of the analogWrite(pin, value) function.

#define POTM_PIN A0  // potmeter analog input 
#define LIGHTS_PIN 6 // output to FET module

void setup() {
}

void loop() {
  analogWrite(LIGHTS_PIN, map(analogRead(POTM_PIN), 0, 1024, 0, 256);
}

What? Can we make a LED dimmer with just one line of code? Yes we can. Note that just like with an analog input, an analog output pin does not need to be defined in setup() first … we can start to use it just like that.

Let’s test this … yes … we made a dimmer! When we rotate the potentiometer, the LEDs change brightness. To see what is happening, a Serial.print() can be added.

#define POTM_PIN A0  // potmeter analog input 
#define LIGHTS_PIN 6 // output to FET module
byte pwm;

void setup() {
  Serial.begin(9600);
}

void loop() {
  pwm = map(analogRead(POTM_PIN), 0, 1024, 0, 256);
  analogWrite(LIGHTS_PIN, pwm);
  Serial.println(pwm);
  delay(100);
}

Let’s set ourselves fun new goal: let’s think of LED strip overhead lighting above our model railway layout which we want to slowly fade on and off to simulate a day / night cycle. Or think of a LED strip lighting under your kitchen cabinets that can be dimmed and gently fades on and off to be pleasant to the eye. We’ll make this in the next video.

–0–

3 thoughts on “Fun with Arduino 15 LED dimmer with Pulse Width Modulation, analogWrite()

  1. Hi there!

    I am trying to make this exact project, but don’t know much about the circuit setup. Do you have a basic circuit sketch for reference?

    Thanks!!!

    Like

Leave a comment