Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Serial Communication

Serial communication on Arduino allows data exchange between the Arduino board and a computer (or other devices) through the Serial Monitor in the Arduino IDE. This feature is useful for debugging, data logging, and controlling Arduino using commands from a computer.

Objective:

Create a program that:

  • Sends a message to the Serial Monitor every second.
  • Reads input from the Serial Monitor to control an LED (turning it on or off based on user input).

Required Components:

  1. Arduino Uno
  2. LED
  3. 220-ohm resistor (for LED)
  4. USB cable (to connect Arduino to the computer)

Circuit Setup:

  1. LED Circuit:
    • Connect the longer leg (anode) of the LED to digital pin 9 on the Arduino through a 220-ohm resistor.
    • Connect the shorter leg (cathode) of the LED to GND on the Arduino.

Code Explanation

In this example, the Arduino sends a message to the Serial Monitor every second, and it also reads data entered in the Serial Monitor to turn the LED on or off based on commands.

Code:

// Define pin for LED
const int ledPin = 9;

void setup() {
  // Initialize Serial communication at 9600 baud rate
  Serial.begin(9600);
  
  // Set up the LED pin as an output
  pinMode(ledPin, OUTPUT);
  
  // Print a welcome message
  Serial.println("Type 'on' to turn LED ON, 'off' to turn LED OFF");
}

void loop() {
  // Check if data is available to read from the Serial Monitor
  if (Serial.available() > 0) {
    // Read the incoming data as a string
    String command = Serial.readStringUntil('\n');
    
    // Check the command and control the LED
    if (command == "on") {
      digitalWrite(ledPin, HIGH);    // Turn LED on
      Serial.println("LED is ON");
    } 
    else if (command == "off") {
      digitalWrite(ledPin, LOW);     // Turn LED off
      Serial.println("LED is OFF");
    } 
    else {
      Serial.println("Unknown command. Type 'on' or 'off'.");
    }
  }
  
  // Send a message every second
  delay(1000);
  Serial.println("Type 'on' or 'off' to control the LED.");
}

Code Breakdown:

  1. Setup Function:
    • Serial.begin(9600); starts serial communication at a baud rate of 9600, which is the speed of communication.
    • Serial.println("Type 'on' to turn LED ON, 'off' to turn LED OFF"); sends a message to the Serial Monitor, letting the user know what commands to use.
  2. Loop Function:
    • Serial.available() > 0 checks if there’s any data in the Serial buffer (i.e., if the user has typed anything in the Serial Monitor).
    • Serial.readStringUntil('\n'); reads the input from the Serial Monitor until a newline character, which completes the user’s command. The command is stored in the variable command.
    • if (command == "on") checks if the user typed “on”; if so, it turns the LED on and confirms with a message.
    • else if (command == "off") checks if the user typed “off”; if so, it turns the LED off and confirms with a message.
    • else handles any unknown command, prompting the user with a reminder to type “on” or “off”.
    • delay(1000); introduces a delay of 1 second, after which another message is sent, prompting the user to control the LED.

Using the Serial Monitor:

  1. Open the Arduino IDE.
  2. Upload the code to your Arduino board.
  3. Open the Serial Monitor by going to Tools > Serial Monitor or by pressing Ctrl+Shift+M.
  4. Set the baud rate to 9600 (to match Serial.begin(9600) in the code).
  5. Type “on” or “off” into the Serial Monitor and press Enter to control the LED.

Result:

  • Every second, a message will appear in the Serial Monitor, reminding you to type “on” or “off”.
  • When you type “on”, the LED turns on, and the Serial Monitor confirms with “LED is ON”.
  • When you type “off”, the LED turns off, and the Serial Monitor confirms with “LED is OFF”.

This example demonstrates how to use serial communication for sending messages to the Arduino and controlling hardware based on user input, which is helpful for real-time debugging and interaction with your projects.

    Leave a Reply

    Your email address will not be published.

    Need Help?