1

On an ESP32 board I am trying this simple code to control the onboard led.

But if I set it to HIGH, led is OFF, if I set it to LOW, led is ON (fully bright). What could cause this ?

The board has one red and one blue leds, the blue one is the one that can be controlled.

#define onboard_led 2

void setup() {
  pinMode(onboard_led, OUTPUT);
}

void loop() {
    digitalWrite(onboard_led, HIGH);
    // digitalWrite(onboard_led, LOW);
}

Edit: it was a board like this ("Geekcreit" I think):

enter image description here

6
  • 1
    To know for sure, you'd need to show the design of the board. But it's not uncommon for leds to be arranged in circuit such that to turn them on you sink current through the chip. As in, LED is wired something like 3.3V --- RESISTOR --- LED --- GPIO_PIN. When GPIO goes LOW (toward ground), your current flows and the LED turns on. It's also possible that the MCU's pin is connected to a transistor that inverts the logic in addition to offloading the current handling onto the transistor.
    – timemage
    Nov 11, 2020 at 4:59
  • 1
    There’s nothing wrong with your board. Your confusion is caused by how the onboard LED is wired as explained by @timemage. Many ESP boards have this arrangement for the onboard LED and you might also find other (external) hardware that has active-low controls. If you measure the I/O pin that is connected to the internal LED you will see that when it’s “HIGH” it’s at 3.3V (or close to it) just like it’s supposed to be.
    – StarCat
    Nov 11, 2020 at 8:25
  • 1
    I know it sound weird as a beginner, but it's not that unusual. Certain chips are better at sinking current (connecting to ground), than sourcing current (connecting to Vcc). A similar inverted logic is often used with buttons, where the input pin has a pull-up resistor. Resulting in reading a LOW when the button is pressed, and HIGH when not pressed. If this messes with your head a bit you could invert the value in code, which might make it more intuitive to read. Like digitalWrite(onboard_led, !HIGH);, where the HIGH indicates you want it to turn on, and the ! inverts it automagically.
    – Gerben
    Nov 11, 2020 at 13:47
  • OK, so I understand this is common. I don't have detailed schematics of the board, but I added a photo and name of board.
    – adrianTNT
    Nov 11, 2020 at 14:29
  • 1
    @adrianTNT, unfortunately there are a lot of boards /like/ that. To the extent that a picture would be helpful, it would need to be either a picture of your board or one that you are certain is identical. Otherwise it's just another variable.
    – timemage
    Nov 11, 2020 at 14:53

1 Answer 1

4

As this image from Okdo Page on LED Driving shows, there are two ways to drive an LED from a GPIO output pin:

https://www.okdo.com/project/gpio-led/?ok_ts=1605113937103

In the Active HIGH case, a HIGH output on the GPIO will turn the LED on since that will source current out of the pin and through the LED into ground.

In the Active LOW case, a LOW output on the GPIO will sink current from the +V supply into the pin.

In your case the LED is hooked up in the Active LOW configuration and so to turn it ON you must set the pin to LOW.

I'll add that these ESP32 boards are made by many different manufacturers and they are not all identical.

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.