您的位置:首页 > 其它

使用树莓派控制4路继电器

2016-11-11 14:49 585 查看




IND1 不是接在17号GPIO口上,而是GPIO.0口上,即为11号口上才是对的,其它网上的教程这方面都未详细说明 ,我也不知道为什么:)

可能 GPIO默认out的是 GPIO.0 号口。 

目前就这么多吧,接下来就是写自动化控制了。

加上简单控制 的python 代码

1:~~~~~~~~

  1 #!/user/bin/python

  2 # like this use: python gpiotest.py 1 or 0

  3 

  4 import sys

  5 import RPi.GPIO as GPIO

  6 GPIO.setwarnings(False)

  7 GPIO.setmode(GPIO.BCM)

  8 args = sys.argv

  9 pin = 17   # 这里的pin = 17 为 BCM 编码 的 17 ,树莓派的第11号口,wiringPi的0

 10 ct1 = args[1]

 11 

 12 if (int(ct1) == 1):

 13     GPIO.setup(pin, GPIO.OUT)

 14     GPIO.output(pin,GPIO.HIGH)

 15 

 16 if (int(ct1) == 0):

 17     GPIO.setup(pin, GPIO.OUT)

 18     GPIO.output(pin,GPIO.LOW)

  

代码2: 控制4路
##################################################

#           P17 ----> Relay_Ch1

# P18 ----> Relay_Ch2

# P27 ----> Relay_Ch3

# P22 ----> Relay_Ch4

##################################################

#!/usr/bin/python

# -*- coding:utf-8 -*-

import RPi.GPIO as GPIO

import time

Relay_Ch1 = 17 

Relay_Ch2 = 18

Relay_Ch3 = 27

Relay_Ch4 = 22

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(Relay_Ch1,GPIO.OUT)

GPIO.setup(Relay_Ch2,GPIO.OUT)

GPIO.setup(Relay_Ch3,GPIO.OUT)

GPIO.setup(Relay_Ch4,GPIO.OUT)

print("Setup The Relay Module is [success]")

try:
while True:
#Control the Channel 1
GPIO.output(Relay_Ch1,GPIO.LOW)
print("Channel 1:The Common Contact is access to the Normal Open Contact!")
time.sleep(0.5)

GPIO.output(Relay_Ch1,GPIO.HIGH)
print("Channel 1:The Common Contact is access to the Normal Closed Contact!\n")
time.sleep(0.5)

#Control the Channel 2
GPIO.output(Relay_Ch2,GPIO.LOW)
print("Channel 2:The Common Contact is access to the Normal Open Contact!")
time.sleep(0.5)

GPIO.output(Relay_Ch2,GPIO.HIGH)
print("Channel 2:The Common Contact is access to the Normal Closed Contact!\n")
time.sleep(0.5)

#Control the Channel 3
GPIO.output(Relay_Ch3,GPIO.LOW)
print("Channel 3:The Common Contact is access to the Normal Open Contact!")
time.sleep(0.5)

GPIO.output(Relay_Ch3,GPIO.HIGH)
print("Channel 3:The Common Contact is access to the Normal Closed Contact!\n")
time.sleep(0.5)

#Control the Channel 4
GPIO.output(Relay_Ch4,GPIO.LOW)
print("Channel 4:The Common Contact is access to the Normal Open Contact!")
time.sleep(0.5)

GPIO.output(Relay_Ch4,GPIO.HIGH)
print("Channel 4:The Common Contact is access to the Normal Closed Contact!\n")
time.sleep(0.5)

except:
print("except")
GPIO.cleanup()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: