Cannot get a Pin to work with a microswitch

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Cannot get a Pin to work with a microswitch

Post by maraujop » Thu Mar 05, 2015 7:07 am

Hi everyone,

I'm doing some experiments with my brand new Pyboard 1.0 (coolest thing in many years). I started playing with the built-in switches and now I've connected a tactile microswitch to Y11 and V3.3 on each side of the jumper. I'm using the following code, which I believe is correct. However the blue led turns on right after booting and stays this way no matter what I do with the switch.

Code: Select all

import time,pyb


switch = pyb.Pin("Y11", pyb.Pin.IN)
switch.init(pyb.Pin.IN, pull=pyb.Pin.PULL_DOWN)


blue_led = pyb.LED(4)

while True:
    if switch.value():
        blue_led.on()
    else:
        blue_led.off()

    time.sleep(0.05)
It's my undertanding that if I do:

Code: Select all

>>> pyb.Pin.board.Y1.low()
>>> pyb.Pin.board.Y1.value()
1
It should be reading 0?

And that if I set any other pin, without nothing connected to it to a pull down, it should be reading 0 unless I draw power to it. Just in case i upgraded firmware this morning to Micro Python v1.3.10-147-g16b1f5e on 2015-03-05; PYBv1.0 with STM32F405RG.

Thanks, cheers
Miguel

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Cannot get a Pin to work with a microswitch

Post by Damien » Thu Mar 05, 2015 9:08 am

The code you have written should be working (it works for me). Did you try a different pin other than Y11?

Note that you can init the pin pull mode in the constructor:

Code: Select all

switch = pyb.Pin("Y11", pyb.Pin.IN, pyb.Pin.PULL_DOWN)

maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Re: Cannot get a Pin to work with a microswitch

Post by maraujop » Thu Mar 05, 2015 11:58 am

Thanks Damien,

First, let me say I admire micropython.

I tried with other pins too, second example was using pin Y1, which doesn't have anything connected. Also I've tried to initialize it as a pull down and read value and it says 1 always, however there is nothing connected to it.

I'm powering the board using USB. However I have an Adafruit micro lipo charger connected to Vin and GND, not sure if that could cause trouble. The battery is unplugged.

I'm not sure what this could be, because playing with the REPL I got the same results while the board was connected. I hope not to have damaged anything while soldering (just a beginner), but board is readable by the computer.

Thanks, cheers
Miguel

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Cannot get a Pin to work with a microswitch

Post by dhylands » Thu Mar 05, 2015 5:04 pm

I tried your code, and it works perfectly for me, exactly as coded.

I created a file called switch.py and copied it to the internal flash and then did: import switch to test.

I just used wire since I didn't have a switch handy. Are you sure your switch is working properly?

https://www.dropbox.com/s/ln45rds2nm5zf ... 2.jpg?dl=0

maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Re: Cannot get a Pin to work with a microswitch

Post by maraujop » Thu Mar 05, 2015 7:10 pm

Thanks for trying dhylands, If it works then my board must be broken. I might have shortcircuit something or broken a trace. Because I checked the switch and works ok.

Code: Select all

>>> pyb.Pin.board.X6.value()
0
>>> x6 = pyb.Pin('X6', pyb.Pin.IN, pyb.Pin.PULL_UP)
>>> x6.value()
1
I tried this and worked. I used a clip for connecting V3.3 with X6 and value was reading ok. I'm seeing my micropython when booting has a red light on longer than before and my REPL fails to connect, but a CTRL^C soft reboots it. Might have to get a new one.

Thanks for your time

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Cannot get a Pin to work with a microswitch

Post by Damien » Thu Mar 05, 2015 11:20 pm

Try this code to scan all pins:

Code: Select all

import pyb

def test_pin(pin):
    pin.init(pin.IN, pin.PULL_UP)
    pyb.delay(100)
    print(pin, pin.value())
    pin.init(pin.IN, pin.PULL_DOWN)
    pyb.delay(100)
    print(pin, pin.value())

for i in range(12):
    test_pin(pyb.Pin('X%d' % (i + 1)))

for i in range(12):
    test_pin(pyb.Pin('Y%d' % (i + 1)))
On a fresh board it gives 1/0 for pull up/down on all pins except the I2C pins, for which it gives 1 also for pull down (since these i2c pins are always pulled high by a 4k7 resistor).

I have a board which has a broken X1 pin which always reads 1. I think it broke due to an excessive voltage. But otherwise that board works fine, because the MCU is pretty robust.

maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Re: Cannot get a Pin to work with a microswitch

Post by maraujop » Tue Mar 10, 2015 5:48 am

Thanks Damien, this is of much help. I have launched this in my pyboard right now and this is what I'm getting:

Code: Select all

Pin(Pin.cpu.A0, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A0, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A1, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A1, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A2, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A2, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A3, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A3, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A4, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A4, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A5, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A5, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A6, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A6, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.A7, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.A7, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B6, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B6, mode=Pin.IN, pull=Pin.PULL_DOWN) 1
Pin(Pin.cpu.B7, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B7, mode=Pin.IN, pull=Pin.PULL_DOWN) 1
Pin(Pin.cpu.C4, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.C4, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.C5, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.C5, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.C6, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.C6, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.C7, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.C7, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B8, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B8, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B9, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B9, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B12, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B12, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B13, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B13, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B14, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B14, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B15, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B15, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Pin(Pin.cpu.B10, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B10, mode=Pin.IN, pull=Pin.PULL_DOWN) 1
Pin(Pin.cpu.B11, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B11, mode=Pin.IN, pull=Pin.PULL_DOWN) 1
Pin(Pin.cpu.B0, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B0, mode=Pin.IN, pull=Pin.PULL_DOWN) 1
Pin(Pin.cpu.B1, mode=Pin.IN, pull=Pin.PULL_UP) 1
Pin(Pin.cpu.B1, mode=Pin.IN, pull=Pin.PULL_DOWN) 0
Funny fact is that Y11 (B0) is misreading, so I'm guessing the pin is busted. Then B10, B11, B6 and B7 are reading all high values, as you warned me. Ok, so now I'm back to checking this tactile switch or other possible things. Thanks a lot for this help, I can submit a PR to docs, if you want me to, adding this troubleshooting, I think others could find it helpful in the future.

maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Re: Cannot get a Pin to work with a microswitch

Post by maraujop » Tue Mar 10, 2015 6:21 am

Well, it turns that dhylands was right in the beginning the problem was the microswitch :( Sorry I didn't see it at first, but it turns to be partially working and it tricked me when I measured it with a multimeter. I'm too of a newbie in the hardware part, too long since I last did hardware fiddling.

Also this snippet was very confusing to me:

Code: Select all

>>> y1 = pyb.Pin.board.Y1
>>> y1.low()
>>> y1.value()
1
The other day I tried other pins, but I must have done it the wrong way, making me think the board was gone.

Anyway, thank you both for your time and guidance.
Miguel

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Cannot get a Pin to work with a microswitch

Post by Damien » Tue Mar 10, 2015 11:51 pm

maraujop wrote: Also this snippet was very confusing to me:

Code: Select all

>>> y1 = pyb.Pin.board.Y1
>>> y1.low()
>>> y1.value()
1
Where did you get this snippet from?

maraujop
Posts: 9
Joined: Thu Mar 05, 2015 6:54 am

Re: Cannot get a Pin to work with a microswitch

Post by maraujop » Thu Mar 12, 2015 5:34 am

Some website in the internet, however I don't have the link at hand, I've tried to find it again but nothing.

Post Reply