Problem with PULL_DOWN internal function

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Problem with PULL_DOWN internal function

Post by zaord » Sun Oct 04, 2020 9:40 pm

Hi Here,

I have a problem in my code here :

Code: Select all

self.SW2 = machine.Pin('X12', machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # P1switch

     async def InvPress(self):
        await uasyncio.sleep_ms(1000)
        #print('Sw2',self.SW2.value())
        while True:
            #print(self.SW2.value())
            if self.SW2.value():
                if self.PressPos == 1:
                    self.stepper.steps(-1600,2600)
                    self.PressPos = not self.PressPos
                else :
                    self.stepper.steps(+1600,2600)
                    self.PressPos = not self.PressPos
            await uasyncio.sleep_ms(120)
I wire a switch beetween +3.3 and X12

When I connect a wire to the X12 pin, The board is becoming crazy and my the program detect High value also when the P1 user button is not pushed...

Any people could help me with it ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem with PULL_DOWN internal function

Post by pythoncoder » Mon Oct 05, 2020 7:49 am

I suggest you separate the issue of the pin and its pulldown from the behaviour of the program as a whole. To test the behaviour of the pin, at the REPL issue

Code: Select all

SW2 = machine.Pin('X12', machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # P1switch
Then periodically issue

Code: Select all

SW2()
as you operate the switch. On my Pyboard it works fine and is unaffected by the user button. If yours doesn't you may have a defective board: perhaps try another pin. Is it a genuine Pyboard or a clone?

If the pin and switch work fine, then there's a problem somewhere in your code.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Problem with PULL_DOWN internal function

Post by zaord » Mon Oct 05, 2020 8:11 am

Do you have any solution to acces the Pyboard REPL with external power supply connected + usb connexion ?

Could it be cuased by electromagnetic interactions with other wires ?

Because I check just the line as you said nad I never get errors ...


Here is my code :

Code: Select all

from step import Stepper
import utime
from bmp280 import *
from sfm3000 import SFM3000
import machine
from ssd1306_setup import WIDTH, HEIGHT, setup
from writer import Writer, CWriter
from nanogui import Label, Meter, refresh
import uasyncio

# Fonts
import courier20 as fixed
import font6 as small
import arial10
ssd = setup(False, True)  # Create a display instance
import sys


class TableAccordage():
    
    def __init__(self):
        #Acuators
        self.IN1 = machine.Pin('X1', machine.Pin.OUT)
        self.IN2 = machine.Pin('X2', machine.Pin.OUT)
        self.IN3 = machine.Pin('X3', machine.Pin.OUT)
        self.IN4 = machine.Pin('X4', machine.Pin.OUT)
        self.IN5 = machine.Pin('X5', machine.Pin.OUT)
        self.IN6 = machine.Pin('X6', machine.Pin.OUT)
        self.BLOW = machine.Pin('X4', machine.Pin.OUT) # limit switch
        self.VANNE = machine.Pin('X19', machine.Pin.OUT) # limit switch
        self.CLAMP = machine.Pin('X20', machine.Pin.OUT) # limit switch
        self.BLOW.high()
        self.PressPos = True
        self.VANNE.high() #Vanne ouverte
        self.SW1 = machine.Pin('X11', machine.Pin.IN) # limit switch
        self.SW2 = machine.Pin('X12', machine.Pin.IN, pull=machine.Pin.PULL_DOWN) # limit switch

        #print(self.SW2.value())
        self.stepper = Stepper(self.IN1, self.IN2, self.IN3)

        self.stepper.steps(-300,1800)

        while self.SW1.value() :
            self.stepper.steps(1,1800)

        self.PressPos = 1
        

        self.bus=machine.I2C(scl=machine.Pin('Y9'), sda=machine.Pin('Y10'))
        #sensors
        self.bmp = BMP280(self.bus)
        #tca.switch_channel(1)
        self.sfm3000 = SFM3000(self.bus)
        self.p0 = self.bmp.pressure
        uasyncio.run(self.main())
        self.InitP0()
        #print(self.sfm3000.Mes_flow())

        
        
     
        
    async def screen(self):
        
        Writer.set_textpos(ssd, 0, 0)  # In case previous tests have altered it
        wri = Writer(ssd, small, verbose=False)
        wri.set_clip(False, False, False)

        nfields = []
        dy = small.height() + 6
        y = 2
        col = 50
        width = wri.stringlen('990.99')
        for txt in ('Press:', 'Y:', 'Z:'):
            nfields.append(Label(wri, y, col, width, bdcolor=None))  # Draw border
            y += dy
        await uasyncio.sleep_ms(100)      
    
        while True :
            Label(wri, 2, 2, ' Press ', True)
            Label(wri, 22, 2, ' Flow   ', True)
            Label(wri, 42, 2, ' Temp ', True)
            i=0    
            for field in nfields:
                value = (self.bmp.pressure-self.p0,self.sfm3000.Mes_flow(),self.bmp.temperature)
                #print(value[i]) # print sensors values
                field.value('{:5.2f}'.format(value[i]))
                i=i+1
            refresh(ssd)
            await uasyncio.sleep_ms(100)
     
    async def InitP0(self):
        self.VANNE.low()#Vanne fermée
        self.BLOW.low()
        await uasyncio.sleep_ms(2000)
        self.p0= self.bmp.pressure
        self.BLOW.high()
        self.VANNE.high()#Vanne fermée
     
    async def InvPress(self):
        await uasyncio.sleep_ms(1000)
        #print('Sw2',self.SW2.value())
        while True:
            #print(self.SW2.value())
            if self.SW2.value():
                if self.PressPos == 1:
                    self.stepper.steps(-1800,2600)
                    self.PressPos = not self.PressPos
                else :
                    self.stepper.steps(+1800,2600)
                    self.PressPos = not self.PressPos
            await uasyncio.sleep_ms(120)

    def _handle_exception(self,loop, context):
        print('Global handler')
        sys.print_exception(context["exception"])
        #loop.stop()
        sys.exit()  # Drastic - loop.stop() does not work when used this way
    
    
    async def main(self):
        #self.loop = uasyncio.get_event_loop()
        #self.loop.set_exception_handler(self._handle_exception)
        uasyncio.create_task(self.InvPress())
        uasyncio.create_task(self.screen())       
        while True:
            await uasyncio.sleep(1000)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem with PULL_DOWN internal function

Post by pythoncoder » Mon Oct 05, 2020 8:28 am

It's a bit of a challenge to debug a long program especially when you don't have the hardware connected.

The reason I asked if you were using a clone board is because of this report which indicates a hardware problem related to the user button on a clone board. This sounds suspiciously similar to your issue. We really can't help with dodgy clone hardware.

Interfacing switches is not as simple as it seems owing to contact bounce. I suggest you look at my Swich class which addresses this issue.

If you can't access the REPL over USB I think there is something wrong with the hardware design of your system. I have built a wide assortment of projects with Pyboards and attached hardware. The only times USB REPL access is disabled is when using the stop and standby low power modes. If you interrupt a program you should always see a REPL.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Problem with PULL_DOWN internal function

Post by zaord » Mon Oct 05, 2020 8:32 am

Sorry, I was talking about the user button 1 for my hardware, not the user button of the pyboard ;)

I don't have this issue if I just make a one line code test as you said so It might come from hardware or code ;)

My problem is if I both connect the board to power supply and USB, the usb is making trouble with my computer : the pyboard is any more reconized and I think it is because some kind of flowing back current via usb, but I am not that clever in electronics wiring !.
I have the same GND for usb and board, but I am not sure this is electrically right ;)

Best

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem with PULL_DOWN internal function

Post by pythoncoder » Mon Oct 05, 2020 3:35 pm

There is a diode on the Pyboard V1.1 which protects the USB from an externally applied Vin. So if this is disrupting your computer I see these options:
  • It's a clone Pyboard lacking the diode.
  • Your real Pyboard is damaged.
  • There is something seriously wrong with your wiring.
The second option is very unlikely. If you got your Pyboard from a reputable source I would examine your wiring very carefully. All subsystems should share the same Gnd.
Peter Hinch
Index to my micropython libraries.

Post Reply