Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Pulsing LED

Wed Oct 30, 2019 9:59 pm

I found a glitch in the leak sensor I built. The valve I use to turn off the water incase of a leak has an open/close switch. To this switch I connected a red and a green LED. My thought was I would know things are running OK as long as the green LED was lit. Well we had a power outage and so the Pi shutdown without cleaning up the GPIO pins. Once the power came back the Pi booted and the green LED came on but my program was not running. Now my thought is to use PWM to pulse the LED. I am assuming if the Pi gets shutdown and restarted unexpectedly the PWM would not be working so the LED would just be on and not pulsing. Is that true?

I am also going to make a cron job so it launches the script on boot.

Now fo rate harder part. Ho who I keep the PWM looping while the rest of my program is running? The program monitors several GPIO pins to see if they are low or high every 5 minutes. If the condition changes it closes a valve and send me a text. Is there a clean way to run the loop pulsing the LED while the main program is sleeping? I guess I could run a loop pulsing the LED that would run for 5 minutes then exit and run the GPIO test. I was wondering if this would be a good case for threading? I have read some about it but don't yet have a good handle on making it work.

One last thing. If a leak is detected I poll the GPIO pin running the green LED to see if the valve is still open. If the PWM is running could that give a false reading on that GPIO pin?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pulsing LED

Wed Oct 30, 2019 10:24 pm

I do not understand how this is wired up. It sounds like the green and red LEDs are hardwired to a switch. If so I don't understand how a PWM pulse would make any change to the LED state.

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: Pulsing LED

Wed Oct 30, 2019 10:40 pm

There are three signal wires coming from the valve. The common is connected to the ground on the Pi. Then the hot side of the switches are connected to GPIO pins through an LED and resistor. So when the valve is open the ground is connected to the wire going to the green LED and its GPIO pin. The GPIO pin is pulled high so when the switch at the valve is closed the current flows from the GPIO pin to ground lighting the LED. Just think about it as an inline switch in a circuit with an LED. If the switch is closed PWM can pulse the LED. If the switch is open the LED is off and the PWM is not effectively doing anything.

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Pulsing LED

Thu Oct 31, 2019 12:11 am

Don’t wire the valve switch in the program loop
Keep it just to indicate valve open or closed
The valve will close with power failure , but will
Indicate position when power back on..
You will have to include the valve will only
Open if the program is running

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: Pulsing LED

Thu Oct 31, 2019 11:41 am

Opening and closing the valve have nothing to do with the signal lines. There are 2 more lines to the valve carrying 24V that are used to open and close the valve. Also the valve stays in its open or closed state no matter what happens to the power. It is a ball valve not a solenoid.

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Pulsing LED

Thu Oct 31, 2019 2:41 pm

Bope wrote:
Thu Oct 31, 2019 11:41 am
Opening and closing the valve have nothing to do with the signal lines. There are 2 more lines to the valve carrying 24V that are used to open and close the valve. Also the valve stays in its open or closed state no matter what happens to the power. It is a ball valve not a solenoid.
That is the problem is when you start building
Things you don’t understand. The time comes
When it develops a fault you can’t repair it
You should have said from the start it was
A Ball valve ,and not a electric power valve

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: Pulsing LED

Sun Nov 10, 2019 8:17 pm

So I finally got back to this project. I got the threading working so the LED will flash while the rest of the program does it's thing. What I ended up doing is just turning an output on for 0.75sec and then off for 0.5sec. That thread just keeps looping. The main program checks the status of 4 GPIO inputs then sleeps for 5 minutes. When I started the program the LED flashed in a consistent manner to how I programmed it. After several hours the flashing is anything but consistent. It kind of looks like someone is tapping out morse code. For this project it is no big deal, I was just looking for a flashing light to tell me the program is running. But I am curious why the timing would be so messed up.

User avatar
scruss
Posts: 3218
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Pulsing LED

Sun Nov 10, 2019 10:03 pm

gpiozero has pulse() as part of its PWMLED class. If this circuit is only closed by the external meter trigger, you could maybe set it going from the beginning and it would only pulse if needed.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Bope
Posts: 71
Joined: Sat Jul 06, 2019 2:57 am

Re: Pulsing LED

Mon Nov 11, 2019 10:59 pm

I looked at my pi this evening and the program had crashed. I had an error that looked like it couldn’t start another thread. It may have been trying to start a new thread flashing the led every 5 min. I moved the line and will see if the flashing stays consistent.

I also tried the pulse() from gpiozero. That is so much easier than writing my own definition to pulse the led and then start a thread to run that definition.

Return to “Beginners”