Project tutorial
LED Display with Arduino ADC and PWM

LED Display with Arduino ADC and PWM

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

  • 1,101 views
  • 0 comments
  • 8 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino 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
Kingbright wp7113srd d image
5 mm LED: Red
×1
Led technology l03r5000q1 image
5 mm LED: Yellow
×1
Kingbright wp7113sgd
5 mm LED: Green
×1
Mfr 25fbf52 221r sml
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
Bourns 3362p 1 103lf image 160px
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
11026 02
Jumper wires (generic)
ANy style of breadboard jumper wires will work fine for connecting to the Arduino IO ports.
×1
12002 04
Breadboard (generic)
A small breadboard should be sufficient.
×1

Necessary tools and 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.

Apps and online services

About this project

This project is the first step towards a larger scale integration of analog signals with a colorful and interactive display. Specifically, I plan to display output voltages and frequencies from my electric guitar to an LED matrix. This is actually the first time I have worked with an Arduino (I know, I've been under a rock) and I immediately recognize why it is such a popular development board.

I was given an Arduino Uno to work with, and since I was new to this board I started with a little research on how to use it. As you may know, Arduino has its own IDE. Arduino's website contains an extensive reference section for functions, data types, and syntax in the Arduino IDE. I chose to look at the analogWrite() function and the sample code it provided turned out to be a very simple start to exactly what I was trying to do.

Getting started, the first step is generating an analog voltage that can be manually varied. For testing purposes, this is easiest with a potentiometer hooked up to the on board power supply and ground ports on the Uno. The output from the potentiometer is then sent to an analog input on the Uno; there's six to choose from. The greatest range is obtained using the 5V power supply, rather than the 3.3V supply, which will be discussed more later. Below, you can toggle between a view of the Arduino input ports and the external potentiometer connections.

The Arduino Uno has a 10-bit analog to digital converter, or ADC for short (ha...short). This means that for a range of up to 5 volts, the ADC will map the input voltage to an integer value up to 1024 (the largest value in a 10-bit binary number). Dividing 5V by 1024 bits tells us that the resolution of this ADC; an increase in 4.9mV increases the ADC mapping by a value of 1, plenty of sensitivity for our intents and purposes.

Now, send a wire from one of the digital pulse width modulation (PWM) outputs to your resistor/LED circuit. I sent three individual PWM outputs to three separate resistor/LED circuits to control different colored LEDs: green, yellow, and red. About 200-ohms resistance in series with the LED limits the current to the LED as a form of "protection" (remembering Ohm's Law, an increase in resistance while a voltage is held constant will drop the current). Below, you can toggle between a view of the Arduino output ports and a view of the LED circuit connections.

Moving to the code, beyond variable initializations and IO definitions, I expanded on the example I found in the analogWrite() documentation with a conditional if/else statement such that the analogWrite() function would write to a different PWM output depending on the captured ADC value. Lower voltages were sent to the green LED, higher voltages sent to the yellow LED, and the upper range of voltages set to the red LED. Basically, I wanted to mimic audio level indicators that respond dynamically to audio levels. Red corresponds to a clipping signal (too much coming through), yellow is a warning that the signal is close to clipping, and green is in the safe zone. However, the PWM output resolution is one-fourth that of the ADC, so every time I used the ADC captured value variable I always divided it by 4, essentially scaling the ADC value to conform to the PWM resolution. This is why the 5V supply worked better for me. When I used the 3.3V supply, the results were not as desirable. Try it for yourself and see how the circuit responds.

Uploading the code onto the Arduino, you can now turn the potentiometer to vary the input voltage. You will find that as you decrease the resistance, more voltage is sent into the ADC and the PWM outputs will change from the green to yellow to red LEDs, respectively. Below, you can toggle between views of each LED lit up from adjusting the potentiometer.

This was a fun introduction to coding with the Arduino but the tip of the iceberg for what is about to come. Stay tuned!

Code

Analog In PWM Out for three LEDs on Arduino UnoC/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
    }
}

Schematics

Breadboard Diagram
This an example view of how you might make connections to the Arduino using a breadboard.
Breadboard view u0zxhc4oin
Schematic
This is a schematic for the analog side of this circuit.
Schematic   full rt00cj1dbr

Comments

Similar projects you might like

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 17,377 views
  • 4 comments
  • 34 respects

Digital & Binary Clock In 8 Digits x 7 Segments LED Display

Project showcase by LAGSILVA

  • 1,827 views
  • 2 comments
  • 9 respects

16x16 LED Matrix Display

Project showcase by Treebug842

  • 3,122 views
  • 2 comments
  • 11 respects

LED Color Sequencer

Project tutorial by Team thefantasticode.Group

  • 5,140 views
  • 10 comments
  • 20 respects

LED Matrix Display Badge

Project tutorial by Team FabLabAQ

  • 2,117 views
  • 0 comments
  • 12 respects

Programming 4 Digit 7 Segment LED Display

by SAnwandter1

  • 94,852 views
  • 30 comments
  • 78 respects
Add projectSign up / Login