Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Raspberry Pi3 project using GPIO and LEDs

Thu Mar 21, 2019 9:33 pm

Hello and thank you

I have a project using the RPi3 and GPIO pins for a project..
I put together a flow chart, but not sure if I drew it the right way.
Any pointers/help appreciated.
See attached
Attachments
GHOLED.png
GHOLED.png (88.91 KiB) Viewed 1346 times

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

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 10:32 am

So your flow chart may not be drawn correctly but its understandable , so what exactly do you want help with ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 11:06 am

I have a project using the RPi3 and GPIO pins for a project..
I put together a flow chart, but not sure if I drew it the right way.
If this is simply your own personal project the flowchart or other design tool is simply an aid to help you understand the problem and its solution clearly. You can be the judge of that.

If this is for a marked or assessed assignment for work or a course of study then it will be important to use symbols that meet any relevant standards. Your use of the same shaped nodes for both decisions and actions does not look like any flowchart standard I have encountered. But if it meets your requirements my opinion and experience are irrelevant.

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 11:44 am

Thanks
Well, I've started and played around with gpiozero and some of the examples to levels of success, I can make LEDs do some stuff, but can't seem to get much farther than modifying the examples. I can make a LED steady or blink based on an input, but not change color and blink or steady. In the examples there's also some simple code to check for a connection to the web, but I would like to modify that to just check for a network connection since my project may be used on a lan without a web connection. I will probably make this into two separate programs, one for the LEDs and one for the gpio switching, for sake of personal ease. Usually I make discreet circuits for everything I need and would be simple, but this seemed simple enough to tackle with potentially just a few lines of code and would have some potential for changes and additions in the future.
Thank you

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

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 11:59 am

So to answer some of your points .

To make an LED change colour you need to use an LED that actually has 2 or 3 LED's built in to one package , you then switch each colour on or off to get the different colours.

example of using 3 colour LED https://www.instructables.com/id/Using- ... n-RGB-LED/

To make your check for a connection to the web code work using just a local network all you need to do is substitute the router address in place of the web address you were using to test to.

So you may have been using google.com for example so to make it just test for a local connection you would substitute the IP of the router which might be for example 192.168.1.1 or 192.168.0.1

if you share the code you are using than may be we can help with the changes you need.

please when posting code use code tags

Add [code] at the top

code goes here

and [/code] at the bottom.

Edit Oops posted wrong link corrected now thanks B.Goode for spotting my mistake
Last edited by pcmanbob on Fri Mar 22, 2019 2:00 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 12:25 pm

pcmanbob wrote:
Fri Mar 22, 2019 11:59 am
So to answer some of your points .

To make an LED change colour you need to use an LED that actually has 2 or 3 LED's built in to one package , you then switch each colour on or off to get the different colours.

example of using 3 colour LED https://www.hackster.io/windowsiot/rgb- ... ple-729ea7

Since you are presumably using the Raspberry Pi Foundation recommended and supported Raspbian Operating System it might be less confusing to continue with gpiozero rather than switching to an alternative Operating System.


To switch between one of several single-colour LEDs, see Section 2.9 here: https://gpiozero.readthedocs.io/en/stab ... l#ledboard

To control a 3-colour RGB LED see 2.16 here: https://gpiozero.readthedocs.io/en/stab ... -color-led

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 3:14 pm

Hi and thank you

Yes I am using a RGB led
I can make this do basic things, but I can't figure out how to in this case, turn the gled off, then the bled on.
I can make them pulse or blink or whatever, but not switch. I get a cyan color. I've taken some shots at "button.when_released = bled.on gled.off" to no avail.

In reference to the checking for a network connection, In my case, I won't be able to tell what the router address will be. The system (raspbian lite based) will be connected to unknown networks. I suppose there could be three states, one being 'blink while no network present' 'pulse while connected to a network' and 'steady when network has web access'. That would work.

Note: I am using in the below example, GPIO23 for my sensing input versus the GPIO22 in the above flow chart because I get errors using the below code with GPIO22. I have connected GPIO22 to GPIO23 to sense the signal if high or low. I would prefer to sense directly the state of GPIO22, however haven't discovered a method to do so without getting errors.

Thank You

Code: Select all

from gpiozero import PWMLED, Button
from signal import pause

rled = PWMLED(24)
gled = PWMLED(25)
bled = PWMLED(26)
button = Button(23, pull_up=False)

button.when_held = gled.on
button.when_released = bled.on

pause()

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 3:41 pm

It's great that you seem to understand the problem, and a possible solution, so let's see if I can help. Now I'm no expert on Python but the problem is that 'button.when_released = bled.on gled.on' is trying to call two functions, which can't be done on one line.

The simplest way around this is to create two new functions, one to do 'blue on, green off' and one to do the opposite. Eg:

Code: Select all

def blue_on():
    bled.on()
    gled.off()
    
def green_on():
    gled.on()
    bled.off()

Now those lines should read :

Code: Select all

button.when _held = green_on
button.when_released = blue_on
Hope that makes sense :-)

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 4:47 pm

Hey thanks

Making some headway. That code works, just one correction

button.when _held = green_on
button.when_held = green_on

I'll have to play around with it and see what I can do

What would I be looking for in adding the web status functionality?
Setting it up for monitoring the status of GPIO22 from GPIO23 seems simple enough now, although I would like to monitor GPIO22 directly, what would I do to add another layer of conditions for that and router/web checking? Should I stick to just pinging google?There are a number of ways of checking network connections, although I wouldn't know how to integrate it into this project.
I would imagine that I would set it up to check for the web first, then condition for the GPIO. Tips on how to combine the conditions of web presence with GPIO status?

Thanks again
S

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

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 6:18 pm

I know this is an old project page but the code it uses is still valid and is an example of how to integrate network checking with led outputs.

https://www.instructables.com/id/Raspbe ... t-Monitor/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Fri Mar 22, 2019 8:18 pm

Thanks Bob

That looks like the angle necessary for making it work, have the network start a process, or send a signal. I may just keep it simple first, just pinging Google or similar, until the rest is working with it. At this point, I need to discover how to integrate the LED indication with gpio changes AND web status. I think if I can get that sorted, then I may be able to infer most of the rest. Hopes

Thanks again, really appreciate the help here
S

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: Raspberry Pi3 project using GPIO and LEDs

Sun Mar 24, 2019 12:08 pm

Use PingServer and LED or PWMLED in GPIO Zero:

https://gpiozero.readthedocs.io/en/stab ... t.html#led
https://gpiozero.readthedocs.io/en/stab ... tml#pwmled
https://gpiozero.readthedocs.io/en/stab ... pingserver

Code: Select all

led.blink() # on / off every second
led.blink(2, 2) # on/off every 2 seconds

pwmled.pulse() # fade in/out every second
pwmled.pulse(2, 2) # fade in/out every 2 seconds

pingserver.is_active # True/False based on ping status
Example: when the internet is connected, the green led is on, when it's not, the red led pulses:

Code: Select all

from gpiozero import LED, PWMLED, PingServer
from time import sleep

red = PWLED(2)
green = PWMLED(3)

network = PingServer('google.com')

while True:
    if network.is_active:
        green.on()
        red.off()
    else:
        red.pulse()
        green.off()
     sleep(30)
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Sun Mar 24, 2019 4:07 pm

Thanks Ben

There were a few typos, but thank you
At the moment, I am looking at https://gpiozero.readthedocs.io/en/stab ... ng-sources for the ability to have both the functionality of a steady (web) or blinking (noweb) blue LED, if GPIO22 is not active, and a steady (web) or blinking (noweb) green LED, if GPIO22 is active. My red LED is reserved at the moment for something more catastrophic in nature. I suppose I could also shelve for the time being, web status indication, in favor of more important functionality, although, being able to tell if the web is present, headless, is an important indicator for everyday troubleshooting and confirmation. Usually I would wire up a circuit that takes care of everything, however, I would like to experiment with the ability to code this feature and be able to change it around with some simple lines in code if I don't like it or want to have more/better. It's probably going to take some time for me to read through the docs and play with the code enough to make something beta. It's common for me to do things in enough ways to become knowing before I call it done. I'm slowly getting to know and recognize how things go together in GPIO0. I like how you offer multiple ways of looking at the challenges.

S

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 2:35 pm

Hello again

Is there an equivalent to this function from RPi.GPIO
This is something that I would like to do and am wondering if I would be better off working with RPi.GPIO

Code: Select all

import RPi.GPIO as GPIO

channel = 11

# Setup your channel
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)

# To test the value of a pin use the .input method
channel_is_on = GPIO.input(channel)  # Returns 0 if OFF or 1 if ON

if channel_is_on:
    # Do something here

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

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 2:52 pm

Looking for an equivalent in gpiozero?

See 13.1.1 https://gpiozero.readthedocs.io/en/stab ... tml#button

Code: Select all

channel_is_on = button.is_pressed
seems to be similar?

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

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 3:12 pm

B.Goode wrote:
Mon Mar 25, 2019 2:52 pm
Looking for an equivalent in gpiozero?

See 13.1.1 https://gpiozero.readthedocs.io/en/stab ... tml#button

Code: Select all

channel_is_on = button.is_pressed
seems to be similar?
Just tried to set an output using LED in gpiozero and then read its state using Button set to the same pin to read the LED state, causes an error because the pin is already in use by LED when the Button set up is attempted.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 3:40 pm

pcmanbob wrote:
Mon Mar 25, 2019 3:12 pm


Just tried to set an output using LED in gpiozero and then read its state using Button set to the same pin to read the LED state, causes an error because the pin is already in use by LED when the Button set up is attempted.

See another recent thread by this user. Maybe you can be paid for your efforts.

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 4:34 pm

Simple wrote:
Mon Mar 25, 2019 2:35 pm
This is something that I would like to do and am wondering if I would be better off working with RPi.GPIO
Why?
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 4:36 pm

pcmanbob wrote:
Mon Mar 25, 2019 3:12 pm
Just tried to set an output using LED in gpiozero and then read its state using Button set to the same pin to read the LED state, causes an error because the pin is already in use by LED when the Button set up is attempted.
You're trying to use an LED object to light an LED, and then a Button object on the same pin to check whether the LED is on? Don't do that.

You can check if an LED is on using led.is_lit:

Code: Select all

led = LED(2)

led.blink()

while True:
    if led.is_lit:
        print("on")
    else:
        print("off")
    sleep(1)
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

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

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 5:00 pm

Ben

I don't know what the OP is going to use as the output , my test was simple to prove you could not do it in the same way as you would with RPi.GPIO were you set a pin as output but still read its state using a GPIO.input command.

you example looks to be what he wants if he uses LED , can this be used with other output drivers ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 6:14 pm

Hello and thank you again

What I have is a DAC that unmutes itself using a GPIO pin GPIO22, so that pin is reserved and functions via it's own driver, aside of anything here in this post. What I would like to to is read that pin internally if possible, rather than connecting another pin to it to read from (which I have done using GPIOzero). I had the same errors as above in attempting to use that technique. I don't know enough about these systems and various layers to know which method is the best. From reading around, I began to wonder if I need an more granular direction, as I assume that GPIOzero is more of a general layer. I dont know. And yes I am up for offering $ for assistance.
To reiterate..

System comes up then

If ever GPIO22 low and
No web then Blink bLED
Yes web then On bLED

If ever GPIO22 high and
No web then Blink gLED
Yes web then On gLED


My other part of the project (which I presume can be constructed as a separate process and program) is to also detect if GPIO22 is high, then run a short delay, say half second, then activate a relay (high out) on GPIO23, then disable the relay if GPIO22 is ever not high.

Simple
Posts: 32
Joined: Tue Aug 01, 2017 6:51 am
Location: Colorado USA
Contact: YouTube

Re: Raspberry Pi3 project using GPIO and LEDs

Mon Mar 25, 2019 7:26 pm

Additionally,

I like the when_held method over the when_pressed in that it eliminates the possibility of getting on the wrong side (opposite functioning) of an edge based method.

Return to “Python”