P2: Control the RGB LED from your computer

Controlling an RGB LED using commands sent from the computer’s COM monitor.

Control the RGB LED from your computer

What you will review in this project

In this project, we will combine what we have learned in previous lessons:

  • Controlling the red, green and blue channels of an RGB LED.
  • Receiving and sending text through the COM port.
  • Using a variable to store a message.
  • Using conditions to decide what to do based on the command received.

Prerequisites

This project puts into practice the concepts learned in L4: Variable-brightness LED, L5: The RGB LED, L6: The serial port and L7: The pushbutton.

The task

Build a program that lets you control the RGB LED’s colour from your computer. You will be able to type these commands in the COM monitor:

CommandWhat the board should do
redTurn the LED red.
greenTurn the LED green.
blueTurn the LED blue.

The LED will start off and keep the selected colour until another valid command arrives. Each time it changes colour, the board will send a confirmation, for example: Colour changed to red.

If it receives an unknown command, it will keep the current colour and reply: Unknown command. Use: red, green or blue.

Before you start

In L6, we made an echo program: the board sent back the text it received. Now that text will be a command that triggers a physical action.

Think about the steps a command goes through: you type red on the computer, the board stores the text in a variable, checks which word you sent, changes the LED and replies through COM.

Prepare a table with each command, the R, G and B values it needs, and the confirmation message. Also decide what should happen if you type a word such as hello.

Hints

Only open these hints if you need them. Try it on your own first!

Show hints
  • Configure the COM port at 115200 bps: the COM monitor always uses this speed.
  • Keep the wiring from L5: GPIO4 for red, GPIO16 for green and GPIO17 for blue.
  • Create a variable called Message with an initial value of "" (empty text).
  • Check whether data is available before reading. Read the text just once and store it in Message.
  • Use the = comparison block from Logic to check whether Message equals the text red, green or blue.
  • Extend an if block with two else if branches and a final else branch for unknown commands.
  • For each colour, set its channel to 100 and the other two to 0.
  • Type commands in lowercase, without quotation marks or spaces at the start or end.
  • You do not need any waits: if there is no new command, leave the LED as it is.

Solution

Show solution

Materials

  • ESP32 development board.
  • BatSTEM expansion board.
  • USB connection cable.
  • Breadboard.
  • Common-cathode RGB LED with 4 leads.
  • Three 100 Ohm resistors.
  • Jumper wires.
  • Computer with access to BatSTEM Cloud.

Wiring

Use the same circuit as in L5: The RGB LED:

  1. Connect GND on the expansion board to the blue rail on the breadboard.
  2. Place the RGB LED with each lead in a different row.
  3. Connect the common lead to GND.
  4. Connect the red channel to GPIO4 through a 100 Ohm resistor.
  5. Connect the green channel to GPIO16 through another 100 Ohm resistor.
  6. Connect the blue channel to GPIO17 through another 100 Ohm resistor.
  7. Connect the board to the computer.

Program

In the setup block:

  1. Configure the COM port at 115200 bps.
  2. Create an RGB LED called MyRGBLED and assign GPIO4 to R, GPIO16 to G and GPIO17 to B.
  3. Create the Message variable with an initial value of "".
  4. Set all three LED channels to 0 so that it starts off.

In the main loop:

  1. Add an if block with the condition data available on COM port.
  2. Inside it, set Message to the value of read text from COM port.
  3. Then add another if block. Use its gear icon to add two else if branches and an else branch.
  4. Compare Message with red, green and blue in the three conditions. In each branch, change all three LED channels and write the corresponding confirmation, with a new line.
  5. In the final else branch, write Unknown command. Use: red, green or blue. Do not change the LED in this branch.

Block program that receives a command through COM, selects the RGB LED’s colour and confirms the result.

ConditionRGBCOM reply
Message = red10000Colour changed to red
Message = green01000Colour changed to green
Message = blue00100Colour changed to blue
Any other commandUnchangedUnchangedUnchangedUnknown command. Use: red, green or blue

The = comparison checks whether two values are equal; it does not change the variable. The set Message to… block stores a new value. The if / else if / else branches ensure that only the response matching the received command runs.

All message processing must stay inside if data available on COM port. This way, the board replies once per command and does not continually fill the monitor with the same message.

Check the result

  1. Run the sketch and open COM Mon.. The LED should start off.
  2. Type red and send it: the LED should turn red and Colour changed to red should appear.
  3. Wait a few seconds without sending anything: the colour should stay the same and no new replies should appear.
  4. Send green, then blue. Check the colour and confirmation each time.
  5. Send hello: the help message listing the valid commands should appear, and the LED should stay blue.
  6. Send red again to check that the program still accepts commands after an unknown one.

Optional challenges

  1. Add the command off to set all three channels to 0.
  2. Add yellow, cyan and magenta by mixing two channels, as in L5.
  3. Add soft red to set R to 20, G to 0 and B to 0. Compare its brightness with the red command.
  4. Add a help command that shows the available commands without changing the colour.
  5. Extend the confirmations to include the R, G and B values used.

Remember to update the message listing valid commands when you add new ones.

Project summary

  • We have sent commands from the computer to control a physical component.
  • We have stored the received text in a variable and compared it with several words.
  • We have used conditions to select an RGB LED’s colour.
  • We have confirmed actions and replied to unknown commands through the COM monitor.
  • We have kept the LED’s state until a new valid command arrives.