Ian Etheridge
Published

LED Display with Arduino ADC and PWM

Light up three different LEDs using pulse width modulation corresponding to different ranges of analog input voltages.

BeginnerFull instructions provided1 hour17,955
LED Display with Arduino ADC and PWM

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Digilent - Analog Parts Kit
The proceeding components used in this project came from this kit. The discrete circuit components are listed below for your convenience if you wish to compile them without this parts kit.
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 221 ohm
Resistor 221 ohm
I used series 100-ohm and parallel 470-ohm resistor combinations to approximately acquire this resistance since this particular value is not included in the Analog Parts Kit. These resistors are used for current limiting the LEDs.
×3
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
Any style of potentiometer can be used to vary the input voltage. Anything from 5k-ohm to 50k-ohm should work as well.
×1
Jumper wires (generic)
Jumper wires (generic)
ANy style of breadboard jumper wires will work fine for connecting to the Arduino IO ports.
×1
Breadboard (generic)
Breadboard (generic)
A small breadboard should be sufficient.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

screwdriver
You may need a small flathead screwdriver to vary the resistance of your potentiometer, depending on the style of potentiometer you are using.

Story

Read more

Schematics

Breadboard Diagram

This an example view of how you might make connections to the Arduino using a breadboard.

Schematic

This is a schematic for the analog side of this circuit.

Code

Analog In PWM Out for three LEDs on Arduino Uno

C/C++
This code takes an analog input and sends it to one of three pulse width modulation outputs depending on the level of input voltage. External resistors (about 200-ohms for each output and a potentiometer for the input), LEDs (preferably different colors), jumper wires, and a breadboard are required for full operation.
int ledPinGreen = 9;                  // LED connected to digital pin 9
int ledPinYellow = 10;                // LED connected to digital pin 10
int ledPinRed = 11;                   // LED connected to digital pin 11
int monitorPin = 6;                   // used to scope the PWM output
int analogPin = A3;                   // potentiometer output connected to analog                                           pin 3
int val = 0;                          // variable to store the read value

void setup()                          //define inputs and outputs
{
  pinMode(analogPin, INPUT);          // sets the pin as input
  pinMode(ledPinGreen, OUTPUT);       // sets the pin as output
  pinMode(ledPinYellow, OUTPUT);      // sets the pin as output
  pinMode(ledPinRed, OUTPUT);         // sets the pin as output
}

void loop()                           // this is the code that "runs forever"
{
  val = analogRead(analogPin);        //read the input pin

  // analogRead ranges 0 to 1023
  // analogWrite ranges 0 to 255, thus the division by 4 of val
  
  if (val > 900)                      // high voltage - Red LED turns on
  {
    analogWrite(ledPinRed, val/4);    // write to Red LED
    analogWrite(ledPinYellow, 0);     // disable Yellow LED
    analogWrite(ledPinGreen, 0);      // disable Green LED
    analogWrite(monitorPin, val/4);   // used to scope the PWM output
    }
  else if (val > 700 && val <= 900)   // approaching high voltage - Yellow LED turns on
  {
    analogWrite(ledPinRed, 0);        // disable Red LED
    analogWrite(ledPinYellow, val/4); // write to Yellow LED
    analogWrite(ledPinGreen, 0);      // disable Green LED
    analogWrite(monitorPin, val/4);   // used to scope the PWM output
    }
  else if (val > 100 && val <= 700)   // low voltage - Green LED turns on
  {
    analogWrite(ledPinRed, 0);        // disable Red LED
    analogWrite(ledPinYellow, 0);     // disable Yellow LED
    analogWrite(ledPinGreen, val/4);  // write to Green LED
    analogWrite(monitorPin, val/4);   // used to scope the PWM output
    }
  else                                // little to no voltage input
  {
    analogWrite(ledPinRed, 0);        // disable Red LED
    analogWrite(ledPinYellow, 0);     // disable Yellow LED
    analogWrite(ledPinGreen, 0);      // disable Green LED
    analogWrite(monitorPin, val/4);   // used to scope the PWM output
    }
}

Credits

Ian Etheridge

Ian Etheridge

6 projects • 11 followers
I am an EE student at UW Bothell, an intern at Digilent Inc, a performing/recording musician, and a guitar/amplifier technician.

Comments