Arduino Based LED Color Selector With Serial Communications From the User.

by hariharan_io in Circuits > Arduino

82 Views, 0 Favorites, 0 Comments

Arduino Based LED Color Selector With Serial Communications From the User.

Arduino LED Color Selector with serial communications from the user.png

Welcome to the "Arduino LED Color Selector with Serial Communications project!"

In this instructable, I will guide you through creating a simple yet interactive system using an Arduino Uno, three LEDs (green, yellow, and red), and a breadboard. This project allows users to control the color of LEDs via serial commands entered through the Arduino IDE's Serial Monitor.

The goal of this project is to help beginners and enthusiasts understand the basics of Arduino programming, circuit design, and serial communication. By the end of this tutorial, you will be able to send commands from your computer to the Arduino, instructing it to light up different LEDs based on your input.

Supplies

you can download this project from my GitHub by using this link:

https://github.com/HariHaranio/Arduino-LED-Color-Selector-with-serial-communications-from-the-user-.git

---or---

you can simulate this project in wokwi.com by using this link:

https://wokwi.com/projects/398199480796721153

okay! let start this one:

just follow the step-by-step instructions given below:

Gather Your Components.

download.jpg
OIP.jpg
download.jpg
download.jpg
OIP.jpg

You will need the following components:

  • Arduino Uno
  • Breadboard
  • 1 Green LED
  • 1 Yellow LED
  • 1 Red LED
  • 3 Resistors (220 ohms or 330 ohms recommended)
  • Jumper wires

Set Up the Circuit.

1.Place the LEDs on the Breadboard:

Insert the green LED into the breadboard. Note that the longer leg is the anode (positive) and the shorter leg is the cathode (negative).

Repeat this for the yellow and red LEDs.

2.Connect the Resistors:

Connect one end of a resistor to the cathode (shorter leg) of the green LED.

Connect the other end of the resistor to the ground rail (blue rail) of the breadboard.

Repeat this for the yellow and red LEDs.

3.Wire the LEDs to the Arduino:

Connect a jumper wire from the anode (longer leg) of the green LED to digital pin 9 on the Arduino.

Connect a jumper wire from the anode of the yellow LED to digital pin 10 on the Arduino.

Connect a jumper wire from the anode of the red LED to digital pin 11 on the Arduino.

4.Connect the Ground:

Connect a jumper wire from the ground rail (blue rail) of the breadboard to one of the GND pins on the Arduino.


Write the Arduino Code

In this step you can either use hardware or software to do this project.

if you're using Arduino IDE please make sure you are select the correct board (Arduino UNO) and COM port.

Copy and paste the download code into the Arduino IDE and save the file,

step to upload the Code:

  • Connect your Arduino Uno to your computer using a USB cable.
  • Select the correct board and port from the Tools menu in the Arduino IDE.
  • Click the upload button to upload the code to your Arduino.

Test the Project

Open the Serial Monitor:

  • Click on the Serial Monitor button in the Arduino IDE (or press Ctrl+Shift+M).

Send Commands:

  • Type "green" and press Enter to light up the green LED.
  • Type "yellow" and press Enter to light up the yellow LED.
  • Type "red" and press Enter to light up the red LED.
  • Type "all" and press Enter to light up all three LEDs.
  • Type "clear" and press Enter to turn off all the LEDs.
  • If you type an invalid color, you will see an error message prompting you to choose 'green', 'yellow', or 'red'.

Summary.

By following these steps, you have created an Arduino-based LED color selector that can be controlled through serial communication. This project is a great way to learn about digital output and serial communication with Arduino. Experiment with different commands and have fun customizing the behavior of your LEDs!

Code Explanation

Arduino LED Color Selector Code

Below is the Arduino code for the LED Color Selector project:


const int green_led = 9; // setting pin 9 to green LED
const int yellow_led = 10; // setting pin 10 to yellow LED
const int red_led = 11; // setting pin 11 to red LED

String main_string = "Please! choose the color to light up ";
String color_string = "The colors are 'green', 'yellow', 'red' ";
String clear_command = "Type 'All' to turn on all the LEDs or type 'clear' to turn off all the LEDs";
String get_color;

void setup() {
Serial.begin(9600);

pinMode(green_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(red_led, OUTPUT);
Serial.println(main_string);
Serial.println(color_string);
Serial.println(clear_command);
}

void loop() {
while (Serial.available() == 0) {
// wait for user input
}
get_color = Serial.readString();
get_color.trim(); // remove any leading/trailing whitespace
get_color.toLowerCase();
Serial.println(get_color);

// turn off all LEDs initially
digitalWrite(green_led, LOW);
digitalWrite(yellow_led, LOW);
digitalWrite(red_led, LOW);

if (get_color == "green") {
digitalWrite(green_led, HIGH);
} else if (get_color == "yellow") {
digitalWrite(yellow_led, HIGH);
} else if (get_color == "red") {
digitalWrite(red_led, HIGH);
} else if (get_color == "all") {
digitalWrite(green_led, HIGH);
digitalWrite(yellow_led, HIGH);
digitalWrite(red_led, HIGH);
} else if (get_color == "clear") {
digitalWrite(green_led, LOW);
digitalWrite(yellow_led, LOW);
digitalWrite(red_led, LOW);
} else {
Serial.println("Invalid color! Please choose 'green', 'yellow', or 'red'.");
}
}


Certainly! Here’s a step-by-step explanation of the Arduino code:

1. Define Pin Constants:

  const int green_led = 9; // setting pin 9 to green led  

  const int yellow_led = 10; // setting pin 10 to yellow led

  const int red_led = 11; // setting pin 11 to red led


  These lines define constants for the pin numbers to which the LEDs are connected.

  `green_led` is connected to pin 9.

  `yellow_led` is connected to pin 10.

  `red_led` is connected to pin 11.


2. Define String Variables;

  String main_string = "Please! choose the color to light up ";

  String color_string = "The colors are 'green', 'yellow', 'red' ";

  String clear_command = "Type 'All' to turn on all the led or Type 'clear' to turn off all the led";

  String get_color;


These lines define several strings that will be used to display messages to the user.

  `main_string` prompts the user to choose a color.

  `color_string` lists the available colors.

  `clear_command` provides instructions for additional commands.

  `get_color` will store the user's input.


3. Setup Function:

  void setup() {

   Serial.begin(9600);

    

   pinMode(green_led, OUTPUT);

   pinMode(yellow_led, OUTPUT);

   pinMode(red_led, OUTPUT);

   Serial.println(main_string);

   Serial.println(color_string);

   Serial.println(clear_command);

  }

  `setup()` initializes the serial communication at 9600 baud rate.

  `pinMode()` sets the LED pins as output.

  `Serial.println()` prints the prompt messages to the serial monitor.


4. Loop Function:

  void loop() {

   while (Serial.available() == 0) {

    // wait for user input

   }

   get_color = Serial.readString();

   get_color.trim(); // remove any leading/trailing whitespace

   get_color.toLowerCase();

   Serial.println(get_color);


  `loop()` continuously checks for user input.

  `while (Serial.available() == 0)` waits until the user enters something.

  `get_color = Serial.readString()` reads the input string from the serial monitor.

  `get_color.trim()` removes any leading or trailing whitespace.

  `get_color.toLowerCase()` converts the input to lowercase.

  `Serial.println(get_color)` prints the user's input back to the serial monitor.


5. Turn Off All LEDs Initially:

   // turn off all LEDs initially

   digitalWrite(green_led, LOW);

   digitalWrite(yellow_led, LOW);

   digitalWrite(red_led, LOW);


  These lines ensure that all LEDs are turned off initially.


6.Check User Input and Control LEDs:

   if (get_color == "green") {

    digitalWrite(green_led, HIGH);

   } else if (get_color == "yellow") {

    digitalWrite(yellow_led, HIGH);

   } else if (get_color == "red") {

    digitalWrite(red_led, HIGH);

   } else if (get_color == "all") {

    digitalWrite(green_led, HIGH);

    digitalWrite(yellow_led, HIGH);

    digitalWrite(red_led, HIGH);

   } else if (get_color == "clear") {

    digitalWrite(green_led, LOW);

    digitalWrite(yellow_led, LOW);

    digitalWrite(red_led, LOW);

   } else {

    Serial.println("Invalid color! Please choose 'green', 'yellow', or 'red'.");

   }

  These lines check the value of `get_color` and control the LEDs accordingly:

  If `get_color` is "green", the green LED is turned on.

If `get_color` is "yellow", the yellow LED is turned on.

  If `get_color` is "red", the red LED is turned on.

  If `get_color` is "all", all LEDs are turned on.

  If `get_color` is "clear", all LEDs are turned off.

  If the input is invalid, an error message is printed.


By following these steps, the Arduino program reads a color command from the user via the serial monitor and lights up the corresponding LED(s).

Final Step

clear.png
greenled.png
red.png
yellow.png
all.png

the above pictures show the working of this project .

note:

if you have any questions regarding this topic. comments below.

And you can find this project in my GitHub the link is given.
And you can also use my simulation in wokwi.com.


Thank you for visiting my project.