Warning
Because serial communication is shared with code upload, you will have to close the serial communication tab before you can upload your code again.
Get a piece of information and display it
Ultrasonic sensor and serial link
This page guides you through using the serial link and the ultrasonic sensor.
A serial link is a communication protocol for exchanging data between two devices by sending the information bit by bit over a single data line. It is widely used in embedded systems, in communication between microcontrollers and computers, and in many electronic devices.
Unlike a parallel link, which sends several bits at once over several lines, a serial link sends the data sequentially, which reduces the number of wires needed and allows transmission over longer distances.
Serial communication relies on sending and receiving data serially, according to a defined protocol. The key parameters of a serial link are:
In general, a serial link uses two main wires:
On XIAO-type and other ESP32 modules, the serial link can go through the USB cable. This USB connection relies on a built-in serial-to-USB converter, which turns a conventional serial link into USB. Using this converter sometimes requires installing specific drivers on the computer.
In addition, this serial link is generally used to upload programs to the board from an IDE such as the Arduino IDE, which makes development and debugging easier.
On platforms such as the Arduino or the ESP32, serial communication is handled by a hardware or software serial port. Here is how to send information over a serial link using the Arduino IDE.
void setup() {
Serial.begin(9600); // Initialise the serial link at 9600 baud
Serial.println("Starting serial communication");
}
void loop() {
Serial.println("Hello, ESP32!"); // Sends a message over the serial link
delay(1000); // Wait 1 second before sending again
}
Serial.begin(9600); → Initialises the serial link at 9600 baud.Serial.println("Starting serial communication"); → Sends an initialisation message.Serial.println("Hello, ESP32!"); → Sends a message every second.delay(1000); → Adds a one-second delay before the next message.With the serial monitor of the Arduino IDE, you can see the messages sent by the board and use them for debugging and for communicating with other devices.
To do so, click Serial Monitor at the top right
. A tab opens below your window, next to the Output tab, showing what your board sends.
The serial link is a simple, effective way to exchange data between electronic components, and is easy to set up on platforms such as the Arduino and the ESP32. We will now use it to send information from the ultrasonic sensor.
Ultrasonic sensors are devices commonly used to measure distances by sending sound pulses and measuring the time they take to return after bouncing off an obstacle. They are very popular in robotics and obstacle detection applications.
An ultrasonic sensor works on the principle of echolocation:
With the following formula, the distance of the object can be calculated from the travel time and the speed of sound in air:
Distance = (Round-trip time × Speed of sound) / 2
Where the speed of sound in air is about 343 m/s at room temperature.
We use the following pins to connect our ultrasonic sensor:
const int trig_pin = D2;
const int echo_pin = D1;
To measure the distance, we follow these steps:
Here is example code that carries out these steps:
const int trig_pin = D2;
const int echo_pin = D1;
void setup() {
Serial.begin(9600);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
}
void loop() {
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
long duration = pulseIn(echo_pin, HIGH);
float distance = (duration * 0.0343) / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
pulseIn() function measures how long the echo is received.This solution detects objects and gives their distance accurately.