Arduino
/****************************************************************
*name:Relay Module
*function: you may hear ticktock.That's the normally closed contact opened the the normally open contact closed.
**********************************************************************/
/*********************************************************************/
const int relayPin=7; //the "s" of relay module attach to
/********************************************************************/
void setup()
{
pinMode(relayPin, OUTPUT); //Initialize relay as an output
}
/**************************************************/
void loop()
{
digitalWrite(relayPin, HIGH); //Close the relay
delay(1000); //wait for 1 second
digitalWrite(relayPin, LOW); //disconnect the relay\
delay(1000); //wait for 1 second
}
树莓派
C
#include <wiringPi.h>
#include <stdio.h>
#define RelayPin 0
int main(void)
{
if(wiringPiSetup() == -1){//when initialize wiring failded, print message to screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(RelayPin,OUTPUT);
while(1){
digitalWrite(RelayPin,LOW);
delay(1000);
digitalWrite(RelayPin,HIGH);
delay(1000);
}
return 0;
}
Python
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
RelayPin = 11 # pin11
def setup():
GPIO.setmode(GPIO.BOARD) #Numbers GPIOs by physical location
GPIO.setup(RelayPin, GPIO.OUT)
GPIO.output(RelayPin, GPIO.HIGH)
def loop():
while True:
print('relay on...')
GPIO.output(RelayPin, GPIO.LOW)
time.sleep(0.5)
print('relay off...')
GPIO.output(RelayPin, GPIO.HIGH)
time.sleep(0.5)
def destroy():
GPIO.output(RelayPin, GPIO.HIGH)
GPIO.cleanup() #release resource
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
网友评论