3

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

enter image description here

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

enter image description here

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

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:

enter image description here

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

enter image description here

(Note: What am I doing wrong!)

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

2 Answers 2

3

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. enter image description here

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.

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

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: 
int sensorReading = analogRead(analogPin); 
Serial.print("sensor read : ");
Serial.println(sensorReading);

// 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

voltage-follower

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):

12 volts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.