Talk to Arduino and ESP32 from the Browser with the Web Serial API
For years, talking to a microcontroller meant installing an IDE, a terminal program or a driver-specific tool. The Web Serial API lets a web page open a serial…
You connect to a board, open the serial monitor, and get a screen of question marks, boxes and random symbols, or nothing at all. Almost every time, the cause is one of a handful of mismatches between the two ends of the link. This guide explains what a serial link actually sends, so you can recognise each mismatch from its symptoms and fix it quickly.
A UART, the serial port in every microcontroller, sends each byte as a short pulse train. The line idles high; a start bit (low) announces a byte, then come the data bits (usually 8, least significant first), an optional parity bit, and one or two stop bits (high). The common setting "8N1" means 8 data bits, no parity, 1 stop bit: 10 bit-times per byte.
There is no clock wire. Both ends must agree in advance on how long a bit lasts, which is what the baud rate sets: at 115200 baud, a bit lasts about 8.7 µs. The receiver waits for the start bit, then samples the line in the middle of each expected bit. If the two ends disagree on the bit length, the samples drift away from the middle and land on the wrong bits.
Cause: a baud rate mismatch. Set the monitor to the same rate as Serial.begin() in your firmware. If you don't know the rate, try the common ones: 115200, 9600, 57600, 38400.
Some garbage at start-up is normal and has a different cause: boot messages printed at another rate. The ESP8266's boot ROM prints its first messages at 74880 baud before your sketch starts at, say, 115200, so a few garbled lines appear and then clean output. Setting the monitor to 74880 makes the boot messages readable, which helps when diagnosing reset problems.
Clock error matters too. A microcontroller can only approximate a baud rate by dividing its own clock. On a 16 MHz Arduino Uno, 9600 baud comes out 0.16 % fast, but 115200 comes out 2.1 % fast (with the double-speed mode the Arduino core uses). Two links that are each a couple of percent off in opposite directions can fail where either alone would work, which is why 115200 between two cheap boards is sometimes flaky while 57600 is solid.
Serial.begin(), or a board whose USB port is separate from the UART you print to, looks exactly like a dead link.Cause: line endings. Systems disagree on how to end a line:
| Ending | Characters | Common source |
|---|---|---|
| LF | \n | Linux, most protocols |
| CR | \r | Some old terminals and devices |
| CR+LF | \r\n | Windows, Arduino's Serial.println() |
When receiving, split on \n and strip a trailing \r. When sending, match what the firmware expects: if it reads with Serial.readStringUntil('\n'), a command sent with "No line ending" never completes, and one sent with CR+LF arrives with a stray \r unless the code calls trim().
For links that must be robust, such as a robot's motor controller talking to its ROS computer, add framing and checksums so corrupted messages are rejected instead of acted on; our guide to designing a serial protocol shows how.
For years, talking to a microcontroller meant installing an IDE, a terminal program or a driver-specific tool. The Web Serial API lets a web page open a serial…
Many robots split the work between a computer running ROS 2 and a microcontroller that drives the motors and reads the encoders. The serial link between them…
"The board doesn't show up" is one of the most common and most frustrating problems in robotics. The cause is rarely the board itself; it is usually the cable,…
Printing sensor values over serial is the quickest way to see what a robot's microcontroller is doing. Done carelessly, it gives you a wall of numbers you…
If you have any query or problem
feel free to contact us
email: [email protected]
Comments 0