Serial echo: the board repeats what you type, on an Arduino Uno
Type into the serial monitor and the Uno answers with each character it receives, while a counter keeps ticking every second: sending and receiving at once. The whole build, an Arduino Uno, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $27. On Mokxi they're free.
What you'll learn
- Sending text to the board over serial
- Checking how many characters have arrived
- Timing with millis instead of delay
- Doing two jobs at the same time
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 1
How it works
There is nothing to wire. The serial port runs over the USB cable. When the board starts, it prints UNO is ready and waits for you to type.
Each character you send comes back as UNO received followed by that character. At the same time, once a second, the board prints a counter that goes up by one. It uses the clock instead of a long pause, so it can count and listen together.
Parts list
1 part, plus the jumper wires. They retail for about $27; on Mokxi they're free.
Kits that can build it
These 13 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- SunFounder Universal Maker Sensor Kit
- Adafruit MetroX Classic Kit
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Talk to the board: it counts out loud and repeats what you type.
//
// Nothing to wire: the serial port runs over the USB cable.
//
// Serial.available() says how many characters have arrived; Serial.read()
// takes the next one. The counter is printed once a second with millis(), so
// the board can listen and count at the same time.
unsigned long lastCount = 0;
int counter = 0;
void setup() {
Serial.begin(9600);
Serial.println("UNO is ready! Type something.");
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n' || c == '\r') continue;
Serial.print("UNO received: ");
Serial.println(c);
}
if (millis() - lastCount >= 1000) {
lastCount = millis();
Serial.print("counter: ");
Serial.println(counter++);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the serial port at 9600 baud and prints a greeting, so you know the board is listening.
void setup() {
Serial.begin(9600);
Serial.println("UNO is ready! Type something.");
}loop sketch.ino
First, Serial.available() tells it if characters arrived, and Serial.read() takes them one at a time and echoes each, skipping newlines. Then millis() is compared with lastCount, so once a second it prints the counter and adds one.
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n' || c == '\r') continue;
Serial.print("UNO received: ");
Serial.println(c);
}
if (millis() - lastCount >= 1000) {
lastCount = millis();
Serial.print("counter: ");
Serial.println(counter++);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing comes back when you type. This project
Check the serial monitor baud rate is 9600, matchingSerial.begin. - Your typing seems to get cut up. This project
The board echoes one character at a time, so a word comes back as separate lines. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.