Raspberry Pi and the two-color Led module


1. Introduction

  In order to realize the subsequent Raspberry Pi receiving the Hall sensor signal and then realize the light, it is a preparatory work to realize, it is strongly recommended that you buy components in the future and try to buy double copies, because sometimes the components sent by the shop are broken, and it is not necessarily It will take another day or two to be able to correctly provide the corresponding information and reissue it again. Therefore, if the funds are sufficient, it is strongly recommended to buy double copies directly.

2. Hardware preparation

1. Raspberry Pi 4B * 1

2. KY-011 two-color LED module * 2

   The picture below shows the broken ky-011 that I bought. The ky-011 is divided into red and yellow. The broken red light is not on, and the yellow light can still be used normally.
Ky-011

Wiring pin

  • (Left in the figure) The pin marked with'-' is connected to GND, the middle pin is connected to digital IO, and the pin marked with "S" (on the right in the figure) is also connected to digital IO
  • When the middle pin is high, the LED lights up red (R).
  • When the S pin is high, the LED lights up yellow (Y).
Raspberry Pi KY-011
Pin 6 GND
Pin 11 AND
Pin 13 R

Three, software preparation

# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)
# 输出模式
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)


while True:
	//熄灭
    GPIO.output(11, GPIO.LOW)
    GPIO.output(13, GPIO.LOW)
    time.sleep(1)
    //黄灯亮
    GPIO.output(11, GPIO.HIGH)
    GPIO.output(13, GPIO.LOW)
    time.sleep(1)
    //红灯亮
    GPIO.output(11, GPIO.LOW)
    GPIO.output(13, GPIO.HIGH)
    time.sleep(1)
	//红灯亮 + 黄灯亮 (形成混合色)
    GPIO.output(11, GPIO.HIGH)
    GPIO.output(13, GPIO.HIGH)
    
    
GPIO.cleanup()

Experimental results:

Alternating yellow and red, self-brain supplement
Insert picture description here

Four, prepare knowledge

1. Common two-color Led module

  Common two-color led modules are generally of this kind, and most of the information on the Internet is also of this kind, with red and green dual colors, grounded in the middle.
Chuanglebo two-color Led

Schematic diagram
Cooperate with the switch wiring to realize the switch light experiment.
Insert picture description here

#!/usr/bin/env python
import RPi.GPIO as GPIO

BtnPin = 11
Gpin   = 12
Rpin   = 13

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
	GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output
	GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Set BtnPin's mode is input, and pull up to high level(3.3V)
	GPIO.add_event_detect(BtnPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x):
	if x == 0:
		GPIO.output(Rpin, 1)
		GPIO.output(Gpin, 0)
	if x == 1:
		GPIO.output(Rpin, 0)
		GPIO.output(Gpin, 1)

def Print(x):
	if x == 0:
		print '    ***********************'
		print '    *   Button Pressed!   *'
		print '    ***********************'

def detect(chn):
	Led(GPIO.input(BtnPin))
	Print(GPIO.input(BtnPin))

def loop():
	while True:
		pass

def destroy():
	GPIO.output(Gpin, GPIO.HIGH)       # Green led off
	GPIO.output(Rpin, GPIO.HIGH)       # Red led off
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()


2. Basic knowledge of output (Output) using RPi.GPIO module

1. First set RPi.GPIO (according to the description here)

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

2. Set a certain output pin status to high level:

GPIO.output(12, GPIO.HIGH)
 # 或者
GPIO.output(12, 1)
 # 或者
GPIO.output(12, True)

3. Set the state of a certain output pin to low level:

GPIO.output(12, GPIO.LOW)
 # 或者
GPIO.output(12, 0)
 # 或者
GPIO.output(12, False)

4. Clean up after the program ends

GPIO.cleanup()

Note that you can read the current state of the output channel set using the input() function. For example, to switch the output:

GPIO.output(12, not GPIO.input(12))

3. A collection of various GPIO operations

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM) #选择GPIO编码格式 BCM
channel = 1
# 1. 设置GPIO模式
GPIO.setup(channel,GPIO.IN)  #输入模式
GPIO.setup(channel,GPIO.OUT) #输出模式
GPIO.setup(channel,GPIO.OUT,initial=GPIO.HIGH) #为输出的引脚设置默认值

# 2. 设置GPIO输出状态
chan_list=[11,12]           #双倍同时输出快乐
GPIO.output(chan_list,GPIO.LOW)             #同时输出低电平
GPIO.output(chan_list,(GPIO.HIGH,GPIO.LOW)) #分别输出不同电平
GPIO.output(12,not GPIO.input(12))          #读取一个输出引脚的状态并将其作为输出值

# 3. 读取GPIO当前状态
GPIO.input(channel)        #低电平返回0/GPIO.LOW/False,高电平返回1/GPIO.HIGH/True。
GPIO.setup(channel,GPIO.IN,pull_up_down = GPIO.PUD_UP)  #如果输入引脚处于悬空状态  上拉解决
GPIO.setup(channel,GPIO.IN,pull_up_down = GPIO.PUD_DOWN)#如果输入引脚处于悬空状态  下拉解决

# 4. 轮询读取Gpio
while GPIO.input(channel)==GPIO.LOW:
    time.sleep(0.01)

# 5. 边缘阻塞检测Gpio(会阻塞直到检测到边缘响应,轮询)
# wait_for_edge()被用于阻止程序的继续执行,直到检测到一个边沿。
# GPIO.RISING、GPIO.FALLING、GPIO.BOTH 对边缘进行检测。
channel = GPIO.wait_for_edge(channel, GPIO.RISING, timeout=5000)
if channel is None:
    print('Time out occurred')
else:
    print('Edgedetectedonchannel',channel)

# 6. 线程回调边缘检测GPIO
# event_detected() 函数 event_detected() 函数被设计用于循环中有其它东西时使用,但不同于轮询的是,它不会错过当 CPU 忙于处理其它事物时输入状态的改变。
#单个回调
def my_callback(channel):
    print('这是一个边缘事件回调函数!')
    print('在通道 %s 上进行边缘检测' % channel)
    print('该程序与您的主程序运行在不同的进程中')
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  # 在通道上添加上升临界值检测

#多个回调
def my_callback_one(channel):
    print('回调 1')
def my_callback_two(channel):
    print('回调 2')
GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_callback_one)
GPIO.add_event_callback(channel, my_callback_two)

# 在通道上添加上升临界值检测,忽略由于开关抖动引起的小于 200ms 的边缘操作
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)
#或
GPIO.add_event_callback(channel, my_callback, bouncetime=200)

# 取消线程检测
GPIO.remove_event_detect(channel)

Guess you like

Origin blog.csdn.net/qq_41071754/article/details/114369219