4CS016 – Embedded Systems
Programming
Lecture 5
Serial Data Communication
Overview
Overview
• What is Serial
• Serial Data and the • What is Serial
Arduino
• Transmitting Serial
• Serial Data and the Arduino
Data from the • Transmitting Serial Data from the Arduino to a COM Port
Arduino to a COM
Port • Reading a sensor
• Reading a sensor
• Storing captured Sensor Data
• Storing captured
Sensor Data • Graph work with Data
• Graph work with
data
1. Serial?
Transferring one bit after the next, rather than sending
multiple ‘bits’ at once, which is parallel.
1. Serial?
1.1 Bits… Bytes
1 0 1 0 1 0 sent to PC
1. 1 Bits… Bytes
A bit is either a 1 or 0 (b)
A Byte is 8 bits (B)
But 1KB is 1024 Bytes, not 1000 Bytes ☹
1. Serial?
1.1 Bits… Bytes
2. Setting up the Serial Port (SP)
• Arduino Sketch has a library (Class) for dealing with the Serial
2. Setting up the Serial
Port(SP)1 & (SP)2 port called “Serial”
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog
Pin Value and Send it via USB
2.1. Setting up the Serial Port (SP) 2
• One method in the class is begin() – This will except
2. Setting up the Serial
either 1 or 2 parameters.
Port(SP)1 & (SP)2 1.Data rate in bps (bits per second)
2.1 Receiving From the Serial
2.Second is for configuration (sets data, parity, and stop
Port bits). We wont be using it in our classes.
2.1.1. Sample Code “print”
1.Data = Number of bits used for data (5 / 6 / 7 / 8)
2.1.2 Sample Code 2.Parity = Sets basic error checking if set to on (non /
“println” odd / even)
2.1.3 Sample Code “write”
3.Stop bits = Number of bits used to end a data string
2.2 Basic Serial Example (1 or 2)
Circuit
• So placing a begin() in the start method in sketch tells
2.3 Reading an Analog the Arduino to open up a serial communication port.
Pin Value and Send it via USB
• Serial.begin(9600, SERIAL_8N1); (default)
https://www.arduino.cc/en/Reference/Serial
https://www.arduino.cc/en/serial/begin
Its worth noting ☺
TX illuminates
2. Setting up the Serial when the Arduino
Port(SP)1 & (SP)2 is transmitting.
2.1 Receiving From the Serial So when we read
Port from the TX pin or
send serial data to
2.1.1. Sample Code “print” the Arduino’s USB
2.1.2 Sample Code port to the outside
“println” world it flashes.
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog
Pin Value and Send it via USB
And…
RX illuminates
2. Setting up the Serial
when receiving
Port(SP)1 & (SP)2 data, such as a
Sketch program
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog
Pin Value and Send it via USB
2.1 Receiving from Serial Port
Serial.print() Serial.println();
2. Setting up the Serial
Port(SP)1 & (SP)2 • Sends ASCII text from • Similar to print, but adds a
2.1 Receiving From the the Arduino to the serial carriage return and line
Serial Port port. feed.
2.1.1. Sample Code “print”
2.1.2 Sample Code Numbers display as numbers unless the second parameter
“println”
is used (this specifies the base)
2.1.3 Sample Code “write”
• Serial.print(78,DEC) = prints 78
2.2 Basic Serial Example • BIN – Binary
Circuit • OCT – Octal
2.3 Reading an Analog
• DEC – Decimal
Pin Value and Send it via USB • HEX – Hexadecimal
• Or values i.e. 0,1,2,3,4 specifies decimal places to display.
http://www.asciitable.com/
2.1.1 Sample Code “print” ☺
2. Setting up the Serial
Port(SP)1 & (SP)2
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog
Pin Value and Send it via USB
2.1.2 Sample Code “println” ☺
2. Setting up the Serial
Port(SP)1 & (SP)2
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog
Pin Value and Send it via USB
2.1.3 Sample Code “write”
2. Setting up the Serial • We use write to output the ascii character of
Port(SP)1 & (SP)2 the number provided.
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
Notice (ascii output of number)
2.3 Reading an Analog Pin
Value and Send it via USB
https://www.arduino.cc/en/Serial/Write
http://www.asciitable.com/
2.2 Basic Serial Example Circuit
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the
serial monitor
This example code is in the public domain.
2. Setting up the Serial */
Port(SP)1 & (SP)2
// digital pin 2 has a pushbutton attached to it. Give it
a name:
2.1 Receiving From the Serial int pushButton = 2;
Port
// the setup routine runs once when you press reset:
2.1.1. Sample Code “print” void setup() {
// initialize serial communication at 9600 bits per
2.1.2 Sample Code second:
“println” Serial.begin(9600);
2.1.3 Sample Code “write” // make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
2.2 Basic Serial Example
// the loop routine runs over and over again forever:
Circuit void loop() {
// read the input pin:
2.3 Reading an Analog But what does it do? int buttonState = digitalRead(pushButton);
// print out the state of the button:
Pin Value and Send it via USB
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
https://www.arduino.cc/en/Tutorial/DigitalReadSerial
2.3 Reading an Analog Pin Value and Send it Via
USB
2. Setting up the Serial
Port(SP)1 & (SP)2
2.1 Receiving From the Serial
Port
2.1.1. Sample Code “print”
2.1.2 Sample Code
“println”
2.1.3 Sample Code “write”
2.2 Basic Serial Example
Circuit
2.3 Reading an Analog But what does it do?
Pin Value and Send it via
USB
https://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
3. Temperature Sensor (LM35[TO-92])
3. Temperature Sensor • According to the data sheet it gives 10mv/oC
3.1 LM35 Circuit • Reads temperatures between 0 and 100oC
• So…
3.2 The Program and
Reading • 0.01 (10mV) x 100 (max oC) = Voltage range of 0 to +1V
• Accuracy ±0.6°C
• Min supply voltage 4V max is 30V
• TO-92 refers to the casing style
• Don’t connect this the wrong way or it will heat up and
burn your fingers and could go pop ☹
http://www.ti.com/lit/ds/symlink/lm35.pdf
3.1 LM35 Circuit
Don’t wire it in
3. Temperature Sensor back to front.
3.1 LM35 Circuit
3.2 The Program and
Reading
3.2. The Programs and Readings
void setup(){
Serial.begin(9600);
3. Temperature Sensor }
3.1 LM35 Circuit void loop(){
float analogueReading =
3.2 The Program and analogRead(A0);
Reading float degreesC;
degreesC =
(analogueReading * 500) /1024;
Serial.print("Temperature is
");
Serial.print(degreesC);
Serial.println(" Centigrade"); Remember Data &
Information?
delay(1000);
}
Data = simple facts or figures
Information = data with structure so it has meaning
4. Using the Data to Plot a Graph
• In the previous example we presented our data as
4.Using the Data to Plot a
Graph
1. Typical CSV File
information, but for it to be usable for plotting on a graph we
2. App Reading com port and
Writes to CSV file
3. CSV File and Excel
ideally need to put it into a CSV File, (Comma Separated
4. Create Excel Data
5. Displaying Real Time Data
Value File)
• A CSV can be read in a spreadsheet like Excel and plotted
on a graph.
4.1 Typical CSV File
4.Using the Data to Plot a
Graph
1. Typical CSV File
2. App Reading com port and
Writes to CSV file
3. CSV File and Excel
4. Create Excel Data
5. Displaying Real Time Data
Opened in Notepad++ Opened in Excel
2 Values
1. Time
Comma
2. Temperature reading.
Point here is…
We can use any Examples
4.Using the Data to Plot a
Graph
software to read the Matlab
1. Typical CSV File com port, and write Processing
2. App Reading com port and
Writes to CSV file the output to a file. PLX-DAQ plug in for
3. CSV File and Excel You cannot read from Excel with Update
4. Create Excel Data
5. Displaying Real Time Data a program and read A program in Java, C
from the com port in or whatever you want.
Sketch at the same
time.
App reads
4.2 Arduino Simply
com port and
Sends reading to a com writes to CSV
4.Using the Data to Plot a
Graph port file
.
1. Typical CSV File int reading = 0;
2. App Reading com port float analogueReading = 0;
and Writes to CSV file float degreesC = 0;
3. CSV File and Excel
4. Create Excel Data void setup(){
5. Displaying Real Time Data Serial.begin(9600);
}
void loop(){
reading = digitalRead(2);
analogueReading = analogRead(0);
degreesC = (analogueReading * 500) /1024;
Serial.println(degreesC);
}
4.3 CSV File and Excel
• Open your Saved CSV file in Excel
• Select the cells
• Click create chart.
• ☺
4.Using the Data to Plot a Graph
1. Typical CSV File
2. App Reading com port and
Writes to CSV file
3. CSV File and Excel
4. Create Excel Data
5. Displaying Real Time Data
4.4 Creates EXCEL data
4.Using the Data to Plot a Graph
1. Typical CSV File
2. App Reading com port and
Writes to CSV file
3. CSV File and Excel
4. Create Excel Data
5. Displaying Real Time Data
4.5 Displaying Real Time Data
• Next week we will see how we can interface a
display to the Arduino to give real time readings on a
LCD Display.
4.Using the Data to Plot a Graph
1. Typical CSV File
2. App Reading com port and
Writes to CSV file
3. CSV File and Excel
4. Create Excel Data
5. Displaying Real Time Data
To Do in the Workshop in Week 6
• Work through the slides today.
• Continue to work through
Workbooks (target workbook 4)
Useful Links
Useful Links http://vanceance.blogspot.co.uk/2012/06/save-file-in-different-p
ath-with.html
(change path of processing)