Linux Format

Ultrasonic tape measure

Doctor Who may have a sonic screwdrive­r, but you can build an ultrasonic tape measure using the Arduino Uno.

-

First, build the circuit as per the diagram, then plug your Arduino into a spare USB port. Install the Arduino editor on Linux, using the excellent guide at www.arduino.cc/en/Guide/Linux.

Open the Arduino applicatio­n. The Arduino has code that runs once, typically in ‘setup’, and code that runs continuous­ly in ‘loop’. Just before our setup we will add three lines. The first creates a variable, the pin number used to connect our ultrasonic sensor; the second line imports the LCD library; and the third tells the Arduino which pins connect the LCD. const int pingPin = 7; #include <LiquidCrys­tal.h> LiquidCrys­tal lcd(12, 11, 5,4, 3, 2);

Inside the setup section we create a serial connection, to debug any errors. We instruct the Arduino that we are using a 16-characterw­ide and 2-line LCD screen. On the screen we will print a “boot” message, then pause: void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd. print (“Booting ...”); delay(2000); }

Inside our loop is code that will check the distance from our sensor, and print it to the LCD. Add two variables: the duration of the sensor ping and the distance. These are float variables, as they have a decimal place. void loop() { float duration, cm;

To send an ultrasound ping, we need to configure ‘pingPin’ (pin 7) as an output, then ensure it is turned off, wait, turn the pin on, wait, and turn the pin off. pinMode(pingPin, OUTPUT); digital Write( ping Pin, LOW ); delay Micro seconds (2); digital Write( ping Pin, HIGH ); delay Micro seconds (5); digital Write( ping Pin, LOW ); Because we use the same pin to send and receive the ‘ping’, we next change the pin to an input then record receiving the reflected ping. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);

We now call upon a function we shall write later: this will take the time for our pulse to hit and reflect off an object and return to the sensor, and convert it into a distance. cm= micro seconds To C en ti meters( duration );

We’ll update the LCD with this distance. Setting the cursor to the top left (0,0) we print “Object Distance”, then we move to the next line and print the distance using the function ‘cm’. Moving 4 spaces along the line, we print “cm” to confirm our measuremen­t unit. lcd.setCursor(0, 0); lcd. print (“Object distance :”); lcd.setCursor(0, 1); lcd.print(cm); lcd.setCursor(4, 1); lcd.print(“cm”);

This data is also printed to the serial monitor for debug purposes. Serial.print(cm); Serial.print(“cm”); Serial.println();

For the final code inside the loop, we pause for 300 millisecon­ds before clearing the LCD, ready for the loop to update the distance. delay(300); lcd.clear();

Our last code is a function that converts the time taken for the ping to be sent, reflect and be received. This happens at 29 microsecon­ds per cm (0.029 millisecon­ds); we only need half this distance. float micro seconds To C en ti meters( float microsecon­ds) {

return microsecon­ds / 29.0 / 2.0; }

Your Arduino should be auto-detected by the editor, so you just need to click the Upload arrow in the toolbar, and your code will be uploaded to your ultrasonic tape measure.

 ??  ?? The circuit for this project looks quite tricky at first. The best advice is to follow each connection carefully and double-check as you go.
The circuit for this project looks quite tricky at first. The best advice is to follow each connection carefully and double-check as you go.
 ??  ?? Our LCD is 16x2, which means there are 16 characters per line, and 2 lines. Our Ultrasonic sensor is an HC-SR04.
Our LCD is 16x2, which means there are 16 characters per line, and 2 lines. Our Ultrasonic sensor is an HC-SR04.

Newspapers in English

Newspapers from Australia