A blinking LED tells you one thing: the board is running. It cannot tell you that the pressure reading is 1004 hPa, or that the sensor failed to start, or that your maths produced 2 when you expected 2.5. For that you need words and numbers, and the Uno has a way to send them down the same USB cable you upload over.
This is called the serial link, and it is the most useful debugging tool you have. It is also a small version of what your CanSat does: take a reading, turn it into characters, send it down a link, read it at the other end. Swap the USB cable for a radio and the idea stays the same.
The sketch
Sending text and numbers to your laptop
void setup() {
Serial.begin(9600); // open the link, 9600 bits per second
Serial.println("Board awake");
}
void loop() {
Serial.println("Still here");
delay(1000);
}
Upload it, then open the Serial Monitor: the magnifying glass at the top right, or Ctrl/Cmd + Shift + M. Check the baud rate dropdown in the corner of that window says 9600. You should see a line appear every second.
Line by line
Serial.begin(9600);- Starts the serial link and sets its speed to 9600 bits per second. It goes in
setup()because you only need to do it once. Both ends have to agree on the number, so the Serial Monitor has a matching dropdown. 9600- The baud rate. 9600 is slow and very reliable.
115200is also common and twelve times faster, which matters when you are sending a packet every second and want it to be sent well before the next one is ready. Serial.println("Board awake");- Sends the text, then moves to a new line. The
lnis for line. Text to be sent literally goes in double quotes. Serial.print(count);- Sends something and stays on the same line, so you can build one line out of several pieces. Give it a number and it turns the number into characters for you.
Serial.- The dot means "belonging to".
Serialis an object the Arduino library provides, andbegin,printandprintlnare things it knows how to do. You will meet the same pattern with the sensor in tutorial 7:bmp.readTemperature(). int count = 0;- Declared outside both functions, so it exists for the whole life of the program and keeps its value between runs of
loop(). Declare it insideloop()and it would be wiped back to zero every time round, which is a classic first bug.
// A variable that survives between runs of loop(), and labelled output.
int count = 0; // make a box called count, put 0 in it
void setup() {
Serial.begin(9600);
Serial.println("Counting:");
}
void loop() {
count = count + 1; // take what is in the box, add one, put it back
Serial.print("reading number ");
Serial.print(count);
Serial.println(" sent");
delay(1000);
}
Reference
The serial commands you will actually use
There are more, but these cover almost everything you will need.
| Line | What it does |
|---|---|
Serial.begin(9600); | Start the link. Needed once, in setup(). |
Serial.print(x); | Send x, stay on the same line. |
Serial.println(x); | Send x, then end the line. |
Serial.println(); | Just end the line. Useful after a run of print calls. |
Serial.print(temp, 2); | Send a decimal number with exactly 2 decimal places. Default is 2, so Serial.print(temp, 4) when you want more. |
Serial.print(value, HEX); | Send a whole number in hexadecimal. BIN and DEC also work. Handy for sensor addresses. |
Serial.available() | How many characters have arrived from the laptop and are waiting to be read. |
Serial.read() | Take the next waiting character. |
Serial.readStringUntil('\n') | Take everything up to the next newline as one piece of text. Good for whole commands. |
Graphs
The Serial Plotter
The IDE has a second window, Tools › Serial Plotter, which draws a graph of anything the board prints as a plain number. It only works if the line contains nothing but numbers, so Serial.println(reading); plots and Serial.println("reading is 512"); does not.
// Print one bare number per line and the Serial Plotter will draw it.
// analogRead is covered properly in tutorial 6. For now, twist a
// potentiometer wired to A0, or just touch the pin with your finger.
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
Serial.println(reading);
delay(50);
}
Print several numbers separated by commas or spaces on one line and the plotter draws a separate trace for each. One line of temperature,pressure gives you two lines on a graph, live, as the sensor changes. That is the quickest honest graph you can produce of a test, and tests with evidence are what the reviewers are looking for.
The other direction
Sending commands to the board
The link works both ways. The Serial Monitor has a text box at the top; anything you type there and send arrives at the board. This is the same idea as telecommand, which is one of the secondary missions the competition suggests.
// Listening to the laptop instead of talking to it.
// Type 1 then Enter to switch the LED on, 0 then Enter to switch it off.
// The "if" statement is tutorial 5. Read it as "only do this when...".
int ledPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Send 1 for on, 0 for off");
}
void loop() {
if (Serial.available() > 0) { // has anything arrived?
char command = Serial.read(); // take one character
if (command == '1') {
digitalWrite(ledPin, HIGH);
Serial.println("LED on");
}
if (command == '0') {
digitalWrite(ledPin, LOW);
Serial.println("LED off");
}
}
}
Note '1' in single quotes, not "1" in double quotes. Single quotes mean one single character, which is what Serial.read() hands back. The character '1' and the number 1 are different things to the board.
Troubleshooting
If the output looks wrong
| What you see | What it is |
|---|---|
| Rows of question marks or random symbols | The baud rate in the Serial Monitor does not match Serial.begin(). Fix the dropdown. |
| Nothing at all, but the sketch uploaded | Either no Serial.begin() in setup(), or you are printing in setup() only and missed it. |
| The first line is always missing | Opening the Serial Monitor resets the Uno, and the board starts printing before your laptop is listening. Add a short delay(500) at the top of setup(), or just print it again. |
| Output stops after a while | Often a String slowly eating the Uno's 2 KB of memory. Prefer Serial.print on separate pieces over building one big String. |
| Uploads fail once you wire something up | Pins 0 and 1 are the serial pins the USB link uses. Never wire anything to them. |
Now change it
Change one thing at a time, upload, and watch what the board does.
- Change
9600to115200in the sketch but not in the Serial Monitor. Look at the output, then fix the dropdown. - Make the counter print every tenth of a second instead of every second. How many readings do you get in a 30 second descent at that rate?
- Move
int count = 0;insideloop()and upload. Explain what you see in one sentence. - Replace the three print lines with a single line that produces output like
7,on,ok. This is called a comma separated line and it is what tutorial 8 builds on. - Print the same number three times: as a plain number, with
HEX, and withBIN. What is 255 in each? - In the listening sketch, add a third command that makes the LED flash five times.
- Make the board print a different message the first time round
loop()only. There is more than one way and all of them teach you something.