There are various ways to create a LED Meter or Bar Graph with a few square LEDs of three colours and demonstrate the function with a potentiometer. There is nothing new with this idea since official documentation of Arduino already has one example of bar graph. However, not all of them are practical to use with every project. The sensors in real often do not generate a voltage (or PWM signal) as a potentiometer does. Some projects have no proper license mentioned and that may create issues to re-use in other projects.
This project itself does not need complicated components. One breadboard, one potentiometer, a few jumper wires, any Arduino IDE compatible board including Arduino UNO, four red LEDs, four yellow LEDs and four green LEDs will do the job.
Twelve LEDs are used because we can use the same code Arduino UNO and ESP8266.
---
![Arduino 12 LED Meter Bar Graph With Potentiometer for Various Projects](https://thecustomizewindows.cachefly.net/wp-content/uploads/2023/06/Arduino-12-LED-Meter-Bar-Graph-With-Potentiometer-for-Various-Projects.png)
There are rectangular LEDs available in shops which sell electronic components. They are great to build something like a VU meter. Rest is the sketch used in this project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // Released under GNU GPL 3.0 // written by Abhishek Ghosh // see @ thecustomizewindows.com int pin[12] = {13,12,11,10,9,8,7,6,5,4,3,2}; int PotVal = 0; int ledPower = 0; int ledNumber= 0; void setup() { for(int i=0; i<12; i++) { pinMode(pin[i], OUTPUT); Serial.begin(9600); } } void loop() { PotVal = analogRead(A5); ledNumber = floor((PotVal+1) / 80); ledPower = ((PotVal+1)%80)-1; Serial.print(PotVal); for(int i=(sizeof(pin)-1); i>=0; i--) { Serial.println(i); if(i<=ledNumber){ analogWrite(pin[i], ledPower); ledPower = 255; } else { analogWrite(pin[i], 0); } delay(2); } } |
The meaning and logic of the sketch are probably obvious. The digit 80 came from trial and error (for the potentiometer). If you change it to a higher value such as 256 then the number of LEDs functioning will become lower in number. The ledPower
is 255 because we are handling PWM. That is nicely explained in this Arduino document:
Arduino’s PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
Probably you will ask the meaning of sizeof()
and floor()
. sizeof
is a commonly used operator in the C. It can compute the size of its operand. sizeof
can be applied to integer, floating-point, structure, union, etc.
In C++, floor()
and ceil()
return integer values. In other words, floor()
and ceil()
(ceiling) delivers the rounded-off value from a fraction. floor()
is lower and ceil()
is the upper. If 38.76 is the fraction, after applying the floor, it will become 38 and after applying the ceiling, it will become 39. In high school math, physics and chemistry, we use only the ceiling. Using floor()
and ceil()
requires some judgement. If the number of LEDs is more, using floor()
and ceil()
to display temperature as a bar graph will not make the result useless.
The code in this guide will produce the effect you can see by clicking the “Simulate” button.