6
# Raspberry Pi + Grove Grove Relay
import time
import grovepi
# Connect the Grove Switch to digital port D3
# SIG,NC,VCC,GND
switch = 3
# Connect the Grove Relay to digital port D4
# SIG,NC,VCC,GND
relay = 4
grovepi.pinMode(switch,"INPUT")
grovepi.pinMode(relay,"OUTPUT")
while True:
try:
if grovepi.digitalRead(switch):
grovepi.digitalWrite(relay,1)
else:
grovepi.digitalWrite(relay,0)
time.sleep(.05)
except KeyboardInterrupt:
grovepi.digitalWrite(relay,0)
break
except IOError:
print "Error"
Run the program
Find the path to the file(According to your own path)
cd GrovePi/Software/Python/
Run Program
sudo python grove_switch_relay.py