Traffic counter: how it is built

An induction loop under the tarmac counts every vehicle on our road, Fagerholmsvägen, since 2003. This page describes the hardware and software. The statistics are on the statistics page (in Swedish).

How it works

Since November 2026

  1. The loop. Wire laid under the tarmac. Every car changes the loop's inductance as it passes over it.
  2. The detector, a Sarasota 215B fed through a 240 to 120 VAC transformer, notices the change and closes a relay for as long as the vehicle is over the loop.
  3. The counter is a Wemos D1 mini (ESP8266, a WiFi microcontroller the size of a stamp). The detector gives a short pulse per vehicle, about 14 ms. The counter takes one pulse from the first closing of the relay until it has been open for 30 ms, so the contact's bounces when it closes and opens belong to the same vehicle, and a 2 s lockout stops one vehicle from counting twice. It stamps each vehicle with the exact time (NTP) and sends it over WiFi (MQTT). If the network is down it keeps up to 600 vehicles and sends them later with their real times.
  4. The server stores every vehicle in a database (SQLite), confirms each one back to the counter, and writes the figures for the statistics page. Every open page gets each vehicle the moment it is stored (Server-Sent Events through NGINX), about 0.7 s after the car has passed. On the 1st of every month it adds last month's summary.
  5. Home Assistant listens too, and flashes a yellow lamp indoors for every vehicle.
Wemos D1 mini with a red, a green and a blue LED and their resistors
The counter since November 2026: a Wemos D1 mini (ESP8266) with its three LEDs. The relay wires from the detector go to D5 and G.

Three LEDs on the counter

Green blinks slowly: the program runs. Blue is lit while a vehicle is over the loop. Red means trouble: steady = no WiFi, fast blink = no contact with the server, slow blink = the server has not confirmed the vehicles.

The original build, 2018

From December 2018 to November 2026 a Raspberry Pi B+ did the counting. The loop and the detector are still the same.

Drawing: the loop under the road, the Sarasota 215B detector and the Raspberry Pi
Overview: the loop under the road, the detector and the Raspberry Pi.
The junction box at the foot of a tree, opened, with the cable
The junction box by the road, opened up.
Wires from the loop cable in the junction box
Two loops share one CAT5 cable. Its many conductors let you choose the sensitivity: connecting turns in series (here blue and brown) gives a stronger loop.
The new connector block in the junction box
The old connector replaced; the second loop uses only the blue pair. Everything got WD-40 before the lid went on.
Raspberry Pi with a red, a green and a blue LED and the white relay wires
The Pi with its three LEDs and the white wires from the relay.
Two resistors and a capacitor on the relay input
Two 2.4 kΩ resistors in series, plus a capacitor and a resistor in parallel, protect the input and stop contact bounce. The D1 mini uses the same network.

With two loops a known distance apart on the same road, the counter could in theory work out each vehicle's speed. The Pi's Python program · All monthly summaries as text · The page as it was in 2018