HI Guys
Very new to messing about with Python and GPIO - so please go gentle on me. I'm primarily a mechanical guy and struggle with software and electronics especially how the Pi GPIO's work and the logic behind them... so here goes!
I'm not sure if this should be in Python or elsewhere so feel free to tell me to move it.
I'm currently modifying a 3d printer and moving over to a PI5. This is running bookworm and Klipper (moonraker etc) That aside I am looking to add some control to the printer mainboard. I need to delay it turning on due to some external drivers that must be powered first (from a seperate PSU). Sounds so simple.
I have 2 possible relays to do this. One is a 3v3 relay that is high to switch on (lets call it A). The other is a 5v relay that works at 3v3 with the only caveat that the VCC and signal need to be both at 3v3 but is active when pulled low (Lets call this B)
The first relay works fine on GPIO21 and VCC from the 3v3 rail. I have fudged a script together from Toms Hardware (uses GPIOD) and added the script to rc.local to get it to switch on on boot. ('scuse the naming it was originally for a mosfet board but it wouldnt work switching -ve due to the ground being passed to the printer board via USBC)
Script isThis is fine for this first relay (A). I specifically dont specify cleanup as i want the relay to stay on as long as the PI does. MFET_line.set_value(1) switches on and MFET_line.set_value(0) switches off - great
Now the second relay (B) works also using vcc from the 3v3 rail. But is reversed on GPIO21 (I believe low on boot) - so it the relay is active on boot (not desirable) therefore I read that some GPIO's have their default pull downs opposite. So by default are high on boot and then when activated are low. Trying it on GPIO5 which is high when booted and low when triggered, it is off while booting (desirable for this application). However this is where it gets wierd and bear with me - We are only taking about relay B now.
MFET_line.set_value(1) in both instances (GPIO21 and GPIO5) causes the relay to switch off - this says to me that regardless of the pull down default the MFET_line.set_value(1) tells the PI to go High on the GPIO? Is this correct? This kind of hurts my brain as I was expecting (1) to basically trigger the GPIO to the opposite of whatever its state is ie if high on boot - go low and vice versa - I assume this assumption has made an ass of me?! Some kind of confirmation that my thoughst are correct would be gratefully received.
OK now Part 2
I have read the above script now will not work with the latest update of bookworm due to changes in GPIOD (great - took me long enough to get this far) so I have tried to look at gpiozero as an alternative.
Surprisingly I actually managed to get a working script.. based on a relay on off script... Almost..
So this turns the relay on - and as it exits gets the output reset by gpiozero's automatic cleanup.. turning the relay back off ... NOT what i wanted and see no way around it with my limited (as in non existent) skill in this area.
I was looking at GPIOzero as a way of a) having some documentation I can barely understand, and b) the Toms hardware example becoming non functional didnt inspire me trying to dig around again for ways to get the relay to work again using GPIOD
Any suggestions of how I can acheive a permanent state for the relay using GPIOzero?
EDIT:- The end result is that I want this script running on startup, so that the relay activates on boot completion (seems to be around 10-14secs) so that then the printer board is switched on and all the electronics are happy..
All help gratefully received!
Steve
Very new to messing about with Python and GPIO - so please go gentle on me. I'm primarily a mechanical guy and struggle with software and electronics especially how the Pi GPIO's work and the logic behind them... so here goes!
I'm not sure if this should be in Python or elsewhere so feel free to tell me to move it.
I'm currently modifying a 3d printer and moving over to a PI5. This is running bookworm and Klipper (moonraker etc) That aside I am looking to add some control to the printer mainboard. I need to delay it turning on due to some external drivers that must be powered first (from a seperate PSU). Sounds so simple.
I have 2 possible relays to do this. One is a 3v3 relay that is high to switch on (lets call it A). The other is a 5v relay that works at 3v3 with the only caveat that the VCC and signal need to be both at 3v3 but is active when pulled low (Lets call this B)
The first relay works fine on GPIO21 and VCC from the 3v3 rail. I have fudged a script together from Toms Hardware (uses GPIOD) and added the script to rc.local to get it to switch on on boot. ('scuse the naming it was originally for a mosfet board but it wouldnt work switching -ve due to the ground being passed to the printer board via USBC)
Script is
Code:
import gpiodMFET_PIN = 21chip = gpiod.Chip('gpiochip4')MFET_line = chip.get_line(MFET_PIN)MFET_line.request(consumer="MFET", type=gpiod.LINE_REQ_DIR_OUT)MFET_line.set_value(1)MFET_line.release()exit()
Now the second relay (B) works also using vcc from the 3v3 rail. But is reversed on GPIO21 (I believe low on boot) - so it the relay is active on boot (not desirable) therefore I read that some GPIO's have their default pull downs opposite. So by default are high on boot and then when activated are low. Trying it on GPIO5 which is high when booted and low when triggered, it is off while booting (desirable for this application). However this is where it gets wierd and bear with me - We are only taking about relay B now.
MFET_line.set_value(1) in both instances (GPIO21 and GPIO5) causes the relay to switch off - this says to me that regardless of the pull down default the MFET_line.set_value(1) tells the PI to go High on the GPIO? Is this correct? This kind of hurts my brain as I was expecting (1) to basically trigger the GPIO to the opposite of whatever its state is ie if high on boot - go low and vice versa - I assume this assumption has made an ass of me?! Some kind of confirmation that my thoughst are correct would be gratefully received.
OK now Part 2
I have read the above script now will not work with the latest update of bookworm due to changes in GPIOD (great - took me long enough to get this far) so I have tried to look at gpiozero as an alternative.
Surprisingly I actually managed to get a working script.. based on a relay on off script... Almost..
Code:
import timeimport sysimport gpiozeroRELAY_PIN = 5relay = gpiozero.OutputDevice(RELAY_PIN, active_high=False, initial_value=True)relay.on()time.sleep(1)exit()
I was looking at GPIOzero as a way of a) having some documentation I can barely understand, and b) the Toms hardware example becoming non functional didnt inspire me trying to dig around again for ways to get the relay to work again using GPIOD
Any suggestions of how I can acheive a permanent state for the relay using GPIOzero?
EDIT:- The end result is that I want this script running on startup, so that the relay activates on boot completion (seems to be around 10-14secs) so that then the printer board is switched on and all the electronics are happy..
All help gratefully received!
Steve
Statistics: Posted by 5teve — Tue Apr 02, 2024 2:24 pm