led.off() turns it on and vise versa??

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
MicroNinja
Posts: 7
Joined: Sun Dec 29, 2019 8:38 am

led.off() turns it on and vise versa??

Post by MicroNinja » Fri Jan 31, 2020 8:14 pm

Why is the led turning off by the on() function? My Led (on board) is reacting the oposite to the function call.

led.on() and led.value(1) is actually turning it off whereas 0 or off turns it on

Code: Select all

from machine import Pin

LED_PIN = 2
LED_PIN, Pin.OUT) 

led.on()
Am I doing something wrong?

kevinkk525
Posts: 602
Joined: Sat Feb 03, 2018 7:02 pm

Re: led.off() turns it on and vise versa??

Post by kevinkk525 » Fri Jan 31, 2020 8:26 pm

Because the leds on those boards might be low active, therefore active when the pin is pulled to ground.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
emtee
Posts: 9
Joined: Thu Jun 14, 2018 4:55 pm
Location: BC, Canada

Re: led.off() turns it on and vise versa??

Post by emtee » Fri Jan 31, 2020 8:37 pm

kevinkk525 is most likely right in that the board you are using has an active low output for the onboard LED.
I am using a NodeMCU board with an active low LED. I was experiencing the LED going on when issuing a led.off() command and turning off when issuing a led.on() command. :?:

The trick is in the initial configuration. I am using v1.10 and it allows the following configuration:

Code: Select all

class BLINK():
    def __init__(self, pin_num=2):
        self._pin_num = pin_num
        # Connect to the LED pin
        # NOTE - the on board LED uses an inverted output
        self._led_pin = Pin(pin_num, Pin.OUT)
        self._led = Signal(self._led_pin, invert=True)
Note the invert=True in the Signal configuration. This should give the expected behaviour, i.e. LED turning on with led.on() and off with led.off(). :D

MicroNinja
Posts: 7
Joined: Sun Dec 29, 2019 8:38 am

Re: led.off() turns it on and vise versa??

Post by MicroNinja » Fri Jan 31, 2020 9:01 pm

Ah thank you guys that make sense now =)

I might be running an older version then. I got

Code: Select all

TypeError: extra keyword arguments given
But thats good enough, was just confused to why it happend!

Actually that was from the Led class which does not have invert argument

Code: Select all

object <class 'Pin'> is of type type
  init -- <function>
  value -- <function>
  off -- <function>
  on -- <function>
  irq -- <function>
  IN -- 0
  OUT -- 1
  OPEN_DRAIN -- 2
  PULL_UP -- 1
  IRQ_RISING -- 1
  IRQ_FALLING -- 2
>>>

bitninja
Posts: 119
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: led.off() turns it on and vise versa??

Post by bitninja » Mon Feb 10, 2020 3:09 am

Yeah this caught my attention a while back... so I had to look it up...

It's because they wire the LED so that the positive side is connected to power and the negative (drain) is connected to the output pin... so when it goes low (or to GROUND) the LED will light. It's because they can pass more current through the LED than if they wired the positive side to the output pin. The chip can only produce a small amount of current on it's outputs... apparently.

Didn't know about the Invert keyword though. Cool.

Post Reply