Engineering · Arduino · Web Serial
Sensor to Webpage
Newton had to compute orbits with pen and paper. You're going to make a hidden physical quantity — light, temperature, distance — show up live on a screen in about twenty minutes. Read a sensor, print it to Serial, then let a simple webpage read that same stream and display it as it changes.
← Back to EngineeringDrag the slider — this is standing in for a photoresistor, potentiometer, or temperature sensor changing in real time.
Core idea
Voltage is invisible until you print it
A sensor turns a physical quantity — light level, temperature, distance — into a voltage between 0V and 5V. Arduino's analogRead() converts that voltage into a number from 0 to 1023. On its own, that number lives only inside the chip. The build below makes it visible in two places at once: the Serial Monitor, and a webpage running in a browser on the same computer.
Light, heat, or distance changes a voltage on an analog pin.
analogRead() converts that voltage into a number, 0–1023.
Serial.println() sends the number over USB as plain text.
Web Serial reads the same USB stream and updates the page live.
Step 1 — wire it up
Sensor into an analog pin
Any analog sensor works the same way here: a photoresistor (LDR), a potentiometer, or a temperature sensor like a TMP36 all output a variable voltage that A0 can read. Wire the sensor's signal leg to A0, power to 5V, and ground to GND.
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
Serial.println(reading);
delay(200);
}
// reading is 0-1023: 0 = 0V, 1023 = 5V
// one line of text per reading, ~5 per second Step 2 — read it in the browser
Web Serial: the same stream, on a webpage
Chrome and Edge can talk directly to a USB serial port from JavaScript using the Web Serial API — no server, no extra software. The page below asks the user to pick the Arduino's port once, then reads whatever Serial.println() sends and updates the display every time a new line arrives.
<button id="connect">Connect to Arduino</button>
<div id="value">--</div>
<script>
connect.addEventListener('click', async () => {
const port = await navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
const reader = port.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TransformStream(new LineBreakTransformer()))
.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
value.textContent = value.trim(); // update the page
}
});
</script> That's the whole loop: request the port once, then read lines forever. Everything else — turning that number into a bar graph, a color, or a gauge — is just regular HTML/CSS/JS reacting to a variable that now updates five times a second from the real world.
Try it
Sensor value, live on a page
Drag the panel slider to stand in for the physical sensor changing. Watch the "webpage" on the right update instantly — this is exactly what your build should do once the Arduino is plugged in and the browser is connected over Web Serial.
Quick check
Trace the signal
1. What range of numbers does analogRead() return?
2. How does the sensor reading get from the Arduino to the computer?
3. What browser feature lets a webpage read the USB serial data directly, with no extra server?
4. If the Arduino loop calls delay(200) after each reading, about how many readings print per second?