Read values sent by Serial Monitor to Arduino

ArduinoArduino BoardsArduino IDEArduino Programming Language

The Serial Monitor of Arduino has a text box at the top, through which, users can send in text to the Arduino board.

The text can be read by Serial.read(). Also, the Serial.available() function can be used to check if there is any data to read. It returns the number of characters or bytes available for reading, i.e., the number of bytes stored in the serial receive buffer.

Example

Using these functions, let’s create a simple echo program for Arduino. The code for the same can be found below −

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   Serial.println();
}

void loop() {
   // put your main code here, to run repeatedly:
   if(Serial.available()> 0){
      char c = Serial.read();
      Serial.print(c);
   }
}

Over here, it is best that you check the Serial Monitor output for yourself. Try sending text to your board, and see it echo it back to you.

Output


raja
Published on 29-May-2021 13:30:30
Advertisements