Sub topic 8
Watching Readings in the Serial Monitor
This chapter assumes you can create and upload an Arduino sketch, use setup() and loop(), and open the Arduino IDE’s Serial Monitor. It also assumes that your potentiometer is wired with its center terminal connected to A0.
The Serial Monitor is a text window that receives data from the Arduino over USB. It is useful for checking sensor readings before you connect those readings to another output. In this chapter, you will print the potentiometer’s raw ADC value and its estimated voltage.
Start serial communication
The Arduino communicates with the computer through a serial connection. Serial communication sends data one character at a time.
In setup(), call Serial.begin() once:
Serial.begin(9600);The number 9600 is the baud rate. It specifies how quickly data is sent. The Serial Monitor must use the same baud rate. If the rates do not match, the output may contain unreadable characters.
Use Serial.print() to write text without moving to a new line. Use Serial.println() to write text and then move to the next line.
Print a potentiometer reading
This sketch reads A0 every 100 milliseconds. On an Arduino Uno, analogRead() returns a value from 0 to 1023. The conversion assumes a 5 V analog reference.
const int POT_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
float volts = raw * (5.0 / 1023.0);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" volts=");
Serial.println(volts, 2);
delay(100);
}The second argument in Serial.println(volts, 2) specifies two digits after the decimal point. A typical line might look like this:
raw=512 volts=2.50Turn the potentiometer slowly. The raw value should increase or decrease, and the voltage should change with it. At one end, the value should be near 0 V. At the other end, it should be near the board’s reference voltage.
Open and configure the Serial Monitor
- Upload the sketch to the Arduino.
- Open the Serial Monitor in the Arduino IDE.
- Set its speed to 9600 baud.
- Turn the potentiometer and watch the values change.
The sketch sends about ten lines per second because delay(100) pauses for 100 milliseconds. A shorter delay produces faster updates, but it can make the output harder to read.
The line ending setting usually does not matter when the Arduino only sends data. It matters when the computer sends characters back to the Arduino.
Interpret unexpected output
| Symptom | Likely cause | Check |
|---|---|---|
| Unreadable symbols | Baud rates differ | Set both sides to 9600 |
| No changing value | Wrong wiring or pin | Check the center terminal and `A0` |
| Value changes in jumps | Electrical noise or loose wire | Secure the circuit and average readings |
| Always near 0 or 1023 | End terminals or ground are miswired | Check 5 V, ground, and the center terminal |
A potentiometer does not produce a perfectly stable voltage. Small changes in contact position, wiring, and electrical interference can make the ADC result move by a few counts. Do not treat every one-count change as a meaningful turn of the knob. Later, averaging or a deadband can reduce this visible jitter.
Exercise
The Serial Monitor shows unreadable characters even though the sketch uploads correctly. What should you check first?
Show answer
Set the Serial Monitor to the same baud rate used by Serial.begin(). For the sketch in this chapter, both must be set to 9600 baud.