# How to turn LEDs based on voltage readings?

I am trying to turn LEDs in an LED bridge based on the voltage reading from a power source. when the Voltage is zero nothing is ON and then the LEDs gradually turn ON as increase the voltage connected to the Ao pin. ( I am using a pro trinket 3V 120 MHZ)

This is my code:

``````const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 7; // the number of LEDs in the bar graph

int ledPins[] = { 3, 4, 5, 6, 8, 9, 10}; // 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() {
// 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);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
``````

However, I am not getting the outcome I want. when there is 0 voltage one LED turns ON instead of nothing. when there is a voltage all connected LEDs turn ON regardless of what the voltage is

when there is no voltage connected (floating wires), I get this:

when I put any amount of Voltage as long as the power supply is ON the results is the same:

Note: What am I doing wrong? edit: connections are something like that but with fewer digital pins.

Note: What am I doing wrong!EDIT: I was able to get around and print the serial monitor result:

• when the power source is OFF, the reading at A0 (sensorreading) is:

Note: What am I doing wrong!- when the power source is ON at any value, the reading at A0 (sensorreading) is:

(Note: What am I doing wrong!)

• Try printing sensorReading and ledLevel in your program and see if they are the values you expect. Commented Jul 14, 2023 at 15:49
• Could you please add a schematic showing how you connected the power source? Voltage divider, protection resistor, ground connection... Commented Jul 14, 2023 at 16:25
• @Delta_G I can not communicate with trinket pro as it does show as a port, but a USBISP Commented Jul 14, 2023 at 16:47
• is the Trinket your only Arduino?
– Juraj
Commented Jul 14, 2023 at 17:03
• Currently, yeah. Commented Jul 14, 2023 at 18:04

You are using a Full-size breadboard, for the full-size breadboards, the power rails are divided into two half sections as shown in this picture, and you need to have jumpers to bridge the two half-sections if you want to use them as a single power trail full-size section.

So on your photo, you thought that your blue wire (for GND) is connected to the rest of GND pins, but it is actually not connected.

• It makes sense. I will try it tomorrow or so. YOU ARE A GENIUS. #Observation Commented Jul 15, 2023 at 22:49

I can say your code is working but you need to check the wirering of your circuit .

Note that the voltage on the analogread pin should not be an oscillating voltage at low frequency like AC voltage. It should be a direct stable voltage to get the correct output.

Here is my diagram below for you to make changes with yours . Ive testedit on my simulation app and its working perfectly.

The code

``````
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 7; // the number of LEDs in the bar graph
int ledPins[] = { 3, 4, 5, 6, 8, 9, 10}; // an array of pin numbers to which LEDs are attached

void setup() {
Serial.begin(9600);
// 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:

// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
Serial.print("led Level : ");
Serial.println(ledLevel);

// 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);
}

// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
} }

//delay(500); //for debugging
}

``````

The diagram

Note that if you're tryingto measure a voltage higher than 5 volts, make sure to build the voltage divider circuit with the appropriate resistor values for you to get 5v output as that is the highest volt the arduino can carry. measure Example below shows a 12 volts dc battery voltage measure(note that 5 volts is the highest voltage supplied to the arduino, not the full 12 volts):