B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

switching GPIO pins

Tue Apr 17, 2018 6:51 am

Hi, im looking to switch some gpio pins
i have found this code that is basically what i want.

Code: Select all

GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.OUT)


GPIO.output(5, HIGH) #switch on
time.sleep(5)        # wait 5 seconds
GPIO.output(5, LOW) #switch off

GPIO.cleanup()
is there any way i can make a command that runs this code?

i have an app on my phone that i can can run commands through ssh that i currently use to reboot or shut down my Pi's and was hoping to be able to name each code so i can type something like "sudo door1" and it runs the code to open door 1 rather than trying to input all that code into the phone app.?

also are there any pins i cant use? i know some are 5v and ground

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

Re: switching GPIO pins

Tue Apr 17, 2018 6:56 am

also are there any pins i cant use? i know some are 5v and ground


See https://pinout.xyz

Alternatively, type the command

Code: Select all

pinout
at a shell (terminal or command line) prompt.

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

Re: switching GPIO pins

Tue Apr 17, 2018 7:03 am

is there any way i can make a command that runs this code?
That appears to be written in the python programming language. But it is not complete. It is missing the import of the GPIO module.

Code: Select all

import RPi.GPIO as GPIO
as the first line in the file should fix that.

You will need to create a file, for example switcher.py, that has the content shown.

Then you can run the script by typing

Code: Select all

python3 switcher.py 

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

Re: switching GPIO pins

Tue Apr 17, 2018 7:18 am

If this is all new to you it might help to get a good grounding with a tutorial written by the Raspberry Pi Foundation, such as this one :

https://projects.raspberrypi.org/en/pro ... -computing

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: switching GPIO pins

Wed Apr 18, 2018 3:27 am

not quite what i need although may help

im running the Pi B+ as a kodi box with libreELEC
i have installed the RaspberryPi tools which looks like it has GPIO Zero
unsure if i need to do anything else for the gpio pins to operate

but i have no GUI for doing anything.

im looking to create the script from ssh and run it from a ssh command

and obviously need a little help in scripting

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: switching GPIO pins

Wed Apr 18, 2018 5:45 am

ok so i have my python script on the pi and running the script it seems to work. (multi meter)

i run the script by

Code: Select all

python door4.py
i want to add

Code: Select all

 echo "signal sent" 
but i get

Code: Select all

 SyntaxError: invalid syntax
i have found this

If you want to run any commands (like ls, cat, or echo), use bash (the normal shell), if you want to run Python code, use the Python shell (python2 or python3, depending on the language version you want).

but i dont understand how im meant to do it?

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

Re: switching GPIO pins

Wed Apr 18, 2018 7:09 am

i have installed the RaspberryPi tools which looks like it has GPIO Zero
unsure if i need to do anything else for the gpio pins to operate
You need to rewrite the script to take advantage of the gpiozero python library module. The sample you posted initially does not do that.

The documentation is here: https://gpiozero.readthedocs.io/en/stable/

For pretty much any problem involving a syntax error it will be necessary to post the failing code here.

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

Re: switching GPIO pins

Wed Apr 18, 2018 9:14 am

B-Man wrote:
Wed Apr 18, 2018 5:45 am
ok so i have my python script on the pi and running the script it seems to work. (multi meter)

i run the script by

Code: Select all

python door4.py
i want to add

Code: Select all

 echo "signal sent" 
but i get

Code: Select all

 SyntaxError: invalid syntax
So echo is a bash command which is what your would use on the command line, if you type that command on your command line it would print signal sent on the screen.

If you want to do the same in a python program you need to replace "echo" with "print"

so your command would become

Code: Select all

print "signal sent"
for python 2
or

Code: Select all

print ("signal sent")
for python 3
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: switching GPIO pins

Thu Apr 19, 2018 8:21 am

pcmanbob wrote:
Wed Apr 18, 2018 9:14 am

Code: Select all

print ("signal sent")
for python 3
thanks that worked
it means the script worked don't know how i can tell if the signal was actually sent apart from watching the door go up

one small issue dont know if its my pin choice or some sort of bug or wtf is going on.

When i connect the pi up to power one of my roller doors opens by itself. (GPIO pin6)
and then nothing else will work. i have to disconnect my common earth then when reconnected everything works fine again

Im using pin GPIO numbers 6,13,19,26

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

Re: switching GPIO pins

Thu Apr 19, 2018 10:00 am

Without knowing what's connected to your pi and how its connected its hard to say what the problem might be , but many relay boards you can get on line are active low and tend to operate while the pi is booting.

try posting some good quality pictures or a diagram of what's connected and how and links to any hardware you are using and don't forget to post your code as well.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: switching GPIO pins

Thu Apr 19, 2018 10:59 am

Thanks for all your help guys

I ended up finding this document https://www.raspberrypi.org/documentati ... herals.pdf
on page 102 it shows the pin function https://www.dropbox.com/s/839z2g4k9tgx9ho/pins.PNG
Pin 6 was reading high compared to the others low. and being the one that was causing trouble i figured it could be the pin starting up as high

i tried switching pins for 6 and 13 and the other door went up so put it to a totally different pin that reads low and don't have that trouble.

this is my project. its basically just a remote door opener circuit board with some transistors and resistors to automate the button press.
i have the battery in there still unsure if the pi could power it with the 3v or not.
its working so i figured not to mess with it.
https://www.dropbox.com/s/vl4j0aoh27rbl ... 182633.jpg

this is my code
I did add the print code and it works but have noticed sometimes the signal doesn't work even though it says it has. maybe when the device is in the shed the doors will work every time. and the plus side its on a kodi box so i can watch whatever i want in the shed

Code: Select all

print ("signal sent")

Code: Select all

import time
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')


import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.OUT)


GPIO.output(6, 1)	#switch on
time.sleep(1)		# wait 1 second
GPIO.output(6, 0)	#switch off

GPIO.cleanup() 
Last edited by B-Man on Thu Apr 19, 2018 11:15 am, edited 1 time in total.

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

Re: switching GPIO pins

Thu Apr 19, 2018 11:08 am

Well you code as posted does not contain the signal sent line and is still using gpio6 ?

But as the posted code is so short and contains next to nothing either you have a problem with the hardware side of your project or your switch on time is to short.

have you tried increasing the time you switch is on for ?
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

B-Man
Posts: 9
Joined: Tue Jan 24, 2017 10:58 am

Re: switching GPIO pins

Thu Apr 19, 2018 11:25 am

the device is off at the moment and the backup i had was when i first set it up and thought it was working.

its running on GPIO20 now and i removed the print "signal sent code" as even though it says it, sometimes it doesn't work.
my guess is the distance, the fact its going through a few walls and sometimes the remote doesn't always work all the time anyway.
the red light on the remote works so the script works and the "button" has been pressed.

the time its held is actually longer than what i would press the button for it to open so i don't believe that is the problem.

will hook it up in the shed when im ready and see how it goes.

Return to “Beginners”

Who is online

Users browsing this forum: quentin9909 and 24 guests