Objective
The goal of this lab is to introduce the concept of a State Machine as a way to manage program flow. You will design and implement a simple state machine to control the color of an RGB LED using commands sent from the Serial Monitor.
Prerequisites
Before starting this lab, you should be comfortable with:
- Setting up and wiring an Arduino Uno R3.
- Using the Arduino IDE to write, compile, and upload code.
- Opening and using the Serial Monitor to send and receive data.
- Wiring a common-cathode RGB LED with current-limiting resistors.
- Using the
analogWrite()function to control the brightness of LED channels.
Materials Required
- 1 x Arduino Uno R3
- 1 x USB Cable
- 1 x Breadboard
- 1 x Common Cathode RGB LED
- 3 x 220Ω Resistors
- Jumper Wires
- A computer with the Arduino IDE installed
Procedure
-
Part 1: Design Your State Machine
Before writing any code, you must design your state machine on paper. This is a critical first step in planning your program's logic.
Your state machine should have four states:
- IDLE: The initial state where the LED is off, waiting for a command.
- RED_ON: The state where the Red color is on.
- GREEN_ON: The state where the Green color is on.
- BLUE_ON: The state where the Blue color is on.
The transitions will be the keyboard characters you send through the Serial Monitor: 'r', 'g', and 'b'.
Task: Sketch your state machine. Use circles to represent the states and arrows to represent the transitions. Label the arrows with the input character that causes the transition. For example, an arrow from IDLE to RED_ON should be labeled with 'r'.
Hint: Think about what happens when you are in the RED_ON state and you press 'g'. The machine should transition from the RED_ON state to the GREEN_ON state. All key presses ('r', 'g', 'b') should be valid from any state. -
Part 2: Hardware Setup
Wire your circuit according to the diagram below. Use PWM-capable pins (marked with a '~' on the Arduino board) for the color channels to allow for brightness control with
analogWrite().- Connect the longest leg (cathode) of the RGB LED to the Arduino's GND pin.
- Connect the Red pin of the LED through a 220Ω resistor to Arduino Pin ~9.
- Connect the Green pin of the LED through a 220Ω resistor to Arduino Pin ~10.
- Connect the Blue pin of the LED through a 220Ω resistor to Arduino Pin ~11.
-
Part 3: Software Implementation
Now, translate your state machine diagram into Arduino code. We will use a variable to keep track of the current state. A
switchstatement is a perfect tool for implementing the logic of a state machine.Create a new sketch in the Arduino IDE and use the following code as a guide. Read the comments carefully to understand how the code maps to your state machine diagram.
// Lab 3: State Machine Control of an RGB LED // Student Name: [Your Name Here] // Define the pins for the RGB LED const int RED_PIN = 9; const int GREEN_PIN = 10; const int BLUE_PIN = 11; // A variable to store the current state of our machine. // We can use a character to make it more readable. // 'i' for IDLE, 'r' for RED, 'g' for GREEN, 'b' for BLUE. char state = 'i'; void setup() { // Initialize the digital pins as outputs. pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); // Start serial communication at 9600 bits per second. Serial.begin(9600); Serial.println("RGB LED Controller Initialized."); Serial.println("Send 'r', 'g', or 'b' to change the color."); // Set the initial state: LED is off. setLedColor(0, 0, 0); } void loop() { // Step 1: Check for an incoming event (a transition trigger). // In our case, the event is a new character in the Serial buffer. if (Serial.available() > 0) { // Read the incoming character char input = Serial.read(); // Step 2: Update the state based on the input. // This is where we handle the transitions. if (input == 'r') { state = 'r'; Serial.println("State changed to: RED_ON"); } else if (input == 'g') { state = 'g'; Serial.println("State changed to: GREEN_ON"); } else if (input == 'b') { state = 'b'; Serial.println("State changed to: BLUE_ON"); } } // Step 3: Perform actions based on the current state. // The switch statement is the core of our state machine execution. switch (state) { case 'i': // IDLE state setLedColor(0, 0, 0); // Turn LED Off break; case 'r': // RED_ON state setLedColor(255, 0, 0); // Turn Red On break; case 'g': // GREEN_ON state setLedColor(0, 255, 0); // Turn Green On break; case 'b': // BLUE_ON state setLedColor(0, 0, 255); // Turn Blue On break; } } // A helper function to make setting the LED color easier. // It takes red, green, and blue values (0-255) and applies them. void setLedColor(int red, int green, int blue) { analogWrite(RED_PIN, red); analogWrite(GREEN_PIN, green); analogWrite(BLUE_PIN, blue); } -
Part 4: Testing and Verification
Upload your code to the Arduino Uno. Open the Serial Monitor (Tools > Serial Monitor or Ctrl+Shift+M). Make sure the baud rate is set to 9600.
Type 'r' into the input box and press Enter or click Send. The RGB LED should turn red. Then, type 'g' and send it. The LED should turn green. Test the 'b' key as well. Verify that the system behaves exactly as you designed in your state machine diagram.
Submission Requirements
You are required to submit a single ZIP file containing the following:
- State Machine Diagram: A clear photo or scan of the state machine diagram you sketched in Part 1. (e.g.,
Lab3_Diagram.jpg) - Arduino Sketch: Your final, commented
.inofile. (e.g.,Lab3_Code.ino) - Demonstration Video: A short video (max 30 seconds) showing your project working. The video must clearly show you typing a character ('r', 'g', or 'b') into the Serial Monitor and the corresponding color change on the RGB LED.