RGB LED

04. RGB LED

This example of digital output uses a RGB LED to display cyclically, every second, the three red, green and blue colors (see Figure 2). It can be managed from the Internet via start and stop commands.

Figure 1 - RGB LED circuit

1. Circuit

Connect the RGB LED as shown in Figure 1:

  • red PIN to d5,
  • GND PIN to d6,
  • green PIN to d7
  • blue PIN to d8.

The longest PIN is the negative.

2. Sketch

Write a sketch K to define:

  • the output connections: r :: d5, gnd :: d6, g :: d7, b :: d8
  • the commands to start and stop the system: start, stop
  • the rules to implement the automaton in Figure 2
r :: d5
gnd :: d6   
g :: d7   
b :: d8   
start = [gnd=0,r=1]   
stop = [r=0,g=0,b=0]   
b==1 -> [b=0,r=1] ##1   
g==1 -> [g=0,b=1] ##1   
r==1 -> [r=0,g=1] ##1 

or simply load the sketch in the workspace with the command

] load e04_rgb_led

Note: the automaton state is identified by the only LED on.

Figure 2 - RGB LED automaton

3. Try and learn

Try the following commands now:

  • ] start

the RGB LED starts with the red, after 1 second becomes green, after 1 second becomes blue and after 1 second it resumes with the red

  • ] pause

the LED stops

  • ] play

the LED resumes

  • ] stop

turns off the LED.

4. Summarizing

In this lesson you learned or repeated:

  • how to use a RGB LED
  • the commands: start, pause, play, stop.