Subscribe to Arduino Academy and start learning today for FREE!

Ultrasonic Range Finding

Yesterday I got a Sainsmart HC-SR04 Ultrasonic Distance Sensor. This is a very simple to use and very inexpensive sensor that gives accurate range readings up to 400cm or so.

There are 4 wires to connect:

Gnd
Vcc (5vdc)
Trig (any digital pin)
Echo (any digital pin)

In this example, we will use pins 11 & 12.

Become the Maker you were born to be. Try Arduino Academy for FREE!

You need to download and install the Newping Library from https://code.google.com/p/arduino-new-ping/

Then upload the following sketch. This sketch takes the height and radius of a cylinder in centimeters, and outputs a fluid level in liters. The concept here is for a cistern. It tells you how many liters of water are left.

#include <NewPing.h>

#define trigPin 12
#define echoPin 11

const float height = 12.7; //cm
const float radius = 3.81; //cm
float totalCap;
float emptyCap;
float filledCap;

NewPing sonar(trigPin, echoPin);

void setup() {
Serial.begin(9600);
}

void loop() {
int distance = sonar.ping_cm();
//Serial.print(distance);
//Serial.println(“cm”);
totalCap =(3.142*(radius*radius)*height)*0.001; //Liters
emptyCap=(3.142*(radius*radius)*distance)*0.001; //Liters
filledCap =totalCap-emptyCap; //Liters
Serial.print(filledCap);
Serial.println(“Liters Remaining”);
delay(500);
}

Become the Maker you were born to be. Try Arduino Academy for FREE!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Archives

0
Would love your thoughts, please comment.x
()
x