Arduino Serial
Francesco de Palma
[email protected]
Serial Communications
• Microcontrollers must often exchange data with other microcontrollers or peripheral devices.
• Data may be exchanged by using parallel or serial techniques.
• With parallel techniques, an entire byte of data is typically sent simultaneously from the
transmitting device to the receiver device.
• While this is efficient from a time point of view, it requires eight separate lines for the data
transfer (for a byte of 8 bits).
• In serial transmission, a byte of data is sent a single bit at a time. Once eight bits have been
received at the receiver, the data byte is reconstructed.
• While this is inefficient from a time point of view, it only requires a line (or two) to transmit the
data.
• The ATmega328 (UNO R3) are equipped with a host of different serial communication subsystems
including:
• the serial USART,
• the serial peripheral interface or SPI,
• and the Two–wire Serial Interface (TWI).
F. de Palma 2
Serial Communication Terminology
• Asynchronous versus Synchronous Serial Transmission: In serial communications, the
transmitting and receiving device must be synchronized to one another and use a common data
rate and protocol.
• In an asynchronous serial communication system, such as the USART aboard the ATmega328,
framing bits are used at the beginning and end of a data byte.
• These framing bits alert the receiver that an incoming data byte has arrived and also signals the
completion of the data byte reception.
• The data rate for an asynchronous serial system is typically much slower than the synchronous
system, but it only requires a single wire between the transmitter and receiver.
• Synchronization allows both the transmitter and receiver to be expecting data
transmission/reception at the same time.
• A synchronous serial communication system maintains “sync” between the transmitter and
receiver by employing a common clock between the two devices. Data bits are sent and received
on the edge of the clock.
• This allows data transfer rates higher than with asynchronous techniques but requires two lines,
data and clock, to connect the receiver and transmitter.
F. de Palma 3
Wiki:
Serial Communication Terminology
• Baud rate: Data transmission rates are typically specified as a Baud or bits per second rate. For
example, 9600 Baud indicates the data is being transferred at 9600 bits per second.
• Full Duplex: Often serial communication systems must both transmit and receive data.
• To do both transmission and reception, simultaneously, requires separate hardware for
transmission and reception. A single duplex system has a single complement of hardware that
must be switched from transmission to reception configuration.
• A full duplex serial communication system has separate hardware for transmission and reception.
• Non–return to Zero (NRZ) Coding Format: There are many different coding standards used within
serial communications.
• The important point is the transmitter and receiver must use a common coding standard so data
may be interpreted correctly at the receiving end.
• The Atmel ATmega328 use a non–return to zero (NRZ) coding standard.
• In NRZ coding a logic one is signaled by a logic high during the entire time slot allocated for a
single bit; whereas, a logic zero is signaled by a logic low during the entire time slot allocated for a
single bit.
F. de Palma 4
Serial Communication Terminology
• The RS–232 Communication Protocol: When serial transmission occurs
over a long distance additional techniques may be used to insure data
integrity.
• Over long distances logic levels degrade and may be corrupted by noise.
• At the receiving end, it is difficult to discern a logic high from a logic low.
• With the RS–232 standard (EIA–232), a logic one is represented with a –12
VDC level while a logic zero is represented by a +12 VDC level.
• Chips are commonly available (e.g.,MAX232) that convert the 5 and 0 V
output levels from a transmitter to RS–232 compatible levels and convert
back to 5V and 0 V levels at the receiver.
• The RS–232 standard also specifies other features for this communication
protocol.
F. de Palma 5
Serial Communication Terminology
• Parity: To further enhance data integrity during transmission, parity techniques
may be used.
• Parity is an additional bit (or bits) that may be transmitted with the data byte.
• The ATmega328 employ a single parity bit.
• With a single parity bit, a single bit error may be detected.
• Parity may be even or odd.
• In even parity, the parity bit is set to one or zero such that the number of ones in
the data byte including the parity bit is even.
• In odd parity, the parity bit is set to one or zero such that the number of ones in
the data byte including the parity bit is odd.
• At the receiver, the number of bits within a data byte including the parity bit are
counted to ensure that parity has not changed, indicating an error, during
transmission
F. de Palma 6
Serial Communication Terminology
• ASCII: The American Standard Code
for Information Interchange or ASCII is
a standardized, 7-bit method of
encoding alphanumeric data.
• It has been in use for many decades,
and it is still the most common
method of encoding alphanumeric
data.
• For example, the capital letter “G” is
encoded in ASCII as 0x47.
• The “0x” symbol indicates the
hexadecimal number representation.
F. de Palma 7
Serial Communication Terminology
• Unicode is the international counterpart of
ASCII. It provides standardized 16–bit
encoding format for the written languages
of the world (Unicode 15.0 has a total of
149,186 characters www.unicode.org).
• ASCII is a subset of Unicode.
• ASCII reserves the first 32 codes (numbers
0–31 decimal) for control characters: codes
originally intended not to represent
printable information, but rather to control
devices (such as printers) that make use of
ASCII, or to provide meta-information
about data streams such as those stored
on magnetic tape
F. de Palma 8
UART Frame format
• The UART data transmission line is normally held at a high voltage level
when it’s not transmitting data.
• To start the transfer of data, the transmitting UART pulls the transmission
line from high to low for one clock cycle.
• When the receiving UART detects the high to low voltage transition, it
begins reading the bits in the data frame at the frequency of the baud rate.
• The data frame contains the actual data being transferred. It can be 5 bits
up to 8 bits long if a parity bit is used.
• If no parity bit is used, the data frame can be 9 bits long.
• In most cases, the data is sent with the least significant bit first. (LSB->MSB)
https://www.rohde-schwarz.com/it/prodotti/misura-e-collaudo/oscilloscopi/educational-content/comprensione-uart_254524.html
F. de Palma 9
UART Frame format
example
• If we want to send the «S» in 7 bit ASCII (0x53 hexadecimal)
binary = 1 0 1 0 0 1 1
• Inverting it with LSB first we get: 1 1 0 0 1 0 1
• The binary number has 3 «0»s and 4 «1»s
• With even parity, the parity bit is 0, since the number of ones in the data
byte including the parity bit is even.
• With odd parity, the parity bit is 1, since the number of ones in the data
byte including the parity bit is odd.
• The stop bit is used to end the frame and bring back the level to inactivity
state.
• The parity bit can detect only a single flipped bit. If more than one bit is
flipped, it is impossible to sense them using a single parity bit.
https://www.rohde-schwarz.com/it/prodotti/misura-e-collaudo/oscilloscopi/educational-content/comprensione-uart_254524.html
F. de Palma 10
Arduino UNO R3 USART
• The Arduino UNO R3 provides access to the USART transmission (TX) and reception (RX) pins via
DIGITAL pins 1 (TX) and 0 (RX).
• The USART may be connected to a an external USART compatible input device, output device, or
microcontroller.
• The Arduino UNO R3 may also communicate with the host personal computer (PC) via the USB
cable.
• The Arduino processor pins are configured for TTL (Transistor-transistor logic) compatible inputs
and outputs. That is, logic highs and lows are represented with 5 VDC and 0 VDC signals,
respectively.
• The TX and RX pins are not compatible with RS–232 signals. A level shifter such as the MAX232 is
required between the Arduino processor and the RS–232 device for communications of this type.
• When connected to a PC via the USB cable, appropriate level shifting is accomplished via the USB
support chip onboard the Arduino processing board.
• The Arduino Development Environment commands to provide USART communications
https://www.circuitbasics.com/how-to-set-up-uart-communication-for-arduino/
F. de Palma 11
USB-to-TTL Serial chip
• To communicate with the computer, the
Arduino relies on a USB-TTL interface.
• In UNO, ATMega16U2 with custom
firmware act as a USB – TTL interface chip.
• An USB 2.0 Cable Type A/B could be used to
connect Arduino to the PC
• This could be used to both program Arduino
and communicate with it (input and output)
F. de Palma 12
https://www.arduino.cc/reference/en/language/functions/communication/serial/
Arduino IDE functions
Command Description
Serial.begin() Sets Baud rate
Disables serial communication. Allows Digital 1(TX) and
Serial.end() Digital (0) RX to be used for digital input and output.
Determines how many bytes have already been received and
Serial.available() stored in the 128 byte buffer.
Serial.read() Reads incoming serial data.
Clears the buffer once all outgoing characters have been
Serial.flush() sent.
Prints data to the serial port as ASCII text. An optional second
parameters pecifies the format for printing (BYTE, BIN, OCT,
Serial.print() DEC, HEX).
Prints data to the serial port as ASCII text followed by a
Serial.println() carriage return.
Writes binary data to the serial port. A single byte, a series of
F. de Palma 13
Serial.write() bytes, or an array of bytes may be sent.
Serial plotter and monitor!
• https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-serial-plotter
• (before without potentiometer, then this one:
https://www.arduino.cc/en/Tutorial/BuiltInExamples/ReadAnalogVoltage )
• https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-serial-monitor
• Use the serial monitor as input: https://www.circuitbasics.com/how-to-
read-user-input-from-the-arduino-serial-monitor/
• For the String example remember to use «no line ending»
• Check also: https://docs.arduino.cc/built-in-
examples/strings/StringComparisonOperators
F. de Palma 14
IDE examples:
• Serial monitor input example
• https://docs.arduino.cc/built-in-
examples/communication/ReadASCIIString
• Ascii table:
• https://docs.arduino.cc/built-in-examples/communication/ASCIITable
F. de Palma 15
Uart comunication example
• https://www.circuitbasics.com/how-to-set-up-uart-communication-
for-arduino/
F. de Palma 16
SERIAL PERIPHERAL INTERFACE–SPI
• The ATmega Serial Peripheral Interface or SPI also provides for two–way
serial communication between a transmitter and a receiver.
• In the SPI system, the transmitter and receiver share a common clock
source.
• This requires an additional clock line between the transmitter and receiver
but allows for higher data transmission rates as compared to the USART.
• The SPI system allows for fast and efficient data exchange between
microcontrollers or peripheral devices.
• There are many SPI compatible external systems available to extend the
features of the microcontroller (liquid crystal display or a digital–to–analog
converter)
F. de Palma 17
SPI
• SPI interface was found by Motorola in 1970.
• SPI has a full-duplex connection, which means that the data is sent
and received simultaneously. A master can send data to a slave and a
slave can send data to the master simultaneously.
• SPI is synchronous serial communication means the clock is required
for communication purposes.
• A SPI has a master/Slave communication by using four lines.
• A SPI can have only one master and can have multiple slaves.
• A master is usually a microcontroller and the slaves can be a
microcontroller, sensors, ADC, DAC, LCD etc.
F. de Palma 18
SPI
• The SPI may be viewed as a synchronous 16–bit shift register with an 8–bit
half residing in the transmitter and the other 8–bit half residing in the
receiver.
• The transmitter is designated the master since it is providing the
synchronizing clock source between the transmitter and the receiver. The
receiver is designated as the slave.
• A slave is chosen for reception by taking its Slave Select (SS) line low.
• When the SS line is taken low, the slave’s shifting capability is enabled.
• SPI transmission is initiated by loading a data byte into the master
configured SPI Data Register (SPDR).
• At that time, the SPI clock generator provides clock pulses to the master
and also to the slave via the SCK pin.
F. de Palma 19
SPI
• A single bit is shifted out of the master designated shift register on the
Master Out Slave In (MOSI) microcontroller pin on every SCK pulse.
• The data is received at the MOSI pin of the slave designated device.
• At the same time, a single bit is shifted out of the Master In Slave Out
(MISO) pin of the slave device and into the MISO pin of the master device.
• After eight master SCK clock pulses, a byte of data has been exchanged
between the master and slave designated SPI devices.
• Completion of data transmission in the master and data reception in the
slave is signaled by the SPI Interrupt Flag (SPIF) in both devices.
• The SPIF flag is located in the SPI Status Register (SPSR) of each device. At
that time, another data byte may be transmitted.
F. de Palma 20
Multiple Slaves
• To start communication between master and slave we need to set the required
device's Slave Select (SS) pin to LOW, so that it can communicate with the master.
• When it's high, it ignores the master.
• This allows you to have multiple SPI devices sharing the same MISO, MOSI, and
CLK lines of master.
• In the above image there are four slaves in which the SCLK, MISO, MOSI are
common connected to master and the SS of each slave is connected separately to
individual SS pins (SS1, SS2, SS3) of master.
• By setting the required SS pin LOW a master can communicate with that slave.
F. de Palma 21
SPI pins in arduino
SPI Line Pin in Arduino
MOSI 11 or ICSP-4
MISO 12 or ICSP-1
SCK 13 or ICSP-3
SS 10
F. de Palma 22
SPI
• The difficult part about SPI is that the standard is loose, and each device
implements it a little differently.
• This means you must pay special attention to the datasheet when writing
your interface code.
• There are three modes of transmission numbered 0 - 3. These modes
control whether data is shifted in and out on the rising or falling edge of
the data clock signal, and whether the clock is idle when high or low.
• All SPI settings are determined by the Arduino SPI Control Register (SPCR).
A register is just a byte of microcontroller memory that can be read from or
written to.
• Registers generally serve three purposes, control, data and status.
https://docs.arduino.cc/tutorials/generic/introduction-to-the-serial-peripheral-interface
F. de Palma 23
SPI
• Control registers code control settings for various microcontroller
functionalities. Usually, each bit in a control register effects a particular
setting, such as speed or polarity.
• Data registers simply hold bytes. For example, the SPI data register (SPDR)
holds the byte which is about to be shifted out the MOSI line, and the data
which has just been shifted in the MISO line.
• Status registers change their state based on various microcontroller
conditions. For example, the seventh bit of the SPI status register (SPSR)
gets set to 1 when a value is shifted in or out of the SPI.
• The SPI control register (SPCR) has 8 bits, each of which control a particular
SPI setting.
https://docs.arduino.cc/tutorials/generic/introduction-to-the-serial-peripheral-interface
F. de Palma 24
SPI
7 6 5 4 3 2 1 0
• SPCR
SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
• SPIE - Enables the SPI interrupt when 1
• SPE - Enables the SPI when 1
• DORD - Sends data least Significant Bit First when 1, most Significant Bit first
when 0
• MSTR - Sets the Arduino in controller mode when 1, peripheral mode when 0
• CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
• CPHA - Samples data on the falling edge of the data clock when 1, rising edge
when 0
• SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
F. de Palma 25
https://www.arduino.cc/reference/en/language/functions/com
SPI Library munication/spi/
• This library allows you to communicate with SPI devices, with the
Arduino as the controller device.
• To use this library: #include <SPI.h>
Command Description
SPISettings The SPISettings object is used to configure the SPI port for your SPI device.
Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK
begin() and MOSI low, and SS high.
end() Disables the SPI bus (leaving pin modes unchanged).
To Set the SPI clock divider relative to the system clock. The available
setClockDivider() dividers are 2, 4, 8, 16, 32, 64 or 128.
this function is used to simultaneous send and receive the data between
transfer() master and slave. F. de Palma 26
SPI master & slave example
• https://circuitdigest.com/microcontroll
er-projects/arduino-spi-
communication-tutorial
F. de Palma 27
TWO–WIRE SERIAL INTERFACE–TWI /I2C
• The TWI/I2C subsystem allows the system designer to network several related devices (microcontrollers,
transducers, displays, memory storage, etc.) together into a system using a two-wire interconnecting
scheme.
• The TWI allows a maximum of 128 devices to be connected.
• Each device has its own unique address and may both transmit and receive over the two-wire bus at
frequencies up to 400 kHz.
• This allows the device to freely exchange information with other devices in the network within a small area.
• The TWI system consists of a two wire 100k bps (bit per second) bus.
• The 100k bps bus speed is termed the standard mode, but the bus may also operate at higher data rates.
• There are multiple TWI compatible peripheral components (e.g. LCD displays, sensors, etc.).
• The TWI system uses a standard protocol to allow the nodes to send and receive data from the other
devices.
• All nodes on the bus are assigned a unique 7–bit address.
• The eighth bit of the address register is used to specify the operation to be performed (read or write).
• Additional devices may be added to the TWI based system as it evolves.
F. de Palma 28
TWO–WIRE SERIAL INTERFACE–TWI /I2C
• The two-wire bus consists of the
serial clock line (SCL) and the serial
data line (SDA).
• These lines are pulled up to logic
high by the SCL and the SDA pull up
resistors.
• Nodes connected to the bus can
drive either of the bus lines to
ground (logic 0).
• Devices within a TWI bus
configuration must share a
common ground.
F. de Palma 29
I2C pins on Arduino UNO
• For Arduino Uno the I2C pins are D18, D19
F. de Palma 30
https://www.arduino.cc/reference/en/language/functions/communication/wire/
Wire Library
• The Wire Library of the Arduino IDE provides support for the TWI system. It allows
communication between the Arduino processing board and TWI/I2C compatible devices.
• To use this library: #include <Wire.h>
Command Description
begin() This function initializes the Wire library and join the I2C bus as a controller or a peripheral
end() Disable the Wire library, reversing the effect of Wire.begin().
requestFrom() This function is used by the controller device to request bytes from a peripheral device
beginTransmission() This function begins a transmission to the I2C peripheral device with the given address.
This function ends a transmission to a peripheral device that was begun
endTransmission() by beginTransmission() and transmits the bytes that were queued by write().
This function writes data from a peripheral device in response to a request from a controller
device, or queues bytes for transmission from a controller to peripheral device (in-between calls
write() to beginTransmission() and endTransmission()).
available() This function returns the number of bytes available for retrieval with read()
This function reads a byte that was transmitted from a peripheral device to a controller device
read() after a call to requestFrom() or wasF. de
transmitted
Palma from a controller device to a peripheral device.31
TWI/I2C example
• Master & Slave (string comunication)
• https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterWriter
• Master & Slave (led):
• https://create.arduino.cc/projecthub/PIYUSH_K_SINGH/master-slave-i2c-
connection-f1aa53
• Master and slave (text to and from master)
https://www.instructables.com/Arduino-I2C-and-Multiple-Slaves/
• Digital potentiometer & Ultra-Sonic Range Finder (we will see if we have
them)
• https://docs.arduino.cc/learn/communication/wire
F. de Palma 32
Stream class IDE
• Stream is the base class for character and binary based streams. It is not called
directly but invoked whenever you use a function that relies on it.
• Stream defines the reading functions in Arduino.
• When using any core functionality that uses a read() or similar method, you can
safely assume it calls on the Stream class.
• For functions like print(), Stream inherits from the Print class.
• Some of the libraries that rely on Stream include:
• Serial
• Wire
• Ethernet
• SD
• https://www.arduino.cc/reference/en/language/functions/communication/strea
m/
F. de Palma 33
IDE example with additional software
• With processing (or python!)
• Change led luminosity from PC
• https://docs.arduino.cc/built-in-examples/communication/Dimmer
• Read potenziometer position with a PC:
• https://docs.arduino.cc/built-in-examples/communication/Graph
• Turn on and off the led using processing (mouse position):
• https://docs.arduino.cc/built-in-
examples/communication/PhysicalPixel
F. de Palma 34
IDE example with additional software
• Shows a changing plot related to the analog signal of two analog
detectors and a switch (digital), with handshake
• https://docs.arduino.cc/built-in-
examples/communication/SerialCallResponse
• Similar with ASCII encoding (not binary as before, less limitations on
the value)
• https://docs.arduino.cc/built-in-
examples/communication/SerialCallResponseASCII
F. de Palma 35