1

This is my followup question based on a serie of one project. Go my to profile to read 'm all.

So I'm building something which includes a function that has a count (1000) that decreases over time, but by pressing a soft pot meter the count increases. By every increment of 200 one LED is lit. So if the count is 837 4 LEDs are burning.

In my previous question I had issues lighting the LEDs, because they didn't burn even when the code was, as far as I could see correct. Instead it did the opposite of what I coded. Now I changed my code the opposite direction,

writing:

digitalWrite(LED1, LOW); // turn the LED on (LOW is the voltage level)

Which actually set the LED to bright. While is I'd write it 'HIGH' it would dim.

So my actual question is: How could my LED be lit if it's set to 'low', while you have to set it to 'high' to be actually popping off light?


My complete code:

int cleanCount = 1000;
int IsItWorkingLED = 13;
int softpotPin = A0; //analog pin 0
int LED1 = 1;
int LED2 = 2;
int LED3 = 4;
int LED4 = 5;
int LED5 = 7;



void setup () {
    Serial.begin(9600);
    digitalWrite(softpotPin, HIGH); //enable pullup resistor    
    pinMode(LED1, OUTPUT);  
    pinMode(LED2, OUTPUT);  
    pinMode(LED3, OUTPUT);  
    pinMode(LED4, OUTPUT);  
    pinMode(LED5, OUTPUT);

    digitalWrite(LED1, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED2, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED3, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED4, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED5, HIGH);   // turn the LED on (HIGH is the voltage level)  
}


void loop() {
    int softpotReading = analogRead(softpotPin);
    if (softpotReading < 980) { // IF TOUCHED
        digitalWrite(IsItWorkingLED, HIGH); 
        ++cleanCount;
        Serial.println(cleanCount);
        delay(8); // Increase 5 times as fast
    }
    else if (softpotReading > 980) { // IF NOT TOUCHED
        digitalWrite(IsItWorkingLED, LOW); 
        --cleanCount;
        Serial.println(cleanCount);
        delay(32); // Decrease slowly
    }
    else {      // If there is something wrong..
        Serial.println("Something wrong!");
        digitalWrite(IsItWorkingLED, HIGH);    
        delay(250);               
        digitalWrite(IsItWorkingLED, LOW);    
        delay(250);               
    }

        if (cleanCount <= 200) {
            Serial.println("Knal 1 LED aan");
            digitalWrite(LED1, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED2, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED3, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED4, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED5, HIGH);   // turn the LED on (LOW is the voltage level)
        }
        else if (cleanCount >= 201 && cleanCount <= 400) {
            Serial.println("Knal 2 LEDs aan");
            digitalWrite(LED1, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED2, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED3, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED4, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED5, HIGH);   // turn the LED on (LOW is the voltage level)
        }
        else if (cleanCount >= 401 && cleanCount <= 600) {
            Serial.println("Knal 3 LEDs aan");
            digitalWrite(LED1, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED2, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED3, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED4, HIGH);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED5, HIGH);   // turn the LED on (LOW is the voltage level)
        }
        else if (cleanCount >= 601 && cleanCount <= 800) {
            Serial.println("Knal 4 LEDs aan");
            digitalWrite(LED1, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED2, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED3, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED4, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(LED5, HIGH);   // turn the LED on (LOW is the voltage level)
        }
        else if (cleanCount >= 801 && cleanCount <= 1000) {
            Serial.println("Knal 5 LEDs aan");
            digitalWrite(1, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(2, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(4, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(5, LOW);   // turn the LED on (LOW is the voltage level)
            digitalWrite(7, LOW);   // turn the LED on (LOW is the voltage level)
        }
        else {
            Serial.println("Just..do nothing"); 
        }



}

3 Answers 3

2

Your assumption that a high is required to light a LED is false. All you need is to meet certain minimum voltage and current thresholds. Since the anode of the LED is tied high, the way to meet these is to bring the cathode low.

LED symbol

2
  • I don't got a clue what you actually mean with 'anode' and 'cathode' (beginner, as you might have guessed), but thus by setting it to 'low' it does shine. Ah okay. Thanks! Apr 8, 2015 at 8:18
  • 1
    @SanderSchaeffer: Added a link and annotated symbol to hopefully make it a bit clearer. Apr 8, 2015 at 8:21
0

When you command digitalWrite LOW you are basically connecting that pin to GND. If you command digitalWrite HIGH then you are connecting that pin to +5v. So depending on how you have the LED wired up you can use either to turn on the LED. Since LOW turns your LED on then you must have the long lead of the LED attached to +5v supply and the short lead to the pin on the arduino. I don't know why the LED doesn't turn all the way off when you command digitalWrite HIGH, but it should.

If you wanted to dim the leds then you should try using the analogWrite command which will send a PWM signal and will allow you to make the LEDs any intensity you want to.

0

An LED wants positive voltage to flow from the anode to the cathode (the + terminal to the - terminal)

You can wire it to a digital pin 2 different ways. (In both cases you'll also need a current limiting resistor to avoid burning out your LED and/or your digital pin, but ignore that for this discussion.)

You can wire the positive terminal of the LED to +5V, and the negative terminal to a digital pin. It sounds like this is what you've done. When you do that, if you set the digital output to HIGH, then both terminals of the LED are connected to +5V, so the LED will be off. Set the output to LOW, and it gets connected to ground. Now current flows out of the +5V rail, through the LED, into the grounded digital pin, and the LED lights up.

You can reverse that wiring instead. Connect the positive terminal of the LED to a digital pin and the negative side to ground. Now, when you set the pin to HIGH, +5V flows out of the pin, through the LED, and into the ground connection, so the LED lights. Set the pin to LOW and both sides of the LED are connected to ground, so it turns off.

Edit:

Note that you talk about voltage levels and dimming LEDs. If you're using digital outputs and HIGH or LOW, you can only turn the LED on or off. There's no way to vary its brightness.

You can, however, hook an LED to a PWM (Pulse Width Modulation) pin and set up that pin for analog output. In PWM, the Arduino switches the LED on and off rapidly. Set it to the highest output value, 255, and the LED stops flashing and is on 100% o the time. Set the output to 128, and the LED is on half the time and off half the time. Set the output to 64, and the LED is on 1/4 of the time and off 3/4 of the time, and so on.

When you flash a light fast enough, our eyes average out the brightness, and a light that's on 1/4 of the time looks 1/4 as bright as a light that's on 100% of the time.

Most Arduinos have a limited number of analog outputs. The Arduino model you're using might not have 7 analog outputs.

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