Skip to main content

How to control digital output with serial monitor in Arduino

Hello Friends, in this blog we will be controlling digital output with serial monitor command.


First let’s understand the working of serial monitor.

Serial monitor in Arduino IDE is a tool which allows communication between the computer and Arduino board via a serial connection, normally we use USB cable for connection.


What are the features of Serial Monitor?

It shows the data sent from the Arduino board by using the functions like Serial.print() or Serial.println().


It allows to send text or numeric data to the Arduino board, which can be read by function like, Serial.read() or Serial.parseInt(), thereafter you can use this data for further analysis and action.


We can use this tool for debugging and monitoring the function of the sketch.


There is a procedure to use the serial monitor, below are the steps given.

First initialize the serial communication in the sketch as given below. Normally baud rate is set 9600.


 Void setup(){

         Serial.begin(9600);

}


Data can be printed on the serial monitor as the below way

Serial.print(“Arduino is Good”);

It prints the data to the monitor.


Serial.println(“Arduino is Good”);

It prints the data to the monitor with new line.




Reading of the data can be done like the following steps.

If(Serial.available()>0){

Char input = Serial.read();

Serial.print(“You typed: “);

Serial.print(input);

}


Serial Monitor window can be opened by clicking to the Serial Monitor Icon on the top-right side of the Arduino IDE or just go to Tool > Serial Monitor.


Check the baud rate, it should be as same as you set in the sketch during initialization of serial communication. (9600).


Now we will write the sketch for controlling one digital output and also print the status on serial monitor.


 // It defines the pin for the digital output, pin 2 is used

const int outputPin = 2;


 void setup() {

  // Initializes the digital output pin

  pinMode(outputPin, OUTPUT);

  // Initialize the Serial Monitor

  Serial.begin(9600);

  Serial.println("Send OUT1_ON to turn ON, OUT1_OFF to turn OFF");

}


void loop() {

  // Checks if data is available on the Serial Monitor

  if (Serial.available() > 0) {


    // Reads the incoming command

    String command = Serial.readStringUntil('\n');

    command.trim(); // Removes any whitespace or newline characters


     // Handles the commands

    if (command == "OUT1_ON") {

      digitalWrite(outputPin, HIGH);

      Serial.println("Output 1 turned ON");

}


else if (command == "OUT1_OFF") {

      digitalWrite(outputPin, LOW);

      Serial.println("Output 1 turned OFF");

}


else {

      Serial.println("Invalid command. Use OUT1_ON or OUT1_OFF");

    }

  }

}



After writing the code and compilation, upload it to the Arduino board, (I am using Arduino UNO)


Test the code

Open the serial monitor, and ensure the baud rate is 9600.

Send the command OUT1_ON, "Output 1 turned ON") should be printed to the serial monitor.

Send the command OUT1_OFF, "Output 1 turned OFF") should be printed to the serial monitor.


 


 


 

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for ...

Difference between Bootloader & Firmware Update

Updating the bootloader and updating the firmware in a microcontroller are two distinct processes, each serving different purposes and involving different steps. Here's a breakdown of the differences: Bootloader Update Purpose : The bootloader is a small program that runs before the main application. It is responsible for initializing the microcontroller and loading the main application firmware. Updating the bootloader might be necessary to add new features, fix bugs, or improve the boot process.   Process : 1.       Enter Bootloader Mode : To update the bootloader, the microcontroller must be put into a special mode. This often involves setting specific pins, pressing buttons, or using a specific command through an existing firmware interface. 2.      Communication Interface : The bootloader update typically uses a specific communication interface such as UART, SPI, I2C, or USB. 3.   ...