Product name:Bicolor LED
Product introduction:Two-color light-emitting diode (LED) can emit two different colors of light, usually red and green, rather than the only one color.
Product Image:
Bicolor LED
Experimental introduction:Connect the Bicolor LED sensor module to the GPIO pin of the Raspberry Pi through the female-to-female DuPont cable, and make the Bicolor LED sensor emit red and green light alternately through programming.
Sensor schematic diagram:
Components required for the experiment:
Raspberry Pi Board *1
Raspberry Pi Power Adapter*1
Bicolor LED sensor module*1
female-to-female DuPont cables
Bicolor LED Sensor Module |
Raspberry Pi GPIO (BOARD) |
Function Name |
Raspberry Pi GPIO ( wiring) |
S1 |
36 |
GPIO.27 |
27 |
S2 |
38 |
GPIO.28 |
28 |
GND |
34 |
GND |
|
Circuit connection:
Program running result:the traffic lights of the Bicolor LED sensor flash
Python code:
"""
import RPi.GPIO as GPIO
import time
# Remove warning message
GPIO.setwarnings(False)
# Set the pin mode to BOARD mode
GPIO.setmode(GPIO.BOARD)
# Two pins of bi-color LED connected to 34
pins = [36, 38]
# Delay time, the smaller the value, the faster the LED color switching, and vice versa
sleep_time = 0.1
#Pin initialization
for i in pins:
#Pin initialization
# GPIO.setup(pins, GPIO.OUT)
# 36 38 Pin is low voltage
# GPIO.setup(pins, GPIO.LOW)
GPIO.setup(i, GPIO.OUT, initial=GPIO.LOW)
while True:
# 36 Pin is high level, the light is on
GPIO.output(pins[0], GPIO.HIGH)
# Print prompt information, (can be not needed)
print('Red on')
# Delay
time.sleep(sleep_time)
# 36 Pin is low level, the light is off
GPIO.output(pins[0], GPIO.LOW)
print('Red off')
time.sleep(sleep_time)
# 38 The pin is high level and the light is on
GPIO.output(pins[1], GPIO.HIGH)
print('Green on')
time.sleep(sleep_time)
# 38 Pin is low level, the light is off
GPIO.output(pins[1], GPIO.LOW)
print('Green off')
time.sleep(sleep_time)
"""
C language code:
"""
#include <wiringPi.h>
#include <stdio.h>
#define LED_R 27 //Macro definition interface
#define LED_G 28
int main (void)
{
wiringPiSetup () ; //Initialize GPIO
pinMode (24, OUTPUT) ; //output mode
pinMode (25, OUTPUT) ; //output mode
for (;;)
{
digitalWrite (LED_R, HIGH) ; delay (500) ;
digitalWrite (LED_R, LOW) ; delay (500) ;
digitalWrite (LED_G, HIGH) ; delay (500) ;
digitalWrite (LED_G, LOW) ; delay (500) ;
}
return 0 ;
}
"""