glennford49
Posts: 4
Joined: Sat Jun 01, 2019 10:23 am

2 python script using single gpio pin

Sat Jun 01, 2019 10:44 am

will it be possible to use 2 scripts using only single pin

#first.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
GPIO.input(8)


can i use the 2nd script to read the pin 8 state?
how to read the pin 8 state?
im a newbie on raspberry pi hoping someone that can give me idea on this.
thanks

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

Re: 2 python script using single gpio pin

Sat Jun 01, 2019 12:21 pm

Hi.

So you first program will be used for controlling the output pin.

Code: Select all

#first.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
Now in your second program you need to set the gpio pin in the same way, but with no GPIO.output, but now you can read its current state using a GPIO.input.

Code: Select all

#second.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
pin_state = GPIO.input(8)
your code was short and simple so it did not matter but please in future use code tags when posting code to preserve indentation.

Add [code] at the top

code goes here

and [/code] at the bottom.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

glennford49
Posts: 4
Joined: Sat Jun 01, 2019 10:23 am

Re: 2 python script using single gpio pin

Wed Jun 05, 2019 2:31 pm

Code: Select all

#second.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
pin_state = GPIO.input(8)
Python 3.5.3 (/usr/bin/python3)
>>> %Run first.py
>>> %Run second.py
second.py:6: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin,GPIO.OUT)
>>>

this is really my problem, thanks for your response

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

Re: 2 python script using single gpio pin

Wed Jun 05, 2019 3:43 pm

glennford49 wrote:
Wed Jun 05, 2019 2:31 pm

Code: Select all

#second.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
pin_state = GPIO.input(8)
Python 3.5.3 (/usr/bin/python3)
>>> %Run first.py
>>> %Run second.py
second.py:6: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(pin,GPIO.OUT)
>>>

this is really my problem, thanks for your response


Why is it a "problem"?

Surely your deliberate intention is that the activity started by the first script should be detected by the second? If the Warning (note, not an error) troubles you, the message tells you what you can do about it.

glennford49
Posts: 4
Joined: Sat Jun 01, 2019 10:23 am

Re: 2 python script using single gpio pin

Thu Jun 06, 2019 10:02 am

Code: Select all

#first.py
import RPi.GPIO as GPIO
import time
pin = 8
GPIO.setwarnings(False) # avoiding future warnings
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
GPIO.output(pin,GPIO.HIGH) # set as high again without GPIO.cleanup()
is it just fine/safe to use GPIO.setwarnings(False) to avoid future warnings just incase if i set the same pin as high without GPIO.cleanup()?
thanks for the quick response above, its helps me a lot

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

Re: 2 python script using single gpio pin

Thu Jun 06, 2019 10:09 am

You would normally only use GPIO.cleanup() at the end of your program as the last gpio command before exiting , it resets all gpio pin to the default state of input.

You can change the state of an output pin from high to low or low to high as many times as you wish within your program, if a pin is already set high then setting it high again has no affect , the same is try if a pin is set low and you set it low again.

what you can't do is have 2 programs attempting to set the same output pin high or low.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”