Working with Serial Data

Planning
Step 1
Communicating via Serial

Serial Communication is a method for transmitting and receiving data

In this project we will use Serial Communication to send control data between a remote transmitter and a robotic receiver.

Information
Step 2
Serial data is sent and received one byte at a time

We use the commands read and write for receiving and transmitting bytes.

Information
Step 3
Our Arduino communicates with the computer through the Serial Port
Step 4
Open the Serial Monitor by clicking the Magnifying Glass Icon
Temp1493944730 Temp1493944684
Information
Step 5
We can use the Serial Monitor to view Serial data on the Arduino

The box at the top let's us send data to the Arduino's receiver pin.

Temp1493943749

The box at the bottom let's us view the Arduino's transmitted data.

Information
Step 6
You may remember using the Tx and Rx pins from previous lessons
Temp1494021163

These pins are linked to the Arduino's Serial port.

Planning
Step 7
Let's send data to the computer

To do this we first need to set the baud rate- the rate at which data is transferred in bytes per second.

The default baud rate is 9600 as you can see in the monitor:

Temp1493945105
Step 8
Use Serial.begin() to set the Serial Port's baud rate to 9600 bytes/sec
Temp1493945178
Step 9
Now we can use the write command to transmit data
Temp1493946199

Hit Upload and you'll see this data in the Serial Monitor's Tx field.

Temp1493946359
Step 10
Serial Buffers

Writing Serial data places the data into a buffer which transmits the information one byte at a time through the port:

This data is then stored by the receiving entity in a second buffer.

Information
Step 11
Sending data from the computer to the Arduino

This field in the Serial Monitor allows us to send data to the Arduino's receiver pin (The Rx pin).

Temp1493947882

The data will be stored in the Serial buffer.

Information
Step 12
Serial.read()

Each time Serial.read() is called, the first character in the buffer pops out and gets returned:

Temp1493948430
Step 13
Create a character type variable named "inByte"

In the loop() function, have it continuously read from the Serial buffer.

Temp1493949068

Then write inByte back to the Serial Monitor so we can check it

Check Your Work
Step 14
Upload your code and check the Serial Monitor

You should see a constant stream of null-characters!

Temp1493949174

We need to tell our program to only read from the buffer if there is data available.

Step 15
Use Serial.available to make the program only read data if there is data available in the Serial buffer
Temp1493949405 Temp1493949610
Check Your Work
Step 16
Whatever you type in the Rx field should now appear in the Tx field

Send a few messages and notice how they get written right next to each other:

Temp1493949778
Information
Step 17
Right now our system looks like this

We type something and send it to the Rx pin of our Arduino's Serial Port buffer.

Then we can read each character using Serial.read

Temp1493953426

Lastly we write the character to the Tx pin of the Arduino's Serial Port buffer, which we can see in the monitor.

Information
Step 18
The newline character '\n'

This is a special, invisible, character \n which creates a newline when read.

For example

Temp1493950294
Step 19
Set the Serial Monitor to add a newline character after each message you send
Temp1493950345
Check Your Work
Step 20
Each message now create a newline at the end

This will be really useful in the upcoming lessons where we will want to split multi-character data into separate "chunks"

Temp1493950439
Planning
Step 21
In the next lesson we will be setting up our transmitter so that it automatically connects with our receiver
Working with Serial Data Info

Account

MVCode Clubs

Created By

Newprofile jamie

Course:

Build your own remote controller (advanced)

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach