www.joy-it.net
Pascalstr. 8 47506 Neukirchen-Vluyn
2. Code example
We provide you with two sample codes to test your RGB LED. One code
which sets the LED to a color and then holds it each for 3 seconds (led.py)
and one code which sets the LED to a pulsating mode in dierent colors
(pwm.py).
The following example code is the one named led.py. First, you create a
file with the following command and copy the following code inside of it.
# Required modules are imported and set up
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Here you declare the output pin to which the LEDs are connected.
LED_ROT = 25
LED_GRUEN = 24
LED_BLAU = 23
GPIO.setup(LED_ROT, GPIO.OUT, initial= GPIO.LOW)
GPIO.setup(LED_GRUEN, GPIO.OUT, initial= GPIO.LOW)
GPIO.setup(LED_BLAU, GPIO.OUT, initial= GPIO.LOW)
print ("LED-Test [druecken Sie STRG+C, um den Test zu beenden]")
# Main program loop
try:
while True:
print("LED ROT 3 Sekunden an")
GPIO.output(LED_ROT,GPIO.HIGH) #LED red is switched on
GPIO.output(LED_GRUEN,GPIO.LOW) #LED green stays off
GPIO.output(LED_BLAU,GPIO.LOW) #LED blue stays off
time.sleep(3) # Lights up for 3 seconds
print("LED GRUEN 3 Sekunden an")
GPIO.output(LED_ROT,GPIO.LOW) #LED red stays off
GPIO.output(LED_GRUEN,GPIO.HIGH) #LED green is switched on
GPIO.output(LED_BLAU,GPIO.LOW) #LED blue stays off
time.sleep(3) # Lights up for 3 seconds
print("LED BLAU 3 Sekunden an")
GPIO.output(LED_ROT,GPIO.LOW) #LED red stays off
GPIO.output(LED_GRUEN,GPIO.LOW) #LED green stays off
GPIO.output(LED_BLAU,GPIO.HIGH) #LED blue is switched on
time.sleep(3) # Lights up for 3 seconds
# Clean up after the program has ended
except KeyboardInterrupt:
GPIO.cleanup()
sudo nano led.py
With the following command you can now run the program:
sudo python3 led.py