LzyPenguin
Posts: 4
Joined: Thu May 23, 2019 4:46 pm

Easiest way to control GPIO pin?

Thu May 23, 2019 4:52 pm

Hi,

I have a script that will always be running on my Pi, and under certain conditions it will turn on one of the GPIO pins for 30 seconds and then turn it back off.

I want to manually be able to turn on and off the GPIO pin while the other script is still running. Preferably with something on the desktop i can just double click and it either run, or open an interface where i can toggle a sequence that will turn it on for 30 seconds and then back off. Is this possible?

Bonus - How can i incorporate into my python script that i have manually turned on the GPIO pin, so that i can program my python script to not try and turn it on/back off while i am manually turning it on/off?

dl324
Posts: 118
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 5:17 pm


LzyPenguin
Posts: 4
Joined: Thu May 23, 2019 4:46 pm

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 7:37 pm

dl324 wrote:
Thu May 23, 2019 5:17 pm
https://elinux.org/RPi_GPIO_Code_Samples
I know how to interact with the GPIO pins already with python code. What i need is to somehow essentially make something clickable on the desktop that i can program to interact with the GPIO pins that will work ALONG with my python code currently running.

Andyroo
Posts: 3314
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 7:52 pm

Having two programs trying to access the pin at the same time is not a great idea (even if system locks let you do it - i’m tempted to try :lol: ).

I would look to run your current code with a simple GUI using something like TkInter toolkit and have a button on there to trigger the manual change.

You can then use a global Boolean variable to say if the pin can be changed manually or not.

An intro can be found at http://effbot.org/tkinterbook/ though there are plenty of videos on YouTube. A simple button example is at http://effbot.org/tkinterbook/button.htm

Your main issue maybe keeping your current code running but without knowing how that works I cannot help.
Need Pi spray - these things are breeding in my house...

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

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 8:12 pm

Andyroo wrote:
Thu May 23, 2019 7:52 pm
Having two programs trying to access the pin at the same time is not a great idea (even if system locks let you do it - i’m tempted to try :lol: ).
The system will allow it but it leads to some strange voltages on the gpio pin , helped another user that was having problems with strange voltages on gpio pins , it turned out he had 2 programs controlling the same gpio pins, so one program turned a pin off but the other turned it on , ended up with a sort of pwm output on the pin because it was being turned on and off by the 2 programs , the strange voltages were a result of the interaction of the 2 programs resulting in different duty cycles.

simple when you know what it is but took some head scratching to figure out , if I remember correctly he was trying to add manual control to an automated / timed program, ended up having to add the manual bit to his existing automatic / timed program.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

dl324
Posts: 118
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 8:14 pm

LzyPenguin wrote:
Thu May 23, 2019 7:37 pm
I know how to interact with the GPIO pins already with python code. What i need is to somehow essentially make something clickable on the desktop that i can program to interact with the GPIO pins that will work ALONG with my python code currently running.
So write a script and put it on your desktop.

LzyPenguin
Posts: 4
Joined: Thu May 23, 2019 4:46 pm

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 8:15 pm

Andyroo wrote:
Thu May 23, 2019 7:52 pm
Having two programs trying to access the pin at the same time is not a great idea (even if system locks let you do it - i’m tempted to try :lol: ).

I would look to run your current code with a simple GUI using something like TkInter toolkit and have a button on there to trigger the manual change.

You can then use a global Boolean variable to say if the pin can be changed manually or not.

An intro can be found at http://effbot.org/tkinterbook/ though there are plenty of videos on YouTube. A simple button example is at http://effbot.org/tkinterbook/button.htm

Your main issue maybe keeping your current code running but without knowing how that works I cannot help.
I see what your saying. Maybe having 2 things trying to interact with the same pin is going to be a problem. Is there a way I could essentially setup a button (or something I can click on) on the desktop of the pi, that will then communicate something to my python code running and trigger the program that already has control of the pin to make the changes?

Thanks

Andyroo
Posts: 3314
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 8:33 pm

You could use MQTT to pass messages between them or use sockets or even create a file in a known location and look for that.

All of these will require you to change your current program though...
Need Pi spray - these things are breeding in my house...

LzyPenguin
Posts: 4
Joined: Thu May 23, 2019 4:46 pm

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 10:12 pm

I think i might have a solution. I think i will use tkinter and just create a window with a button it it in my current code, and have the button is pressed it turns on then back off the GPIO pin. I think this would work.

Andyroo
Posts: 3314
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Easiest way to control GPIO pin?

Thu May 23, 2019 10:27 pm

Just make sure the callback function fir the button does not activate at the same time as the timer.

You could look at disabling the button during this time...

See my post above for TkInter links if you need them.

Good luck.
Need Pi spray - these things are breeding in my house...

Return to “Beginners”