ESP32 Hello World – Serial Print For Debugging – Arduino

Previous Tutorial Previous Tutorial Tutorial 2 Next Tutorial Next Tutorial
ESP32 Hello World! Serial Port Print – Arduino
ESP32 Course Home Page 🏠

 

In this tutorial, you’ll learn how to use the ESP32 serial print function in Arduino IDE to print our first Hello world example. Then, we’ll discuss how to print numeric variables for debugging Arduino code. And finally, use the ESP32 to send some numeric data over the serial port and plot the data points in Arduino IDE serial plotter. So, let’s get started!

ESP32 Hello World Example - Serial Port Print - ESP32 Serial Plotter Tutorial


Requirements For This Tutorial

Prior Knowledge

  • Nothing

Software Tools

Hardware Components

You can either get the complete course kit for this series of tutorials using the link down below. Or just refer to the table for the exact components to be used in practical LABs for only this specific tutorial.


ESP32 UART Serial Port

 

As we’ve stated earlier in the introductory tutorial, the ESP32 has 3 UART peripherals. One of which is hard-wired to the USB-TTL converter on the ESP32 dev board itself. This is what we’re going to use in this tutorial to send data from the ESP32 to our PC over the UART serial port.

This is a very easy and quick way to debug your Arduino Applications by printing out the current variables’ values to the terminal screen and keep track of what’s going on. And maybe check some flags, logic decisions, etc.

Another use that we’ll also try in this tutorial is the serial plotting feature in the Arduino IDE. We can actually send numeric variables from the ESP32 to the PC and get the value plotted in a graphical interface inside Arduino IDE itself.

ESP32 Serial Port Initialization

First of all, you need to initialize the UART serial port before you’re able to use it for sending/receiving any data. The Arduino built-in library for the serial port doesn’t require you to enter any configuration parameter for the UART peripheral other than the Baud Rate. Which is the rate at which we’ll be sending/receiving serial data.

To initialize (start) the serial port, you need to call the Serial.begin() function and pass the desired baud rate value to it. As shown in the example down below.

This step is typically done in the setup() function before starting the main loop() function. Now, we can send/receive serial data over the UART port using the Arduino API functions shown in the next sections down below.

ESP32 Serial Print Functions

To send data over the serial port, you need to call the Serial.print() function. Which will send the string equivalent of your message byte-by-byte until it’s done. While the Serial.prinln() function will do the exact same thing but it sends a line termination escape sequence “\r\n” at the end of the message.

The code above will print “Hello!” in one line and terminate the line to start a new line for the next message.

ESP32 Serial Read Functions

Before reading serial data, it’s important to check if there is data that has been received already in the buffer or not. And then, we can proceed to read its contents. To check if data has been already received or not, you can use the Serial.available() function.

The example down below shows you how to check the incoming data and echo the received data back to the serial port. So you can see what the ESP32 has already received.

 

If you want to learn more about UART Serial Communication Protocol. And how serial communication works at the low level. You can check that tutorial guide for UART communication. It’s pretty long, but very informative indeed!

 


Components For This Tutorial’s LABs

 

QTY. Component Name Buy Links
1 ESP32 Devkit v1 DOIT Board

or Any Other ESP32 Dev Board

Amazon.com  –  eBay.com  –  Banggood.com
2 BreadBoard Amazon.com –  eBay.com –  Banggood.com
1 Micro USB Cable Amazon.com  –  eBay.com –  Banggood.com

*Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com, Banggood.com. This may be one of the ways to support this free platform while getting your regular electronic parts orders as usual at no extra cost to you.


ESP32 Hello World Example – LAB

 

LAB Number 2
LAB Name Hello World Serial Print
  • Initialize the UART serial port @ the desired Baud Rate
  • Send a “Hello World!” message once a second

ESP32 Hello World Code Example

The delay() function is used in this example just to achieve the timing requirement for this LAB. In order to have a message sent once a second. Note that it’s not an efficient way to get this job done, you’ll be introduced to better techniques later on in this course (series of tutorials).

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

Open the Arduino serial monitor from the icon on the top right or from the tools menu and select Tools > Serial Monitor

Screenshot For The Result

ESP32 Hello World Example Arduino IDE - Serial Port Print Tutorial

 


ESP32 Print String And Variable – LAB

 

LAB Number 3
LAB Name ESP32 Serial Print String & Numeric Variable
  • Initialize the UART serial port @ the desired Baud Rate
  • Create a counter variable
  • Send a message showing the counter variable’s value (once a second)

ESP32 Printing a String + Numeric Variable

The numeric variable (Counter) will be evaluated and converted to a string and get printed after the “Counter Value = ” string. The counter is incremented by 1, then the ESP32 executes a delay for 1 second, and repeat!

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

Screenshot For The Result

ESP32 Hello World Example Arduino IDE - Serial Print String And Variable

 


ESP32 Serial Plotter Example – LAB

 

LAB Number 4
LAB Name ESP32 Serial Plotter
  • Initialize the UART serial port @ the desired Baud Rate
  • Create a lookup table (Array)
  • Send a message containing the current value in the lookup table

ESP32 Serial Plotter in Arduino

The code example down below defines a sine waveform lookup table consisting of 32 values (Array of integers). Then, we’ll send those data points one-by-one over the UART serial print and use the Arduino Serial Plotter to plot the incoming data points. Which should be a, roughly looking, sinusoidal waveform.

Choose the board, COM port, hold down the BOOT button, click upload and keep your finger on the BOOT button pressed. When the Arduino IDE starts sending the code, you can release the button and wait for the flashing process to be completed. Now, the ESP32 is flashed with the new firmware.

Demo Video For The Result

 


Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!

 

Stay tuned for the upcoming tutorials and don’t forget to SHARE these tutorials. And consider SUPPORTING this work to keep publishing free content just like this!

 

PCBgogo Ad

 

ESP32 Course Home Page 🏠 
Previous Tutorial Previous Tutorial Tutorial 2 Next Tutorial Next Tutorial
Share This Page With Your Network!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

Leave a Reply

%d bloggers like this: