HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
display
Deutsch   English   

6. Color LEDs

 

 

YOU LEARN HERE...

 

how colors are made up of a red, a green and a blue component and how to light up the color LEDs with the desired color.

 

 

RGB-LED

  To save and process color images with the computer, the colors must be defined as numbers. There are several ways to do this. The best known is the RGB color model, where the intensities of the three color components red, green and blue are specified as numbers between 0 and 255.

With the Calliope's color LED, you can experiment with the colors in the RGB model.

The command
set_colors(r, g, b)
where r, g, and b are numbers between 0 and 255, sets the red, green and blue color components.

 

 

The Calliope_mini 3 has three RGB LEDs. These are addressed as NeoPixels:

LEDs = NeoPixel(pin_RGB, 3)
LEDs[0] = (r, g ,b)
LEDs[1] = (r, g ,b)
LEDs[1] = (r, g ,b)
LEDs.show()
 


 

EXAMPLES

 

Example 1: Lighting up a color LED in different colors
The color LED lights up in sequence in the colors red, green, blue, yellow, magenta and cyan for 1000 milliseconds and then switches off.

 




Program:

from calliope_mini import *

led.set_colors(255, 0, 0)
sleep(1000)    
led.set_colors(0, 255, 0)
sleep(1000)    
led.set_colors(0, 0, 255)
sleep(1000) 
led.set_colors(255, 255, 0)
sleep(1000)
led.set_colors(255, 0, 255)
sleep(1000) 
led.set_colors(0, 255, 255)
sleep(1000) 
led.set_colors(0, 0, 0) 
► Copy to clipboard

For Calliope_mini 3 you have to adapt the program accordingly. Here the first LED lights up red, the second green and the third light blue.

Program:

from calliope_mini import * 
from neopixel import *

LEDs = NeoPixel(pin_RGB, 3)
LEDs[0] = (255,0,0)
LEDs[1] = (0,255,0)
LEDs[2] = (0,255,255)
LEDs.show()
► Copy to clipboard

If the maximum possible number 255 is selected for the color component, the LEDs light up very brightly. It is more pleasant if you choose smaller numbers for r, g, b:
LEDs[0] = (100,0,0)
LEDs[1] = (0,100,0)
LEDs[2] = (0,100,100)

 

Example 2: Lighting up the color LED in randomly selected colors

Here, the color LED is to light up in randomly selected colors for 500 ms each. You determine the color components r, g, b in the randomColor()function with three random numbers in the range 0 ... ... 100.

Press button A to end the color game.

 

Program:

from calliope_mini import *
from random import randint

def randomColor():
    r = randint(0, 100)
    g = randint(0, 100)
    b = randint(0, 100)
    led.set_colors(r, g, b)
    
while not button_a.was_pressed():
     randomColor()
     sleep(500)       
led.set_color(0, 0, 0) 
► Copy to clipboard

Example 3: Using a color LED for a voltage display
The Calliope can also be used for physical experiments, for example to measure voltages of a sensor in the range 0...3.3V.

If you touch the + (3.3V) connection and pin 1 at the same time, a circuit is created and the voltage at pin 1 increases.
Thel pin1.read_analog()function returns the voltage as a number between 0 and 1023.

In your program, the LED lights up blue without a circuit, green when pin 1 is touched and red when pin 2 is touched.

 

Program:

from calliope_mini import *

while True:
    if pin1.read_analog() > 400:
        led.set_colors(0, 255, 0)
    elif pin2.read_analog() > 400:
        led.set_colors(255, 0, 0)   
    else:
        led.set_colors(0, 0, 255)          
    sleep(100)
► Copy to clipboard

 

 

REMEMBER ...

 

The color of a color LED is determined by the red, green and blue color components (a number between 0 and 255). As the LED shines very brightly, it is better to use numbers between 0 and 100.
Calliope Mini 3 has three RGB LEDs. You import the neopixel module and select the colors according to the template in example 1

 

 

TO SOLVE BY YOURSELF

 

 

1.
Write a program with which the color LED lights up in rainbow colors one after the other. Repeat this color sequence 3 times. Use the adjacent assignment of the colors.  

2.

When you press button A, the color LED should light up in the color magenta, when you press button B it should light up in the color cyan. So that the LED does not light up too brightly, select a maximum value of 100 for the color components.

3.

if you touch pin + and pin 1 with your fingertips, the color LED flashes red, if you touch pin + and pin 2, it flashes blue, if you touch pin + and pin 3, it flashes green.

4.

In an endless while loop, the color LED with white color should send the SOS signal, whereby it should light up three times briefly (200 ms) at S and three times for a long time (600 ms) at O. Each SOS signal is followed by a pause.