1.8 Serial Read

Share for us

Overview

In addition to reading data from the electronic components, the Mega2560 board can read the data input in the serial port monitor, and you can use Serial.read() as the controller of the circuit experiment. Then we use LED to experiment with the Serial. Read() statement to control LED to turn on and off.

Components Required

Note: Refer to Part 2 to check details of hardware.

Fritzing Circuit

In this example, we use digital pin 9 to drive LED. When 1 is entered in serial monitor, the LED lights up. When 0 is entered, the LED turns off.

Schematic Diagram

Code

const int ledPin = 9;
int incomingByte = 0; 

void setup() 
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600); 
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();
    if(incomingByte=='1')
    {
      digitalWrite(ledPin,HIGH);
    }
    else if(incomingByte=='0')
    {
      digitalWrite(ledPin,LOW);
    }
  }
}

After the codes are uploaded to the Mega2560 board, please turn on the serial port monitor. Typing in ”1” can make LED turn on and typing in ”0” can make it turn off.

Code Analysis

Declare digital pin 9 as ledPin.

const int ledPin = 9;

Serial.read() reads a single byte of ASCII value, and therefore you need to declare a int type variable, incomingByte to store the acquired data.

int incomingByte = 0; 

Run the serial communication in setup() and set the data rate to 9600.

Serial.begin(9600); 

Set ledPin to OUTPUT mode.

pinMode(ledPin,OUTPUT);

The state of serial port monitor is judged in loop(), and the information processing will be carried out only when the data are received.

if (Serial.available() > 0){}

Reads the input value in the serial port monitor and stores it to the variable incomingByte.

 incomingByte = Serial.read();

When the character ‘1’ is obtained, the LED is lit; when ’0’ is obtained, the LED turns off.

if(incomingByte=='1'){digitalWrite(ledPin,HIGH);}
else if(incomingByte=='0'){digitalWrite(ledPin,LOW);}

NOTE: Serial.read() takes the ASCII value in single character, which means that when you input ‘1’, the obtained value is not the number ‘1’, but the character ‘1’ whose corresponding ASCII value is 49.

※ ASCII chart

The ASCII (American Standard Code for Information Interchange) encoding dates to the 1960’s. It is the standard way that text is encoded numerically.

Note that the first 32 characters (0-31) are non-printing characters, often called control characters. The more useful characters have been labeled.

ASCIICharacterASCIICharacterASCIICharacterASCIICharacterASCIICharacterASCIICharacter
0Null23 46.69E92/115s
1 24 47/70F93]116t
2 25 48071G94^117u
3 26 49172H95_118v
4 27 50273I96119w
5 28 51374J97a120x
6 29 52475K98b121y
7 30 53576L99c122z
8 31 54677M100d123{
9tab32space55778N101e124|
10line feed33!56879O102f125}
11 3457980P103g126`
12 35#58:81Q104h127 
13carriage return36$59;82R105i  
14 37%60<83S106j  
15 38&61=84T107k  
16 39,62>85U108l  
17 40(63?86V109m  
18 41)64@87W110n  
19 42*65A88X111o  
20 43+66B89Y112p  
21 44,67C90Z113q  
22 4568D91[114r 

Phenomenon Picture