In this project we are going to create a graphical representation of the signal amplitude. The higher the amplitude the more number of LEDs will be turned ON.
We will be using 8 LED’s to show the intensity of the signal received. We will be reading the analog values from pin A0 and accordingly turning ON the LEDs. The higher the analog value more number of LEDs will be turned ON and lower the analog value lesser number of LEDs will be turned ON.
These LED bar graph indicators are basically used on Audio Mixers, Audio Amplifiers, Studio Decks etc.
Working Principle
The Analog Input will be mapped to the number of LEDs in the circuit. The code provided below will remain unchanged if 6 LED’s are connected. We just have to change the variable const int ledCount = 6.
Components Needed
- Arduino Uno
- Potentiometer 10K
- LED x 8
- 220Ω Resistor x 8
- Jumper cables
Circuit Diagram
Code
/*
LED bar graph
This Method can control as many number of LEDs you connect to the Arduino. Just change the number of pins
Connected to the Arduino.
*/
//constants do not change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 8; // the number of LEDs in the bar graph
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}else { // turn off all pins higher than the ledLevel:
digitalWrite(ledPins[thisLed], LOW);
}
}
}
The Code first declares the pins that are used in the connections. Next, we define the pins as input or output. The analog reading is taken from the analog pin and is mapped to the LED number. If the mapped values are met from ledLevel the LEDs will be turned on respectively from the array.
You will see the LED turn ON one by one when the value of analog reading increases and turn OFF one by one while the reading is decreasing.