Activity 2: Serial Monitor Control | Betabox

Activity 2: Serial Monitor Control

Overview

In this section, you will be loading a new program onto your robotic arm and using the Serial Monitor to control the movement of the arm. This will help you understand how the arm moves and help you understand the XYZ coordinates that the arm uses to position itself.




Needed Materials

  • Robotic Arm on Workbench Base
  • Foam Blocks
  • Power Supply
  • Coordinate Grid Worksheet



Introducing the Arduino Serial Monitor

Let’s start by familiarizing ourselves with the Arduino Serial Monitor.

Think of the Serial Monitor as a tether to your microcontroller.  The Serial Monitor does two things:

  • It accepts and displays the data that the microcontroller sends to your computer. That helps you understand what is happening with the microcontroller, and therefore, with whatever it is controlling.  In this case, your robot.
  • It allows you to send commands from your computer to the microcontroller.  If the microcontroller has been flashed with a program that lets it interpret these commands, it can respond to them in real time, which is very helpful.

 

IMPORTANT: For the Serial Monitor to work, your computer must be connected to the ESP32 via the USB cable. This allows data to flow in both directions.




Uploading the New Program to The ESP32

To complete this activity, you will need to upload a new set of code to the ESP32. To do this, you will need to plug the ESP32 into your computer and open the Arduino IDE.

Follow the steps below to upload the new code to your ESP32:

  1. Plug one end of a USB-C cable into the top USB port on your ESP32 and the other end of your USB-A/USB-C cable (depending on the device you're using) into your computer.
  2. Open Arduino IDE if it is not already open and open the appropriate sketch file in the Arduino IDE. (Program5—Serial_Monitor.ino)
  3. Click on "Tools" at the top bar of your device and scroll until you see "Upload Speed." Set this to "230400."
  4. Now click the dropdown menu next to the play button. It might say "Select Board" but it could be different if the computer has ever used Arduino IDE before.
  5. Click "Select other board and port..." and you should see a pop-up window appear.
  6. In the search bar, search for "ESP32 Dev Module" and select the option that says "ESP32 Dev Module - esp32_bluepad32."
  7. Now look at the ports section of the window. If you see multiple entries here it may mean that your computer has different monitors or modules being used. You need to look for an entry that says either "COM" on a Windows device or "/dev/cu.usbserial" on a MacOS device.
  8. Once selected, you can click "OK" and that should close the pop-up window.
  9. Finally, you can click the button that has a right arrow on it and is next to the checkmark button to upload the program to the arm.



About Program5 - Serial_Monitor.ino

Program5—Serial_Monitor.ino uses the Serial Monitor to control the built-in LED light on the top of the ESP32 that controls your robot.  This LED is a convenient way to test whether you’ve connected successfully because you don’t have to wire anything—it’s always ready to go.

 

This program lets you send five different commands to the microcontroller; each command does something different with the LED.

  • A command of “Y” or “y” will turn the LED ON.  By default, when the program starts, the LED is turned OFF.
  • A command of “N” or “n” will—you guessed it—turn the LED OFF.
  • You can also make the LED flash a predetermined number of times.  Entering a “1” makes it flash once; “2” makes it flash twice; and “3”—wait for it!—makes the LED flash three times.

 

Think of these LED commands as “stand-ins for commands that tell your robot arm what direction to move, how fast to move, whether to close or open the gripper, or when to stop moving.

 

VIDEO (Use the command line.mp4)




Open the Serial Monitor

IMPORTANT: Your microcontroller must remain connected to your computer with the USB cable, and the IDE must acknowledge the microcontroller before you can proceed.

 

In the Arduino IDE, there are two ways to open the Serial Monitor.

 

  • Option 1 is to use the menu at the top of the IDE.  Choose Tools > Serial Monitor as shown in Figure 5.2.2.

 

Figure 5.2.2 Open the Serial Monitor using the menu.

<INSERT FIGURE 5.2.2>

 

  • Option 2 is to use the hotkey, which is to press Ctrl+Shift+M (“M” for “Monitor,” get it?) at the same time.  Both methods will have the same result: a new window will open up in the lower half of the IDE screen.  This new window is the Serial Monitor.

 

Figure 5.2.3 The Serial Monitor appears in a new window in the lower half of the IDE.

<INSERT FIGURE 5.2.3>




Testing Commands

With the sketch loaded successfully to your microcontroller, let’s use the Serial Monitor to test the five commands.

 

Take a look: Pay attention to the state of the blue LED before you type anything into the Serial Monitor.  Is it ON or OFF?  Write down your observation. [Hint: The LED is OFF by default when the program first starts to run.] 

Important first step:  All your commands must be typed into the Message bar (Figure 5.2.6) at the top of the Serial Monitor.  Hit <ENTER> when you are ready to send. 

Figure 5.2.6 Enter all commands into the Message bar

<INSERT FIGURE 5.2.6>

Now, type a “Y” or a “y.”  The program is not case-sensitive (that is, it treats uppercase and lowercase letters the same).

  • What does the blue LED do?  Write down your observation.  [Hint: It turns ON and stays ON.  It may take one second before doing so, but that is programmed into the instructions.]  
  • What does the Serial Monitor tell you?  Write down your observation.  [Hint: It says that the LED has been turned ON. Figure 5.2.7 The Serial Monitor shows that the LED has been turned on.  [NOTE: The figure goes into the HINT.]

<INSERT FIGURE 5.2.7>

Type another “Y.”  

  • What happened now?  Write down your observation.  [Hint: Even though the LED doesn’t change (you told it to turn ON when it was already ON), the microcontroller was smart enough to know that, and informed you that the state didn’t change.  Figure 5.2.8 The newest message is at the bottom; It tells you the LED is already on. [NOTE: The figure goes into the HINT.]

<INSERT FIGURE 5.2.8>

So, the microcontroller is keeping track of the state of the LED, as well as feeding that information back to you.  This tells us that the information is bi-directional—that is, it flows to the microcontroller (when you send a command) and from the microcontroller back to the serial monitor with status updates displayed in the main window of the Serial Monitor.

Now type an “N” or “n.” 

  • What happened?  Write down your observation.  [Hint: After a pause of one second, the LED turns OFF.] 
  • What information does the microcontroller feed back to you?  Write down your observation.  [Hint: Not only does it tell you whether the state changed, but it also informs you how long the previous state lasted. It is keeping track of time as well as the state of the LED.  Figure 5.2.9 Shows the result of keying “no” into the command line. [NOTE: The figure goes into the HINT.]

<INSERT FIGURE 5.2.9>




Try This: Flashing the LED 

We can flash the LED, as if we were sending a coded message.

With the LED either ON or OFF, type “1” and hit <ENTER>.  

  • What happens?  Write down your observation.  [Hint:  The LED stays off for one second, then it flashes quickly for less than a second, and then stays off.]
  • What happens if the LED was ON when you entered the “1”? Write down your observation. [Hint:  If the LED was on, it will turn off before starting the flash pattern.]

 

Likewise, type “2” + <ENTER>, and after that, try a “3.”  

  • What happens?  Write down your observation.  [Hint:  As before, the LED stays off for one second, and then it flashes quickly.  But it flashes as many times as the number you entered in this case. Then it stays off when the flashing is complete.]
  • What happens if the LED was ON when you entered the “2” or “3”?  Write down your observation. [Hint:  If the LED was on, it will turn off before starting the flash pattern.]

 

These options show that we can send an infinite number of commands to the microcontroller from the Serial Monitor.

 

There may be unexpected (but beneficial) behaviors coded into this simple program.  For example:

 

Type 123 (no spaces) and then hit <ENTER>

  •  What does the LED do?  Write down your observation.  [Hint: The LED flashes once; waits a second; flashes twice; waits another second; then flashes three times.]  
  • What does this imply that the microcontroller is doing with the commands it receives?  Write down your observation.  [Hint: Instead of processing only the first command it recognized and ignoring everything else, it stores and queues the commands for processing in the order they were entered.]

 

Type “YNYN” and hit <ENTER>.  

  • What happens?  Write down your observation.  [Hint: As above, the microcontroller is queuing these commands and executing them in order.]


Now turn the LED off (use the letter “N”); then enter a string of letters for which no commands have been programmed, but include only ONE “Y” somewhere in the middle of the string.  For example, with the LED OFF, type “UIYPQ” and hit <ENTER>.  

What happens?  Write down your observation.  [Hint: The microcontroller will ignore (throw away) all the letters it does not recognize, but it will process the one it recognizes as a command.  However, it will take one second to process each letter, recognized or not.]




Tips and tricks when using the Serial Monitor

Clear output from the Serial Monitor screen:

As you work with the serial monitor, lines of data can be built up. Get rid of data you no longer want to see by clicking the Clear Output button at the far-right corner at the top of the Serial Monitor window (below).

 

Figure 5.2.10 Clear Output to get rid of messages you no longer need.

<INSERT FIGURE 5.2.10>

Change the size of the Serial Monitor

You can drag the Serial Monitor’s upper boundary up or down with the mouse. 

 

Figure 5.2.11 Drag the blue bar to adjust the size of the monitor.

<INSERT FIGURE 5.2.11>

Close the Serial Monitor

There are several ways to accomplish this:

  • Option 1 is simply using the hotkey combination you learned before (Ctrl+Shift+M).

 

  • Option 2 is to use the top menu (Tools > Serial Monitor), as described above.

 

  • Option 3 is to click the small “x” next to the Serial Monitor label at the top of its window.  

 

Notice that both Option 2 and Option 3 leave the “Bottom Panel” open.

 

Figure 5.2.12 Option 3 – Use the small x to close the Serial Monitor.

<INSERT FIGURE 5.2.12>

If you want to close the Bottom Panel as well, you can use…

  • Option 4, which is the “Toggle Bottom Panel” button at the very lower-right corner of the Arduino IDE window.

 

Figure 5.2.13 The Toggle Bottom Panel button closes the entire panel.


<INSERT FIGURE 5.2.13>




Next Steps

That should give you a solid footing in the Arduino IDE’s Serial Monitor.  Now let’s explore how we can use the Serial Monitor to communicate with our robot in real time.