MinistryofSillyWalks
Posts: 5
Joined: Sun Jan 31, 2021 3:45 pm

GPIOs not reliably switching on our motor

Mon Aug 02, 2021 7:15 pm

We have a setup with a RPI 4B and a custom extension board with which we are controlling a motor for a tray.

The code for opening the tray is this:

Code: Select all

  import RPi.GPIO as GPIO
    from time import sleep

    GPIO.setmode(GPIO.BCM)
    Tray = [2,3]
    Close = [3]
    Open = [2]
    
    
    GPIO.setup(Tray, GPIO.OUT, initial=GPIO.HIGH)
    
    GPIO.output(Open, GPIO.HIGH)
    GPIO.output(Close, GPIO.LOW)
    
    GPIO.cleanup(Tray)
    sleep (5)

Now, we have the problem, that the tray/ motor gets reliably activated only in spyder but not reliably from the console.
Both spyder and the console use the same python version.

It seems to me, that it is a timing problem because when executing from terminal, it randomly works or not. The motor cycle (set with a time limit) starts in one direction when GPIO 3 is low and 2 is high and vice versa.

Any idea where the problem might lie or how we can improve this?

Many thanks

User avatar
joan
Posts: 16166
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIOs not reliably switching on our motor

Mon Aug 02, 2021 8:19 pm

You are changing the outputs to inputs with the call to cleanup(). Put the cleanup() after the sleep().

Return to “Troubleshooting”