Skip to Content

LEDs and PWM

Posted on

The “Hello World” of electronics is a blinking LED. Arduino helpfully includes a built-in LED identified as pin 13. In the Arduino IDE there are example sketches (programs). Select File->Examples->01. Basics->Blink and upload the sketch to the Arduino board. Blink blink!

How about a blinking LED on a breadboard? First, some numbers:

  • digital pins on Arduino Uno can supply up to 5 Volts; 40 milliamps (mA) per pin; 200mA in total from all pins being used
  • standard LED requires voltage of 2V and about 30mA (0.03A) of current

If I want to give my LED a long and blinking life I require a resistor in my breadboard circuit, and I figure out the minimum resistor needed using Ohm’s Law

RESISTANCE = VOLTAGE / CURRENT

However, there are two voltages to consider: the power supply voltage (VSUPPLY), and voltage to power the LED (VFORWARD). The modified calculation is …

R = (VSUPPLY - VFORWARD) / CURRENT = (5V -2V) / 0.03A = 100 ohms

Components:

  • 1 x Arduino Uno
  • 1 x Breadboard
  • 1 x LED
  • 1 x 220 ohm resistor
  • 2 x Jumper Wires

I wire up the circuit with the above components, then create and upload myBlink sketch to the Arduino …

// Variables
const int LED = 9;      // pin that the LED is attached

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED as an output.
  pinMode(LED, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(2000);                       // wait for two seconds
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(500);                       // wait for a half second
}

Some of the values and functions of the Arduino programming language used …

const int
Assign variable an integer value that cannot be changed
void
Function will return no value
pinMode
Configure pin to serve as input or output
digitalWrite
Write a HIGH or LOW value to the pin ... HIGH is 5V and LOW is 0V
delay
Pause program for amount of time measured in milliseconds

Fade

Pulse Width Modulation (PWM) is a method for getting analog effects using digital means. By varying the period of time during which an output is active - known as the duty cycle - with millisecond precision, the result is a perceived effect of a value somewhere between 0V and 5V. In the case of an LED, it generates a brightening and darkening effect. Using the analogWrite function, assign a value between 0-255 with higher values keeping the LED on for greater lengths of time and vice-versa.

PWM

Arduino’s digital pins with the ~ symbol - pins 3, 5, 6, 9, 10, 11 - can be used in PWM mode, and the IDE includes two example sketches that implement PWM using different methods.

Sketch File->Examples->01. Basics->Fade increments the duty cycle from 0-255 in 5 point chunks, using an if statement to test whether the cycle has reached either 0 or 255 and reversing the incremental change when it does.

I think the other example - found in File->Examples->03. Analog->Fading - performs the increment function in a more elegant manner using for statements. This method uses only one variable to set the LED pin, nothing is required in setup(), and the for loops have all the required initialization, condition, and incrementing values self-contained, making it easier to share this code in other sketches …

int ledPin = 9;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

Very easy to experiment with different increments and delays to achieve different fading effects. LEDs are cool.

I retrieved the PWM image and programming definitions above from the Arduino Language Reference. Great resource!

Happy hacking!