Arduino Serial

Posted on  by admin

Every Arduino board has at least one serial port, and many have additional built-in serial ports (Serial1, Serial2, etc.). Other boards, such as the Leonardo, also have serial communication in the form of a USB) port. In USB-enabled boards, we refer to the USB as Serial. Serial data transfer is when we transfer data one bit at a time, one right after the other. Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. Just like we used that technique to turn an LED on and off, we can.

Contents

  • 3 How to open Arduino Serial Monitor
  • 4 Arduino serial commands and syntax

About Arduino Serial Print

Arduino Serial print is an inbuilt function. which allows the computer to communicate with the Arduino board having the following syntax

It uses SPI (Serial Peripheral Interface) protocol to exchange the data between the computer and Arduino. Also, it converts the raw data in ASCII (American Standard Code of Information Interchange). So humans can easily read it.

Understand this with a real-life situation. Suppose, you have a temperature sensor and you want to know the temperature of your surroundings. Then, a serial print Arduino function comes into the picture. It allows users to read the real-time temperature value and convert it into a human-readable format and display.

Material required

  • Arduino Board
  • Programing cable

How to open Arduino Serial Monitor

Make sure your Arduino board is connected to your computer.

The easiest way to open Arduino Serial monitor is to click on the magnifier icon. The second method is press Ctrl+Shift+M. The third way is to go to Tools>Serial Monitor

Arduino serial plotter

Baud Rate

First of all, you have to define the baud rate on which you want to communicate. Baud rates supported by Arduino range from 300 to 2000000, depending on the application.

Arduino Serial

Always select same baud rate you define in code

Arduino serial set address

Arduino serial commands and syntax

Arduino Serial Begin

Serial begin is the initiation to set the baud rate in which you want to communicate.

Arduino commands to print on Serial Monitor

You can print anything using the following functions-

Serial Print (456); Output : 456

Arduino serial Print New Line

Using this function you can print a new value in a new line that will make your output more sensible and easy to read.

Arduino Serial Read

Arduino Serial read command is used to read the data coming from any other device. Here, I’m using a python script to communicate with Arduino I use the serial read command to read data coming from the computer.

All serial commands

Here is an example code to read the temperature and print it on the serial monitor.

FAQ

Serial print not working ?

Choose correct port, Board and then try again. Also make sure Arduino is connected to your computer.

Serial write vs print

Serial write is use to send data and Serial print is use to print on serial monitor.

Arduino Serial Port

  • Arduino Tutorial
  • Arduino Function Libraries
  • Arduino Advanced
  • Arduino Projects
  • Arduino Sensors
  • Motor Control
  • Arduino And Sound
  • Arduino Useful Resources
  • Selected Reading

A Serial Peripheral Interface (SPI) bus is a system for serial communication, which uses up to four conductors, commonly three. One conductor is used for data receiving, one for data sending, one for synchronization and one alternatively for selecting a device to communicate with. It is a full duplex connection, which means that the data is sent and received simultaneously. The maximum baud rate is higher than that in the I2C communication system.

Board SPI Pins

SPI uses the following four wires −

  • SCK − This is the serial clock driven by the master.

  • MOSI − This is the master output / slave input driven by the master.

  • MISO − This is the master input / slave output driven by the master.

  • SS − This is the slave-selection wire.

The following functions are used. You have to include the SPI.h.

  • SPI.begin() − Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.

  • SPI.setClockDivider(divider) − To set the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter of the frequency of the system clock (5 Mhz for the boards at 20 MHz).

  • Divider − It could be (SPI_CLOCK_DIV2, SPI_CLOCK_DIV4, SPI_CLOCK_DIV8, SPI_CLOCK_DIV16, SPI_CLOCK_DIV32, SPI_CLOCK_DIV64, SPI_CLOCK_DIV128).

  • SPI.transfer(val) − SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal.

  • SPI.beginTransaction(SPISettings(speedMaximum, dataOrder, dataMode)) − speedMaximum is the clock, dataOrder(MSBFIRST or LSBFIRST), dataMode(SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3).

We have four modes of operation in SPI as follows −

  • Mode 0 (the default) − Clock is normally low (CPOL = 0), and the data is sampled on the transition from low to high (leading edge) (CPHA = 0).

  • Mode 1 − Clock is normally low (CPOL = 0), and the data is sampled on the transition from high to low (trailing edge) (CPHA = 1).

  • Mode 2 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from high to low (leading edge) (CPHA = 0).

  • Mode 3 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from low to high (trailing edge) (CPHA = 1).

  • SPI.attachInterrupt(handler) − Function to be called when a slave device receives data from the master.

Now, we will connect two Arduino UNO boards together; one as a master and the other as a slave.

  • (SS) : pin 10
  • (MOSI) : pin 11
  • (MISO) : pin 12
  • (SCK) : pin 13

The ground is common. Following is the diagrammatic representation of the connection between both the boards −

Let us see examples of SPI as Master and SPI as Slave.

SPI as MASTER

Example

Arduino Serial Plotter

SPI as SLAVE

Arduino Serial Read

Example