Lab 0: Serial Monitor
Universal Serial Bus (or “USB”) was invented in the 1990s as an attempt to unify the various interfaces used to connect peripherals (external devices) to PCs, like PS/2 ports (keyboards and mice), parallel ports (printers and scanners), serial ports (modems), and game ports (joysticks and gamepads). USB can be used to connect almost any type of peripheral to a PC. It removes the need for manual configuration (a huge hassle in pre-2000s PCs) and even provides a modest amount of power, removing the need for external power supplies for many devices.
The downside is that USB is exceptionally complicated, at least compared to the ports listed above that it replaced. Fortunately, your Arduino includes a neat little integrated circuit called the CH340G that actually simulates a serial port (yes, the outdated one mentioned above) and allows you to send ASCII text, one byte at a time, back and forth between your Arduino and the PC using a USB cable (which also conveniently provides power to the Arduino and allows it to run).
In many of the labs that follow you will use the USB serial connection to send text from your Arduino to your PC, which can then be viewed in the Arduino IDE’s serial monitor. This is incredibly useful for debugging, as otherwise it can be very difficult to tell what an Arduino program is doing. Although it is possible to send data in the other direction (from the PC to the Arduino), you will not do that in any of these labs.
Example Program
Connect your Arduino to your PC and upload the following program:
void setup()
{
// Open the serial connection:
Serial.begin(9600);
}
void loop()
{
// Print a message twice a second:
Serial.println("Hello, world!");
delay(500);
}
If you open the serial monitor in the Arduino IDE (“Tools > Serial Monitor”, or Ctrl+Shift+M) you should see the text “Hello, world!” (without quotes) printed to the screen twice each second. This data is sent over the USB cable, so if your Arduino is not connected to your PC you will not see any text appear!
If you are getting errors in the Arduino IDE, make sure that you have typed println
with a lowercase L
and not printIn
with an uppercase I
. The two letters look very similar in many fonts. Also, make sure that you have used double quotes like "
and not single quotes like '
. Although they are interchangeable in Python they mean something completely different in C.
Learn More
-
Although the C programming language has a lot in common with Python they look very different. C uses semi-colons (
;
) to mark the end of a statement in the same way that Python uses newlines (the thing that happens when you hit the “Enter” key). C also uses curly braces ({
and}
) to enclose blocks of code in the same way that Python requires you to keep blocks of code indented to the same level. This book will point out more similarities and differences between the two programming languages in future labs. -
Any text that follows
//
in a C program is a comment and is ignored by the compiler. By now you have probably been told too many times that you should be using comments to document your code. But what does that mean? It can be helpful to use comments to summarize and separate blocks of code, the same way you might use headers and section dividers in a long text document (like this one) to make it easier to read. Comments are also useful for explaining the behavior of non-obvious code, like a complicated algorithm. -
Every Arduino program must contain two special functions:
setup()
andloop()
. Thesetup()
function is called exactly once when your program first starts and is where your program should perform its one-time initialization tasks. After that finishes, yourloop()
function will be called repeatedly until the Arduino is unplugged or restarted. This is where your main program logic will go. -
Serial.println()
is a function that will send whatever you specify (text, numbers, etc.) from your Arduino to your PC to be displayed in the serial monitor. In the code above it is used to send a specific text string,"Hello, world!"
, to your PC. -
There is also a function called
Serial.print()
that does exactly the same thing except that it does not automatically advance to the next line likeSerial.println()
. -
If you want to use any of the
Serial
functions you will need to first callSerial.begin()
to open the connection to your PC in yoursetup()
function. The number specified in parenthesis is something called the bit rate or baud, which is the speed at which data is sent (in bits per second). You should leave this at 9600. -
The
delay()
function is used to pause the program for a specified amount of time (in milliseconds). There are 1000 milliseconds in a second, so this program prints its message twice per second.
Assignment
Using the program above as a starting point, create a program that prints the worst “Dad joke” you know (or can find on the internet) to the serial monitor. Use the delay()
function to ensure that your joke has the correct comedic timing (i.e., delay between the joke and the punchline).
Challenge
Add an additional creative flair, like a few animated dots between the joke and the punchline to build up the suspense for someone watching. Remember that you can use Serial.print()
to print text without automatically advancing to the next line.