DIY Ultrasonic Distance Sensor
by UT CNS Maker Space in Circuits > Arduino
439 Views, 0 Favorites, 0 Comments
DIY Ultrasonic Distance Sensor
This project has been designed and built by Muhammad Khan and Nikafareed Papari of the Freshman Research Initiative Maker Space Stream at The University of Texas at Austin
Learn how to make a distance sensor that can be used to measure the distance between the sensor and a surface of your choice. This project focuses on building a low-cost ultrasonic distance sensor to measure water depth and capture precise data regarding water level fluctuations. This ultrasonic sensor transmits an ultrasonic wave towards the surface and senses how far away it is by calculating the time it takes to receive the signal back. Follow along to build a precise, functional, and automated ultrasonic distance sensor for a fraction of the price!
Supplies
To build ultrasonic sensor:
- Arduino Uno
- Breadboard
- Ultrasonic Distance Sensor
- Male to Male Breadboard Jumper Cables
- Arduino SD Card Holder Module
- SD Card
- 9V Battery Attachment
- 9V Battery
To build waterproof sensor housing box:
- 3D printer with PLA filament
- Four 10-24 x 4” screws
- Four 10-24 wingnuts
- Four #8 washers
- Silicone glue to seal sensor housing
Setup the Electronics
Follow the diagram to connect the ultrasonic sensor to the Arduino Uno. Plug the sensor into the breadboard and connect wires from the slots adjacent to the sensor to the corresponding slots on the Arduino. Then, attach the SD card holder module to the breadboard and attach jumper cables from the breadboard to the Arduino as shown in the image.
Code the Distance Sensor
This code enables the ultrasonic sensor to emit ultrasonic waves towards an object or surface and calculate its distance, displaying it in the Arduino app.
Open the Arduino app on your device.
You can download and upload the code file attached below
or
copy and insert this code:
// The following code has been developed by Muhammad Khan and Nikafareed Papari of the Freshman Research Initiative Maker Space Stream at The University of Texas at Austin.
#define echoPin 7 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 8 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(50);
}
Downloads
Add the Logging Code
This section of code enables the autonomous functionality of the sensor by allowing it to run via battery power and record and save data on the SD card. The logging code will allow you to save the code on the SD card and use the sensor without a computer as the sensor will execute the task of measuring and recording the distance on its own. Depending on the battery life of the 9V battery and the measurement interval that you set, the sensor should be able to log data for about 3 weeks on its own.
Open the Arduino app on your device.
You can download and upload the code file attached below
or
copy and insert this code before the void setup () section:
// The following code has been developed by Muhammad Khan and Nikafareed Papari of the Freshman Research Initiative Maker Space Stream at The University of Texas at Austin.
#include <SD.h>
#include <SPI.h>
File sdcard_file;
int CS_pin = 10; // Pin 10 on Arduino Uno
const int sensor_pin = 7;
float distance;
float output;
It allows the data captured to be stored on the Sd card in the SD Card holder attached to the breadboard.
Next, this code can be added to the void setup () section:
pinMode(sensor_pin, INPUT);
pinMode(CS_pin, OUTPUT);
Serial.begin(9600);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
Serial.print(" Distance ");
Serial.println(" ");
sdcard_file = SD.open("data.txt", FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(" Distance ");
sdcard_file.println(" ");
sdcard_file.close(); // close the file
}
Finally, this code can be added to the void loop () section:
output = analogRead(sensor_pin);
Serial.println(distance);
sdcard_file = SD.open("data.txt", FILE_WRITE);
if (sdcard_file) {
sdcard_file.println(distance);
sdcard_file.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
delay(3000);
Downloads
Upload the Code to the SD Card
To upload the code from your computer to the SD card, you will need to open the code in the Arduino app, compile it and then upload it. Then save the file to the SD card and place the SD card with the uploaded code into the SD card holder module on the sensor breadboard.
Once the SD card is in place, insert the 9V battery into the 9V battery attachment and plug in your 9V battery attachment into the Arduino Uno power inlet. The Arduino Uno should display a green light indicating that you have a functioning Ultrasonic Distance Sensor!
Make a Waterproof Box for Sensor
The sensor housing is a 3D printed waterproof box allowing you to deploy this sensor near water without worrying about the electronics getting water-damaged. The box is composed of two pieces: the main body and the lid. The two pieces lock together and are clamped together by four screws, ensuring an airtight waterproof seal. Additionally, silicone glue is used to seal the sensor transmitter and receiver at the bottom of the box and ensure waterproofing from all possible inlets.
We printed the ultrasonic sensor box on the Ultimaker S5 using Black Tough PLA filament and an infill percent of 100%. However, any 3D printer with the capability to print a box this size, and any filament that is able to keep its shape and not dissolve in general solvents should work.
Use the following link to access the files for the 3D printed waterproof box for the ultrasonic distance sensor: https://www.thingiverse.com/thing:5493302
Once your box has printed, follow the directions below to fit the ultrasonic sensor into the housing box.
- Insert the ultrasonic sensor and Arduino Uno first, keeping the receiver and transmitter aligned with the holes.
- Fit in the breadboard and 9V battery next according to the placement in the image.
- Use double sided tape to keep the breadboard and 9V battery in place
- Apply silicone glue around the ultrasonic sensor receiver and transmitter to seal the outlet holes.
- Once the silicone has dried and you are ready to use the sensor outdoors, switch the battery on to power on the sensor and proceed to the next step of closing the lid.
- Close the lid and use the four 10-24 x 4” screws, four 10-24 wingnuts, and #8 washers to tightly close the box. *Using wingnuts allows for easier opening and closing of the box but other types of nuts can also be used*
- The waterproof ultrasonic sensor is now ready to be deployed.
Questions and Support
This project is created by Muhammad Khan and Nikafareed Papari from the CNS Maker Space at the University of Texas at Austin.
For questions, suggestion, or inquiries regarding this project, email us at cnsmakerspace@gmail.com