AndyPi’s Sonic Ruler measures distances in the same way bats do – using ultrasonic sound waves! The circuit and code are both very simple, and it gives you an idea of how to use an ultrasonic sensor for more complex projects, such as object detection and avoidance for robots, or a theremin (a musical instrument by which the note played depends on the distance from the sensor).

Components

Arduino Nano v3
AndyPi HD44780 LCD display
HC-SR04 Ultrasonic Sensor
Jumper Wires
Your own case and 5v PSU!

Circuit

The circuit is shown in the Fritizing diagram below, and the wiring is also listed in the Arduino code.

SonicRuler_schem

The AndyPi prototype – inside the box. Power is provided to the +ve / GND pins that you can see in the top right corner. You could also add you own battery supply for portability.

IMG_1072

Arduino Code

The Arduino code is very straightforward, since there is a library for the HD44780 LCD. The HC-SR04 ultrasonic sensor is also pretty easy. The Arduino first sends a 10 microsecond HIGH pulse to the “trigger” pin on the sensor, and then reads from the “echo” pin, which is the time in microseconds that an ultrasonic wave takes to travel from the sensor to the object in front of it, and back again. You’ll need to input the speed of sound for your location into the code (which varies by altitude, but is 330m/s at sea level), and there is a simple equation by which the Arduino converts this into the distance to the object in front of the sensor.