← Arduino

Talking to the computer

Serial.begin(9600) in setup() opens a line to the computer; Serial.println(level) sends a value and starts a new line, and Serial.print stays on the same line. The serial monitor shows what the board sees, which is how you test and debug a sketch. In C++, "words" + a number does not join them.

Your sketch decides from a number you can't see. When the LED does something odd, how do you find out what the board actually read?

The serial monitor shows text the board sends over the USB cable. On this page it is under the board; in Tinkercad and the Arduino IDE it is a panel you open.

void setup() {
  Serial.begin(9600);       // open the line to the computer (9600 bits a second)
}

void loop() {
  Serial.print("Level: ");  // words, and stay on this line
  Serial.println(level);    // the number, then a new line
}
WidthMore contexts, same idea.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

What appears?

The knob is halfway. loop() runs Serial.println(level); after level = analogRead(A0);. What does the serial monitor show?

TryTry it on the board and compare the result with your prediction.

Try: watch the monitor

Try it yourselfYour edits are saved in this browser.

Upload and run, then turn the knob and watch the monitor.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: print and println

Worked exampleBefore each step, say what you think comes next.
  1. Serial.begin(9600);\text{Serial.begin(9600);}In setup(): open the line. Nothing prints without it.

Now pick the pair that does this.

Which pair of lines prints Level: 512 on one line, then starts a new line?

Sam's monitor shows bits of Level: and then nonsense instead of Level: 512. Click the line where Sam's sketch goes wrong.

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

Change the sketch so each line reads Level: and then the number.

Use the board. This ticks when you get there.

This sketch prints nothing. Fix it.

Use the board. This ticks when you get there.

Modify TypicalChange the problem on purpose and see what happens.

Build it in Tinkercad

PractiseA few on your own, to check you have it.

Printing

loop() is Serial.print(1); Serial.print(2); Serial.println(3);. What does the monitor show after two loops?

A night light comes on in daylight. What is the most useful first step?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Print a percentage

Try it yourselfYour edits are saved in this browser.
Need a hint?

map(value, fromLow, fromHigh, toLow, toHigh) rescales a number.

Another hint

int percent = map(level, 0, 1023, 0, 100);

Another hint

Serial.println(percent); and test at both ends of the knob.

Print the knob as a percentage, 0 to 100, one number per line.

Use the board. This ticks when you get there.

For teachers

Students open the serial line in setup(), print a knob's reading every loop and watch it change, build lines with print and println, meet C++'s "words" + number trap (it moves along the text instead of joining), fix a missing Serial.begin, and print a percentage with map. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The board behaves like the real one: nothing prints without Serial.begin, and "words" + a number moves along the text (pointer arithmetic), printing the end of the words or nonsense. A real board would print whatever happens to be in memory; ours prints a few shaded blocks.

The baud rate (9600) must match the monitor's setting on real hardware; the board here ignores it.

Running the lesson

  1. Predict: ask why it is not 'level'.
  2. Find the mistake: show that Python would join them; C++ doesn't.
  3. Practise, debugging: link back to the night light.

The hard part (on purpose)

Python habits: + to join text. Show what the board really prints, then the two-line fix.

When students get stuck

Stretch and support

Stretch: Print Level: 512 (dark) or Level: 512 (light) depending on a threshold.

Support: A card: print = same line, println = new line, begin first.

Where it leads

Next: Outputs that vary (analogWrite and PWM).

Code: micro:bit and the computer uses the same serial idea from MakeCode.

Sources

What each step is for

Now you can
Next lesson5. Outputs that varyanalogWrite(9, value) sets a brightness from 0 to 255 on a PWM pin (marked ~ on the Uno: 3, 5, 6, 9, 10, 11). The pin doesn't get a lower voltage: it switches fully on and off very fast, and the value sets how much of the time it is on. analogRead gives 0 to 1023, so divide by 4 to drive it from a knob.