Lessons and the learning game

Distance and presence sensing

Ultrasonic, time of flight, IR, PIR, radar and break beams on an Uno, then a museum visitor counter that tells in from out and measures each visitor.

Take the course at /learn/path/distance-and-presence. It is an intermediate course: it assumes the Uno course. It is not a robot course; the robotics capstone covers Sharp sensors on a chassis.

The lessons

How far. An HC-SR04 read by hand with pulseIn(): the echo time, the speed of sound, a timeout for no echo, and a cold day that reads 5% long until the sketch corrects for it. A VL53L0X timing laser light, with the 8190 that means nothing came back. A Sharp GP2Y0A21 read raw, fitted with a power law, refused past 80 cm, and caught folding back below 10 cm.

Is anyone there. A PIR's OUT pin timed: hold time, the blind block time after each pulse, repeat and single trigger, and a porch light on its own timer. An RCWL-0516 radar with no blind time, and a room that needs two blips to count as occupied. A photo interrupter on an interrupt: things passing counted and timed, and a slotted disc read in RPM.

Boss. A museum doorway: beam A outside, beam B inside, a VL53L1X in the ceiling for each visitor's height, an I2C LCD, a capacity limit and a FULL lamp.

The benches

Five lessons open gallery circuits with a dst* sketch: the parking sensor (TRIG 7, ECHO 6, LED 8), the laser ruler (I2C), the distance rainbow (VO on A0), the fog trigger (PIR on 2, LEDs on 4 and 3) and the radar light (OUT on 2). The break beam lesson has an interrupter on pin 3. The boss has beams on pins 2 and 3, a VL53L1X and an LCD at 0x27 on A4 and A5, and the FULL lamp on 13.

What each sketch does

  • dstsonar: TRIG pulse, pulseIn() with a 30 ms timeout, SOUND_MM_PER_US.
  • dsttof: Pololu's VL53L0X, single readings, the 8190 code and NEAR_MM.
  • dstir: averaged analogRead(), CURVE_A and CURVE_B, and FAR_VOLTS.
  • dstpir: pulse lengths and gaps, and LIGHT_MS.
  • dstradar: blips, CONFIRM inside WINDOW_MS, and EMPTY_MS.
  • dstbeam: a CHANGE interrupt, MIN_BREAK_MS, and SLOTS for RPM.
  • dstboss: a five-state doorway machine, CEILING_MM, CHILD_MM and CAPACITY.

Honest limits

  • The HC-SR04 echo has a few microseconds of jitter but no soft surfaces, angles or multiple echoes.
  • The VL53L0X reading does not change with its timing budget, target color or sunlight.
  • PIR and radar movement is a click on the part: there is no distance, direction or size.
  • The doorway's break beams are slot interrupters standing in for a beam across a door.

Sensors and measurement introduces many of these parts one at a time, and More pins adds pins when a project outgrows the Uno.