Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Arduino - LED Bar Graph

Previous Arduino example was on how to take input by reading an analog input pin and getting a serialized output

This is an example of working on Arduino will show you how to read an analog input at analog pin 0, by converting the values from analogRead() function into voltage, and printing it to the serialized monitor of the Arduino Software integrated development environment (IDE).

Components Required


You will need the following components

• 1 * Breadboard
• 1 * Arduino Uno R3
• 1 * 5k ohm variable resistor (potentiometer)
• 2 * Jumper Wires
• 8 * LED or some cases you can use Led Bar Graph display

Free picture downloaded from unsplash.com

Sketch


Open the Arduino IDE software on your PC or computer. Coding in the Arduino language will give you the power to gain control over your circuit. Open a new file for sketch by clicking New.

There are many uses of 10-segment bar graph LEDs. These things are easy for prototyping rather than a finished product with compact footsteps and straightforward hookup. Fundamentally, there are 10 individual blue LEDs group together, each with individual anode and cathode connectivity.

They are also available in yellow, red, and green colors.

Note: The pin out on those bar graphs may fluctuate from what is given in the datasheet.  By turning the device 180 degrees will correct the change, making the pin 11 the first pin in line.

Arduino Code


const int analogPin = A0;
const int ledCount = 8;

int ledPins[] = {1, 2, 3, 4, 5, 6, 7, 8};

void setup()
{
   for (int thisLed = 0; thisLed
  {
      pinMode(ledPins[thisLed], OUTPUT);
   }
}

void loop()
{
   int sensorReading = analogRead(analogPin);
   int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
   for (int thisLed = 0; thisLed
{
      if (thisLed
{
         digitalWrite(ledPins[thisLed], HIGH);
      }
        else
{
         digitalWrite(ledPins[thisLed], LOW);
      }
   }
}

Note to Code


You will get to know that the sketch works in the following way:  

In this case for ten LEDs, first read the input then you map the input value to the output range. Then you set up a for-loop to iterate over the outputs. If the output's number in the series is lower than the mapped input range, you turn it on. If not, you turn it off.

Result


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.




This post first appeared on SpiralComputing, please read the originial post: here

Share the post

Arduino - LED Bar Graph

×

Subscribe to Spiralcomputing

Get updates delivered right to your inbox!

Thank you for your subscription

×