L6: The serial port

Communication between the development board and the computer through the serial port.

The serial port

What you will learn in this lesson

In this lesson, we will cover:

  • What a serial communication port is.
  • How to use it to send information to the development board and receive information from it.
  • How to use it to monitor and debug our programs.
  • Using variables.

Prerequisites

To follow this lesson, you should have already completed Lesson 1: My first project, Lesson 2: The LED diode, Lesson 3: The LED diode (2), Lesson 4: Variable-brightness LED and Lesson 5: The RGB LED. In this lesson, we will not add a new electronic component: we will learn to communicate with the board from the computer.

Theoretical explanation

I want to know more!

What is the serial port?

The serial port provides a way for the development board and the computer to communicate. In our case, we will use the USB connection that we already use to program the board.

Until now, we have looked at an LED to see whether a program was working:

  • If it blinked, the program seemed to be working.
  • If its brightness changed, we knew that the brightness value was changing.
  • If its colour changed, we knew that the RGB LED was receiving new commands.

But sometimes we want to see more specific information. For example:

  • What number a variable holds.
  • Which step of the program we have reached.
  • Whether the board has received a message.
  • What value a sensor has read.

This is what we use the COM monitor for.

The COM monitor

The COM monitor is a window in the BatSTEM Cloud environment that displays messages sent by the board. It also lets you type text on the computer for the board to receive.

We can think of it as a conversation:

  • The board writes messages to the computer.
  • The computer displays those messages.
  • We can also write messages from the computer.
  • The board can read those messages and respond.

Communication speed

Before sending messages through the serial port, the board and the computer must use the same communication speed. This speed is measured in bps (bits per second).

The BatSTEM Cloud COM monitor always operates at 115200 bps, so we must configure the program to use 115200 bps too.

Variables

A variable is a named space where the program stores a value. That value can change while the program is running.

For example, we can create a variable called Counter with an initial value of 0. Then, each time the main loop repeats, we can increase its value:

  • At the start: Counter = 0.
  • After one loop: Counter = 1.
  • After two loops: Counter = 2.
  • After three loops: Counter = 3.

If we send the variable’s value to the COM monitor, we can see how it changes over time.

Practical part

You will need the following materials:

  • ESP32 development board.
  • BatSTEM expansion board.
  • USB connection cable.
  • Computer with access to BatSTEM Cloud.

Wiring diagram

In this lesson, we only need to connect the development board to the computer using the USB cable. This connection will let us program the board and communicate with it through the COM monitor.

Step by step
  1. Place the ESP32 development board on the BatSTEM expansion board.
  2. Connect the USB cable to the development board.
  3. Connect the other end of the USB cable to the computer.
  4. Open BatSTEM Cloud.
  5. When you run a sketch, open the COM Mon. panel to see the messages sent by the board.

Code

We will create three sketches. First, we will send a simple message. Then, we will use a counter variable. Finally, we will make the board respond when it receives text from the computer.

Sending a message to the COM monitor

Build and run the following sketch:

  1. In the setup block, add the “configure COM port” block.
  2. Select a speed of 115200 bps.
  3. In the main loop, add a “write … to COM port” block.
  4. Enter the text “Hello from BatSTEM”.
  5. Keep the option to send a new line enabled.
  6. Add a wait of 1 second.
  7. Run the sketch and open the COM monitor.
Step by step
  1. Click the Communication category.
  2. Drag the “configure COM port at 115200 bps” block into setup.
  3. Drag a “write … to COM port” block into the main loop.
  4. Add a text block containing the message “Hello from BatSTEM”.
  5. Leave send new line checked so that each message appears on a new line.
  6. Click Time and add a wait of 1 second.
  7. Click Run.
  8. Open the COM monitor and watch the message appear once per second.

Sending the value of a variable

Now we will use a variable to count how many times the main loop has repeated.

Build and run this sketch:

  1. In setup, configure the COM port at 115200 bps.
  2. In setup, create a variable with name Counter and an initial value of 0.
  3. In the main loop, write the value of Counter to the COM port.
  4. Add a wait of 1 second.
  5. Change Counter by 1.
Step by step
  1. Keep the “configure COM port at 115200 bps” block inside setup.
  2. Click the Data category.
  3. Drag the “variable with name” block into setup.
  4. Enter Counter as the variable’s name.
  5. Set its initial value to 0.
  6. In the main loop, add a “write … to COM port” block.
  7. Instead of entering fixed text, insert the block that reads the Counter variable.
  8. Add a wait of 1 second.
  9. Add the “change Counter by 1” block.
  10. Run the sketch and watch the number increase in the COM monitor.

Reading text from the COM monitor

We can also send text from the computer to the board. In this sketch, the board will check whether data is available. If there is text, it will read it and write it back to the COM monitor.

Step by step
  1. Configure the COM port at 115200 bps in the setup block.
  2. Create a variable called Message with an initial value of "".
  3. In the main loop, add an “if” block from the Logic category.
  4. Use the “data available on COM port” block as the condition.
  5. Inside the “if” block, set the Message variable to the value of the “read text from COM port” block.
  6. Then add a “write … to COM port” block and write the Message variable.
  7. Run the sketch.
  8. Type a word in the COM monitor and send it. The board should respond by displaying the same text.

Additional exercises or challenges

  1. Replace the message “Hello from BatSTEM” with some other text.

  2. Modify the counter so that it increases by 2 each time.

  1. Make the counter value appear faster or slower by changing the waiting time.

  2. In the echo sketch, change the response so that the board always writes a message before sending back the text it received.

Summary of what we have learned

  • We have learned that the serial port lets the board communicate with the computer.
  • We have configured the COM port at 115200 bps.
  • We have sent messages from the board to the COM monitor.
  • We have used a variable to store a counter.
  • We have read text sent from the COM monitor to the board.
  • We have used the COM port as a tool to monitor and debug our programs.

Next steps

  • Learn to use a pushbutton as an input device in Lesson 7.

Solutions to additional exercises

Show

Counting in steps of 2

To make the counter increase in steps of 2, keep the same structure as the counter sketch, but change the increment:

  1. Write Counter to the COM port.
  2. Wait 1 second.
  3. Change Counter by 2.

The output will look something like this:

  1. 0
  2. 2
  3. 4
  4. 6
  5. 8

Responding with the received text

In the echo sketch, we can add a fixed message before writing the Message variable. For example:

  1. Write “I received:” to the COM port.
  2. Write the Message variable to the COM port.

This makes it easier to identify the board’s responses in the COM monitor.