WEEK 8 - Serial Communications
The Basics of Asynchronous Serial Communication
Serial communication is a way to make computers talk to each other, like using the Arduino and a laptop over USB(universal serial bus). It can also be used with computers and LCD screens, touch sensor chips, GPS receivers, Bluetooth radio, etc. When two computers need to work together, there are things that they need to agree on since each of them is running their own times. There are three lines that connect each other.
1. Electrical Agreement
- Receive line (RX) from the Arduino side that is attached to a computer’s Transmit line(TX) and vise versa (the Arduino’s transmit to the laptop‘s receive.
- Need to share a common ground
2. Share the same voltage levels
- Since they are connected to each other they also need to share the same voltages. For example, when the Arduino sends out at 5(V), the computer should be able to receive at 5(V).
3. Timing communication/speed
- Both computers need to agree on when they are going to listen. For example, to transmit at 9600 bits per second, means that Arduino is gonna send a bit of information every 9600 of a second and the computer is gonna read every 9600th of a second. Then the Arduino will set its Transmit line eighter high or low, and the computer needs to know how frequently to sample so that it’s sampling one time per bit of information.
4. Logic or what do our pulses of high and low mean?
- When we are using normal or non-inverted logic, that are low voltage(0) and high volatage(1) which is a binary representation of a number that the way of a computer stores information.
Interpreting Serial Data
Serial data is passed byte by byte from one device to another, but it’s up to you to decide how each device (computer or microcontroller) should interpret those bytes, when the beginning of a message is, when the end is, and what to do with the bytes in between. Serial communication protocols define the structure of communication between devices. These notes explain how serial data is interpreted by computers.
Every value you send can range from 0 to 255, the full range that can fit in a byte. A protocol like this is often called a binary protocol, because there’s no other meaning to each byte’s value other than the value of the number itself.
p5.serialport and p5.webserial Compared
“Asynchronous serial communication is not part of the core of p5.js, nor is it part of the core for web browsers. To read serial in a p5.js sketch, or in a web page in general, you need some help. One approach is to open a separate application that connects to the serial port and offers a webSocket connection to the browser. This is the approach that the p5.serialport library and the p5.serialcontrol app take. Another approach is to use the browser API called WebSerial. This is the approach that the p5.webserial library takes.”
“The difference between them is best illustrated by Figures 1 and 2 below. With p5.seriaport, you must open another application on your computer, p5.serialcontrol, to communicate with the serial port. It is this application that handles serial communication, not the p5.js sketch in the browser. This can be more complicated for beginning users.”
Lab: Intro to Asynchronous Serial Communications
A Serial Output Sketch
Connecting via the POSIX Command Line
ASCII vs. Binary: What are you sending?
“The number is up to 1023 on the serial monitor because because
Serial.println()formats the value it prints as an ASCII-encoded decimal number, with a linefeed at a carriage return at the end. To send the value 1023, for example,
println()sents six bytes: the characters 1, 0, 2, and 3, and a carriage return byte and a newline byte. Meanwhile, the serial monitor, receiving the data, assumes it should show you the ASCII character corresponding to each byte it receives.”
The output range from 1023 to 255.
The output range from 500 to 255.
Send the data in many formats
Showing the raw binary value, the ASCII-encoded binary value and the ASCII-encoded decimal, hexadecimal, and octal values.
What’s T, R and N?
“The ASCII table contains several characters that are “invisible”, like tab, newline, carriage return, and so forth. These characters tell the receiving program how to format the visible characters on the screen; newline tells the receiver to move down a line; carriage return tells it to move to the left edge of the screen, and so forth. In most programming languages, these characters are denoted with an escape string starting with a backslash: . For example, newline is n. Carriage return is r. Tab is t. When you see an escape string in a code sample, replace it with the appropriate ASCII value in your mind.”
Send the values for all three sensors