CharlyDelta
Posts: 119
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

Limit Switch

Mon Jun 17, 2019 1:41 pm

Hello

With this code, I can close a chicken coop door. I supervise the action with a camera. But, it would be very convenient to add a limit switch to stop the motor (which runs for 10 seconds) when the door reaches the threshold.

I don't know which gpio to plug it into and most important..., if this is the right way to do it?

Any idea how can I do that ?

Thank you for your help.


Code: Select all

import RPi.GPIO as gpio
import time

def init():
 gpio.setmode(gpio.BCM)
 gpio.setup(17, gpio.OUT)
 gpio.setup(22, gpio.OUT)
 
def reverse(sec):
 init()
 gpio.output(17, False)
 gpio.output(22, True)

 time.sleep(sec)
 gpio.cleanup()

print "reverse"
reverse(10)

User avatar
B.Goode
Posts: 7878
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Limit Switch

Mon Jun 17, 2019 1:52 pm

it would be very convenient to add a limit switch to stop the motor (which runs for 10 seconds) when the door reaches the threshold.

I don't know which gpio to plug it into


Recent models of RPi board have 26 gpio pins available on the 40 way header on the board.

Two are used by your existing code.

That leaves you with 24.

See this diagram. https://pinout.xyz/

It might minimise confusion to avoid the use of pins such as those marked Tx/Rx.

CharlyDelta
Posts: 119
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

Re: Limit Switch

Tue Jun 18, 2019 12:02 am

B.Goode wrote:
it would be very convenient to add a limit switch to stop the motor (which runs for 10 seconds) when the door reaches the threshold.

I don't know which gpio to plug it into


Recent models of RPi board have 26 gpio pins available on the 40 way header on the board.

Two are used by your existing code.

That leaves you with 24.

See this diagram. https://pinout.xyz/

It might minimise confusion to avoid the use of pins such as those marked Tx/Rx.

Thank you for the clarification. I understand it's not the pins that are missing. My problem is where to plug the wires and what to write as code so that when the limit switch is activated, the motor shuts down...
Any idea is welcome

PhatFil
Posts: 1249
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Limit Switch

Tue Jun 18, 2019 3:42 am

in the code you posted your using the BCM pin numbering.
the clue being:

Code: Select all

 gpio.setmode(gpio.BCM)
https://www.quora.com/What-is-BCM-and-w ... spberry-Pi

so pick a currently unused gpio for the limit switch pin40 aka GPIO21 for example

gpio.setup(21, gpio.IN)

a limit switch is basically a button so for the circuit etc..
https://www.cl.cam.ac.uk/projects/raspb ... _switches/

pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Limit Switch

Tue Jun 18, 2019 9:00 am

This is an example of using a switch to stop a motor when the switch state changes.

Code: Select all

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(21, gpio.IN, pull_up_down=GPIO.PUD_UP)
 
seconds = 10
print "reverse"
gpio.output(17, False)
gpio.output(22, True)
while seconds > 0:
    if gpio.input(21) == 0:     
        gpio.output(17, False)
        gpio.output(22, False)
        seconds = -1
    seconds = seconds - 1
    time.sleep(0.5)


gpio.cleanup()
un-tested code expect errors

you would wire the switch like this

Image

So we start with the switch input being pulled high/up.

we start the motor and then the while loop does the count down to zero but it also checks the switch on every loop execution ,
if the switch has closed and the input is now pulled down/low we stop the motor and set the count down to -1,
so that on the next execution of the loop the loop exits and we exit the program.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

CharlyDelta
Posts: 119
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

Re: Limit Switch

Wed Jun 19, 2019 5:33 pm

pcmanbob wrote: This is an example of using a switch to stop a motor when the switch state changes.

Code: Select all

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(21, gpio.IN, pull_up_down=GPIO.PUD_UP)
 
seconds = 10
print "reverse"
gpio.output(17, False)
gpio.output(22, True)
while seconds > 0:
    if gpio.input(21) == 0:     
        gpio.output(17, False)
        gpio.output(22, False)
        seconds = -1
    seconds = seconds - 1
    time.sleep(0.5)


gpio.cleanup()
un-tested code expect errors

you would wire the switch like this

Image

So we start with the switch input being pulled high/up.

we start the motor and then the while loop does the count down to zero but it also checks the switch on every loop execution ,
if the switch has closed and the input is now pulled down/low we stop the motor and set the count down to -1,
so that on the next execution of the loop the loop exits and we exit the program.
Wiring is ok.

I got this error:

Traceback (most recent call last):
File "fermer-switch.py", line 7, in <module>
gpio.setup(21, gpio.IN, pull_up_down=GPIO.PUD_UP)
NameError: name 'GPIO' is not defined

The motor is running but does'nt stop.

My knowledge is limited. I still need your help to continue.

User avatar
B.Goode
Posts: 7878
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Limit Switch

Wed Jun 19, 2019 5:41 pm

CharlyDelta wrote:
Wed Jun 19, 2019 5:33 pm
pcmanbob wrote: This is an example of using a switch to stop a motor when the switch state changes.

Code: Select all

import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(21, gpio.IN, pull_up_down=GPIO.PUD_UP)
 
seconds = 10
print "reverse"
gpio.output(17, False)
gpio.output(22, True)
while seconds > 0:
    if gpio.input(21) == 0:     
        gpio.output(17, False)
        gpio.output(22, False)
        seconds = -1
    seconds = seconds - 1
    time.sleep(0.5)


gpio.cleanup()
un-tested code expect errors

you would wire the switch like this

Image

So we start with the switch input being pulled high/up.

we start the motor and then the while loop does the count down to zero but it also checks the switch on every loop execution ,
if the switch has closed and the input is now pulled down/low we stop the motor and set the count down to -1,
so that on the next execution of the loop the loop exits and we exit the program.
Wiring is ok.

I got this error:

Traceback (most recent call last):
File "fermer-switch.py", line 7, in <module>
gpio.setup(21, gpio.IN, pull_up_down=GPIO.PUD_UP)
NameError: name 'GPIO' is not defined

The motor is running but does'nt stop.

My knowledge is limited. I still need your help to continue.


Challenge yourself. Think a bit instead of panicking and getting someone else to solve it.

You are working with the RPi gpio pins. You are trying to use a constant related to the status of the built-in Pull Up/Down.

Where could that constant be defined? Possibly in the RPi.GPIO library module?

And what name did you choose as an abbreviation for the name of that library when you imported it? Why hasn't anything reported an error until this line?

Hint: case is significant in most of the Linux Operating System and in programming languages such as Python...

pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Limit Switch

Wed Jun 19, 2019 5:46 pm

Error was caused by use of lower case gpio were as the line in question expected upper case GPIO.

upper and lower case variable names are different variable in a Linux environment.

revised code changed all gpio to GPIO which is my norm, and many example web sites norm.

Also added motor stop code when motor runs till time out.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
seconds = 10
print "reverse"
GPIO.output(17, False)
GPIO.output(22, True)
while seconds > 0:
    if GPIO.input(21) == 0:     
        GPIO.output(17, False)
        GPIO.output(22, False)
        seconds = -1
    seconds = seconds - 1
    time.sleep(0.5)

GPIO.output(17, False)
GPIO.output(22, False)

GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

CharlyDelta
Posts: 119
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

Re: Limit Switch

Wed Jun 19, 2019 6:03 pm

Thank you very much, Pcmanbob, everything is working now!

Return to “Automation, sensing and robotics”