Pico 2 Button control 2 Relay

Anyone please help. I am new to Python and Pico. I need to do a coding under Pico which I have 2 push button and 2 Relay. (Press Button 1 start Relay 1, Press Button 2 start Relay 2). i manage to get 1 button and 1 relay to work but can’t get the other Button and Relay Work. the code that i have is below:

from machine import Pin
import time

relay1 = Pin(28, Pin.OUT)
relay2 = Pin(27, Pin.OUT)

relay1.value(1)
relay2.value(0)

button1 = Pin(22, Pin.IN, Pin.PULL_UP)
button2 = Pin(26, Pin.IN, Pin.PULL_UP)

def GPIO():
if relay1.value():
relay1.value(0)
else:
relay1.value(1)

#def GPIO():

if relay2.value():

relay2.value(0)

else:

relay2.value(1)

try:
while True:
if not button1.value():
time.sleep_ms(20)
if not button1.value():
GPIO()
while not button1.value():
time.sleep_ms(20)

if button2.value():

time.sleep_ms(20)

if not button2.value():

GPIO()

while not button2.value():

time.sleep_ms(20)

except:
pass

Please format your code as explained here.

1 Like

Well, the code for button 2 and relay 2 is commented out, so it’s not surprising that it’s not responding!

1 Like