Phil-duino

A collection of Arduino tutorials put together to help a colleague dive into embedded systems!

View the Project on GitHub ChrisAlphabet/Phil-duino

Serial Monitor

After spending many lonely nights in the cold laboratory with your microcontroller, suffering through yet another existential crisis as you fail to get your code to work, have you ever wished your microcontroller could just talk to you and tell you what was wrong. Well, now it can! Kind of. Introducing…

drum roll please

The Serial Monitor!

What is the Serial Monitor

Quite simply, it is a terminal used to send and receive serial data. Obviously there is more to it than that, but if you want the theory go to Wikipedia and search for ‘fuckwit’. After staring long and hard at a picture of yourself, go to your local university and enroll in a computer science degree. If you want to learn how to use the Serial Monitor, read on.

How to use the Serial Monitor

Remember those two standard function we learnt about in Tutorial 1? Well, guess what, we are going to use setup and loop again.

Setup

The only setup required for serial data is that we must specify the baud rate. The baud rate is the data rate in bits per second. The standard baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, and 115200. Use whatever you like, but just make sure that the baud rate you set in your program matches the baud rate you select when you launch the monitor. So, lets take a look at the familiar main.cpp.

#include <Arduino.h>

void setup() {
    // put your setup code here, to run once:
}

void loop() {
    // put your main code here, to run repeatedly:
}

To setup sending and receiving serial data, we use Serial.begin(baud_rate). You could also use Serial.begin(baud_rate, config) but the default config will work just fine. If you really need to know, config is where you can change the data, parity and stop bits. The default is to use 8 data bits, no parity, and one stop bit. The default is perfectly fine for us.

...

void setup() {
    Serial.begin(9600);
}

...

Now, this is where we will take a quick detour and examine what we have done. We have stumbled across an object! An object is an instance of a class. If you have ever heard some nerd talk about about C++ being ‘object orientated’, now you have an idea what they are talking about. Maybe someday, you will be that nerd.

Very simply, classes have data members and member functions. We will create our own classes in a later tutorial, but for now that is all we need to know. This will upset alot of CS majors, but hey, if you write something on the internet and it doesn’t upset someone, are you really even on the internet?

We use the ‘member of object’ operator (.) to access data members and member functions.

The class Serial has been declared in Arduino.h, along with an instance of the class, AKA an object for us to use!

So, very simply, in setup we have used the ‘member of object’ operator (.) to access the member function begin() and passed in the value 9600 to set the baud rate. Again, if you would like to see what is happening under the hood, go to C:\Users\insert_your_username\.platformio\packages\framework-arduinoavr\cores\arduino and look at HardwareSerial.cpp, HardwareSerial.h and HardwareSerial0.cpp. This will show you the C++ in all its gory details.

So, lets print something to the screen!

Loop

To print to the screen, we use the Serial member functions print and println, where println follows the print with a new line character.


...

void loop() {
    Serial.print("See You Next Tuesday!");
}

Write to the Serial Monitor

#include <Arduino.h>

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.print("See You Next Tuesday!");
}

Using PlatformIO:

  1. Build: ctl+alt+B
  2. Upload: ctl+alt+U
  3. Serial Monitor: ctl+alt+M

Select the port with Arduino Nano connected to it and set the baud rate to 9600. PLatformIO is pretty smart, it will probably do this for you. You should now see “See You Next Tuesday!” whizzing across the screen. Slowing it down to human readable and separate lines is left as an exercise to the reader.

Well done! Your microcontroller can now talk to you. You are one step closer to removing human contact from your life.

Read from the Serial Monitor

So now you have been seduced by the sweet whisperings of the microcontroller and you want to respond. Let start a two way dialog.

Setup

Our setup is the same as before, but we need to define a variable to store the value we send across the Serial Monitor.

#include <Arduino.h>

int input = 0;

void setup() {
    Serial.begin(9600);
}

...

Lets examine the line: int input = 0; This is a little love note to the compiler which says:

Dear compiler, Please set aside one sexy integer sized piece of memory. Give it the saucy name of ‘input’. Assign the cheeky bugger the naughty value of zero. Make that minx available to the entire program. Forever yours, Phil

Or, less erotically, create a global variable of data type int called ‘input’ with value 0.

Either way, the result is a variable that we can use to store the value sent from the Serial Monitor into our program.

Serial is setup same as before.

Loop

...

void loop() {
    if(Serial.available() > 0) {
    	input = Serial.read();
    }
} 

Serial.available() returns the number of bytes that are available from the Serial receive buffer. If the number returned is greater than zero, there is something to read! So we should read it using Serial.read().

Read From The Serial Monitor

#include <Arduino.h>

int input = 0;

void setup() {
    Serial.begin(9600);
}

void loop() {
    if(Serial.available() > 0) {
    	input = Serial.read();
    	Serial.print(input);
    }
} 

Using PlatformIO:

  1. Build: ctl+alt+B
  2. Upload: ctl+alt+U
  3. Serial Monitor: ctl+alt+M

Click on the Serial Monitor terminal, use the keyboard to send some data and watch as the Arduino Nano echoes it back!

Homework Exercises

  1. Complete the exercise left to the reader in section “Write to the Serial Monitor”
  2. Toggle the built-in LED everytime the ‘t’ key is pressed and print the current LED state
  3. Flash the built-in LED at a frequency set over serial