input pin level is reversed somehow??

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
adouglas
Posts: 16
Joined: Fri Apr 30, 2021 4:46 pm

input pin level is reversed somehow??

Post by adouglas » Sat May 01, 2021 5:00 pm

I am wondering if I am doing something wrong, or there is a bug in the libraries for the pyboard.

I enter the following code:

Code: Select all

import pyb
Y9 = Pin('Y9', Pin.IN, Pin.PULL_DOWN)

while True:
    if (Y9.value()) == True:
        pyb.LED(4).on()
        print ("pin appears to be high")
    else:
        pyb.LED(4).off()
        print("pin appears to be low")
    pyb.delay(50)
what happens is that the led is on when the pin is unconnected or connected to ground, but it goes out, and prints the confirmation message, when the pin is *high*. It's backwards. What?? any ideas?

adouglas
Posts: 16
Joined: Fri Apr 30, 2021 4:46 pm

Re: input pin level is reversed somehow??

Post by adouglas » Sat May 01, 2021 5:38 pm

now I am seeing a similar issue with the external interupts. I enter this code:

Code: Select all

from pyb import Pin, ExtInt
import pyb

callback = lambda e: print("intr")
ext = ExtInt(Pin('Y9'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
while True:
    pyb.delay(50)
and the "intr" is printed when I touch the pin to ground, not to vcc. any ideas?

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: input pin level is reversed somehow??

Post by Roberthh » Sat May 01, 2021 6:14 pm

About the IRQ: Pin Y9 has an external pull-up resistor of 4.7kOhm. The internal pulls have a value of about 40k. So whatever pull you set, the input level is high, when you set it to input mode. Connecting it to Vcc does not change the level, and therefore the IRQ does not fire. If you connect Y9 to GND, the tip will bounce. So you have a fast pulse sequence of low/high transitions, which cause the IRQ to be triggered.

Edit: Tested it with a PYBV11. Works fine

Code: Select all

import pyb
Y9 = pyb.Pin('Y9', pyb.Pin.IN, pyb.Pin.PULL_DOWN)

while True:
    if (Y9.value()) == True:
        pyb.LED(4).on()
        print ("pin appears to be high")
    else:
        pyb.LED(4).off()
        print("pin appears to be low")
    pyb.delay(50)

adouglas
Posts: 16
Joined: Fri Apr 30, 2021 4:46 pm

Re: input pin level is reversed somehow??

Post by adouglas » Sun May 02, 2021 12:03 am

Thanks, Robert,, much appreciated.

I checked just now with the PULL_DOWN instead of PULL_NONE. It was dumb of me to use PULL_NONE. in the irq thing. I tried to change it, but I could not figure out how to clear the interrupt, and ran out of time. It gives an exception the second time the script is run because the interrupt is already specified.

Anyway, as you say it exhibits the same behaviour. This to me looks like a bug in the pyboard design. The pullups should be such that you can specify pullup or pull down? The behaviour of the system is not going as you would expect based on what is specified in the program. This sort of thing makes learning exponentially harder, and also in my application I simply have no way to actually get a pulldown situation, except building in external resistors. I don't know, but I would think pull down was common and a logical way of doing things.

I don't know where you got this knowledge from, but imo it is a serious inadequacy in the documentation. I could never have known that if you hadn't said so, unless I had read through and thought about the whole datasheet for the ARM chip.

As for using the pin to turn an led on and off, this works now, inexplicably. I cut and pasted my own code, and now it works as would be expected based on your explanation of the pullups/pull downs. But it still looks like a bug to me. I specified pull down, not pull up.

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: input pin level is reversed somehow??

Post by Roberthh » Sun May 02, 2021 7:20 am

The pins X9, X10, Y9 and Y10 have a physically wired pull-up resistor on the board supporting it's use for I2C. This pull-up cannot be overridden by the MCU internal PULL_DOWN, simply because the driving strength of the internal resistor is way lower. It will bring down the voltage at the pin only from 3.3V to 2.9V, which is still a logical high level. Other pins do not have that behavior, so using Y8 should give different result.
I do not know where the fact about the pull-up resistors is documented. I had seen it once, but could not find it again. At least it's in the schematics of the board (https://github.com/micropython/pyboard/ ... YBv10b.pdf), which is not the most obvious place to look at.

adouglas
Posts: 16
Joined: Fri Apr 30, 2021 4:46 pm

Re: input pin level is reversed somehow??

Post by adouglas » Mon May 10, 2021 1:18 am

I'm back to further explore pyboard, after exploring arduino, grass is always greener I guess. We'll see how it goes.

the following code does not work, the pin reads high when unconnected. I'll try it with other pins.

Code: Select all

import pyb
X5 = pyb.Pin('X5', pyb.Pin.IN, pyb.Pin.PULL_DOWN)

while True:
    if (X5.value()) == True:
        pyb.LED(4).on()
        print ("pin appears to be high")
    else:
        pyb.LED(4).off()
        print("pin appears to be low")
    pyb.delay(50)

adouglas
Posts: 16
Joined: Fri Apr 30, 2021 4:46 pm

Re: input pin level is reversed somehow??

Post by adouglas » Mon May 10, 2021 1:40 am

ok now it works. The other pins I checked also work, X1-4.

Post Reply