Lesson 5 Serial Port

2535

Overview

In this lesson, we will program the Arduino MEGA 2560 to achieve function of send and receive data through the serial port. The Arduino receiving data which send from PC, and then controlling an LED according to the received data, then return the state of LED to the PC's serial port monitor.

Requirement

- 1* Arduino MEGA 2560

- 1* USB Cable

- 1* LED

- 1* 220Ω Resistor

- 1* Breadboard

- Several Jumper Wires

Principle

1. Serial ports

Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART). It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.

You can use the Arduino environment's built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin().

To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the MEGA 2560's USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device's RX pin, the RX to your device's TX pin, and the ground of your MEGA 2560 to your device's ground. (Don't connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board.)

2. Key function

●begin()

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

Syntax

Serial.begin(speed)

Parameters

speed: in bits per second (baud) - long

Returns

nothing

●print()

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example:

Serial.print(78) gives “78”

Serial.print(1.23456) gives “1.23”

Serial.print('N') gives “N”

Serial.print(“Hello world.”) gives “Hello world.”

An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:

Serial.print(78, BIN) gives “1001110”

Serial.print(78, OCT) gives “116”

Serial.print(78, DEC) gives “78”

Serial.print(78, HEX) gives “4E”

Serial.println(1.23456, 0) gives “1”

Serial.println(1.23456, 2) gives “1.23”

Serial.println(1.23456, 4) gives “1.2346”

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

Serial.print(F(“Hello World”))

To send a single byte, use Serial.write().

Syntax

Serial.print(val)

Serial.print(val, format)

Parameters

val: the value to print - any data type format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns

byte print() will return the number of bytes written, though reading that number is optional

●println()

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '∖r') and a newline character (ASCII 10, or '∖n'). This command takes the same forms as Serial.print().

Syntax

Serial.println(val)

Serial.println(val, format)

Parameters

val: the value to print - any data type

format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns

byte
println() will return the number of bytes written, though reading that number is optional

●read()

Reads incoming serial data. read() inherits from the Stream utility class.

Syntax

Serial.read()

Parameters

None

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

Procedures

1. Build the circuit

1.png

2. Program

_05_serial.ino

/***********************************************************
File name: 05_serial.ino
Description: If you send a character ‘1’ or ‘0’ on the 
             serial monitor, the status of LED will be lit 
             or gone out.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/12/27 
***********************************************************/

int ledpin=11;           //definition digital 11 pins as pin to control the LED

void setup()
{
  Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
  pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output
}
void loop()
{
    char receiveVal;                  // Defined receive data
   
    if(Serial.available() > 0)       //Receive serial data
    {        
        receiveVal = Serial.read();  //Save the serial data received 
        
       if(receiveVal == '1')          //Receive data is 1, lit LED lights    
       { 
           digitalWrite(ledpin,HIGH); //print out the value of the LED       
           Serial.println("LED:ON"); //send data to the serial monitor
       }
       if(receiveVal == '0')          //Receive data is 0, off LED lights
       { 
           digitalWrite(ledpin,LOW);  //print out the value of the LED                
           Serial.println("LED:OFF");//send data to the serial monitor
      } 
    }
  delay(50);                          //delay 50ms
}


3. Compile the program and upload to Arduino MEGA 2560 board

Open the port monitor, and then select the appropriate baud rate according to the program.

Now, if you send a character‘1’or‘0’on the serial monitor, the state of LED will be lit or gone out.

2.png

3.jpg

Summary

Through this lesson, you should have understood that the computer can send data to Arduino MEGA 2560 via the serial port, and then control the state of LED. I hope you can use your head to make more interesting things based on this lesson.