0

When I was testing out a program I found a strange behavior of ESP8266.

LEDs don't turn off completely in the first digitalWrite(led, LOW), just the brightness of the LED goes down, but it does turn off in the second digitalWrite(led, LOW).

I don't know if it's related to the code or the circuit, so here are both (the program is just two different visual patterns of LEDs toggled by a button)

#define led1 D0
#define led2 D1
#define led3 D2
#define led4 D3
#define button D7

unsigned long pressedButton;
bool change = 0;
unsigned long previousTime = millis();
unsigned int difference;


void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}


void loop(){
  if (!digitalRead(button) && millis()-pressedButton>200){       //check if button was pressed to change visual pattern of leds
      pressedButton = millis();
      change = !change;
      digitalWrite(led1, LOW);         //here led completely turn off
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);   
      digitalWrite(led4, LOW);
      previousTime = millis();
  }

  difference = millis()-previousTime;
  
  if (!change){                                     //default visual pattern of leds
    if (!digitalRead(led1) && difference>=200){
      digitalWrite(led1, HIGH);
    }
    if (!digitalRead(led2) && difference>=400){
      digitalWrite(led2, HIGH);
    }
    if (!digitalRead(led3) && difference>=600){
      digitalWrite(led3, HIGH);
    }
    if (!digitalRead(led4) && difference>=800){ 
      digitalWrite(led4, HIGH);
    }
    if (difference>=1000){
      digitalWrite(led4, LOW);           //the leds just shrink here
    }
    if (difference>=1200){
      digitalWrite(led3, LOW);
    }
    if (difference>=1400){
      digitalWrite(led2, LOW);
    }
    if (difference>=1600){
      digitalWrite(led1, LOW);
      previousTime = millis();
    }
  }
  
  if (change){                    //alternative pattern
     if (!digitalRead(led1) && difference>=200 && change){
       digitalWrite(led1, HIGH);
       digitalWrite(led4, HIGH);
       digitalWrite(led2, LOW);
       digitalWrite(led3, LOW);
    }
    if (digitalRead(led1) && difference>=400 && change){
       digitalWrite(led1, LOW);
       digitalWrite(led4, LOW);
       digitalWrite(led2, HIGH);
       digitalWrite(led3, HIGH);
       previousTime = millis();
    }
  }
}

Actually, it's a NodeMCU Esp8266, with the pins defined on the code and a 470 ohms resistor. (obs: resistor is not shorted, in my breadboard those rows are separated)

4
  • 6
    That resistor won't do anything with the conductor running underneath it in parallel.
    – timemage
    Commented Jul 13, 2023 at 20:33
  • 1
    Leds doesn't turn off completely ... how do you know this? ... maybe they are turning on and off very fast ... reduce your code to turn on led, delay 1 second, turn off led, delay 1 second, and repeat
    – jsotola
    Commented Jul 13, 2023 at 22:51
  • have you checked the connection between wires and tried a new led? Commented Jul 14, 2023 at 0:25
  • @timemage Sorry, I forgot to mention that in my breadboard the bottom row is separated for some reason
    – tatu101
    Commented Jul 14, 2023 at 18:19

1 Answer 1

2
if (!digitalRead(led3) && difference>=600){
  digitalWrite(led3, HIGH);
}
if (!digitalRead(led4) && difference>=800){ 
  digitalWrite(led4, HIGH);
}
if (difference>=1000){
  digitalWrite(led4, LOW);           //the leds just shrink here
}
if (difference>=1200){
  digitalWrite(led3, LOW);
}

Consider what will happen when the difference is >= 1000.

If the LED is off you turn it on (because it passes the first test, the difference is >= 600), and a moment later you turn it off. So the LEDs are being turned on and off in quick succession, which makes them look dim.

Try re-ordering the tests, and add else for each subsequent one. That seems to work for me:

  if (!change){                                     //default visual pattern of leds
    if (difference>=1600){
      digitalWrite(led1, LOW);
      previousTime = millis();
    }
    else if (difference>=1400){
      digitalWrite(led2, LOW);
    }
    else if (difference>=1200){
      digitalWrite(led3, LOW);
    }
    else if (difference>=1000){
      digitalWrite(led4, LOW);
    }
    else if (!digitalRead(led4) && difference>=800){ 
      digitalWrite(led4, HIGH);
    }
    else if (!digitalRead(led3) && difference>=600){
      digitalWrite(led3, HIGH);
    }
    else if (!digitalRead(led2) && difference>=400){
      digitalWrite(led2, HIGH);
    }
    else if (!digitalRead(led1) && difference>=200){
      digitalWrite(led1, HIGH);
    }
  }

Also, that resistor on the bottom row isn't achieving anything because all the pins on the bottom row are connected together so it is just shorted out. Not good for the LEDs or the processor.

1
  • Thank you! I didnt see that problem on code. And sorry, I forgot to mention that my breadboard have the bottom row separated so it's not shorted.
    – tatu101
    Commented Jul 14, 2023 at 18:22

Your Answer

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

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