# Part 13 – Distance Sensing with Ultrasonic Sensor and Arduino

### Part 13 was a variation of some of the work we had done in some of the previous labs. In this lab we got to use a good bit of pre-formatted code to test the operation of an Ultra Sonic or sound wave based sensor. The lab was just getting the sensor to work with some pre-created code. Here is the code I used to test …

/* HC-SR04 Sensor
* Date: 1/31/2017
* Author: Unknown/Leslie Scott Kerfoot
*
*/

// Define the global variables we will use.

const int trigPin = 2; // Assign the trigger pin for the sensor.
const int echoPin = 4; // Assign the echo reading pin for the sensor.
long duration,inches,cm; // These will be variables we’ll use in the main loop.
int distance; // As well as this…

// Note: I changed the pre-formatted code a little be because of the “cost”

//  associated with creating the variable each time through the loop.

void setup() {
Serial.begin(9600); // I also setup debugging
pinMode(trigPin,OUTPUT); // We’re sending the echo to the trigPin
pinMode(echoPin,INPUT); // We’re going to receive the distance from the echoPin
}

long microsecondsToInches(long microseconds) {

// According to the datasheet for the PING, there are
//   73.746 microseconds per inch (Sound travels at 1130 feet per
//   second).  This gives the distance travelled by the ping, outbound
//   and return. so we divide by 2 to get the distance of the obstacle which is half.
//
long myResult = ((microseconds / 72) / 2);
return myResult;
}

long microsecondsToCentimeters(long microseconds) {

// Using the same logic as the Inches sound travels at a speed of 340 meters/second
//   or 29 microseconds per centimeter. Since the ping goes out and back we divide by 2
//   to get just the distance to the ping
long myResult = ((microseconds / 29) / 2);
return myResult;
}

void loop() {

// Setup variable for measuring the duration of the ping
//   and the return in inches and cm.

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to make sure the sensor is ready.
// for a clean HIGH pulse.
digitalWrite(trigPin,LOW); // get the trigger to low.
delayMicroseconds(2); // wait a little bit.
digitalWrite(trigPin,HIGH); // Send the echo.
delayMicroseconds(10); // Pulse it for 10 Microseconds.
digitalWrite(trigPin,LOW); // End the echo pulse.

// Now we need to read the signal to measure the distance

duration = 0;
distance = 0;
duration = pulseIn(echoPin,HIGH);
distance = ((duration/2) / 29.1); // This is the raw distance in CM.

// Converte the time into a distance.

inches = microsecondsToInches(duration); // Run our function for converting to inches.
cm = microsecondsToCentimeters(duration); // Run our function for getting the CM.

// Output my results for testing purposes.

Serial.print(“Ping duration “);
Serial.print(duration);
Serial.print(” Inches “);
Serial.print(inches);
Serial.print(” cm “);
Serial.println(cm);

delay(100);

}

The additional part of the lab was to add code to the sensor to have the sensor run the a motor. Here is a picture of the circuit.

Here is the code that I used to do that….

/* HC-SR04 Sensor
* Date: 1/31/2017
* Author: Unknown/Leslie Scott Kerfoot
*
*/

// Define the global variables we will use.

#define motorPin 9 // Define the pin we’ll use to run the motor.
const int trigPin = 2; // Which pin is the trigger on the sensor.
const int echoPin = 4; // Which pin is the echo return on the sensor.
long duration,inches,cm; // Variables we’ll use in the main loop.
int distance;

void setup() {
Serial.begin(9600);j // Prepare for serial output/debugging.
pinMode(trigPin,OUTPUT); // We’re sending the echo to the trigPin
pinMode(echoPin,INPUT); // We’re going to receive the distance from the echoPin
pinMode(motorPin,OUTPUT); // Setup our motor pin for ourput.
}

long microsecondsToInches(long microseconds) {

// According to the datasheet for the PING, there are
//   73.746 microseconds per inch (Sound travels at 1130 feet per
//   second).  This gives the distance travelled by the ping, outbound
//   and return. so we divide by 2 to get the distance of the obstacle which is half.
//
long myResult = ((microseconds / 72) / 2);
return myResult;
}

long microsecondsToCentimeters(long microseconds) {

// Using the same logic as the Inches sound travels at a speed of 340 meters/second
//   or 29 microseconds per centimeter. Since the ping goes out and back we divide by 2
//   to get just the distance to the ping
long myResult = ((microseconds / 29) / 2);
return myResult;
}

void loop() {

// Setup variable for measuring the duration of the ping
//   and the return in inches and cm.

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to make sure the sensor is ready.
// for a clean HIGH pulse.
digitalWrite(trigPin,LOW); // get the trigger to low.
delayMicroseconds(2); // wait a little bit.
digitalWrite(trigPin,HIGH); // Send the echo.
delayMicroseconds(10); // Pulse it for 10 Microseconds.
digitalWrite(trigPin,LOW); // End the echo pulse.

// Now we need to read the signal to measure the distance

duration = 0;
distance = 0;
duration = pulseIn(echoPin,HIGH);
distance = ((duration/2) / 29.1); // This is the raw distance in CM.

// Converte the time into a distance.

inches = microsecondsToInches(duration); // Run our function for converting to inches.
cm = microsecondsToCentimeters(duration); // Run our function for getting the CM.

// Output our sensor results.

Serial.print(“Ping duration “);
Serial.print(duration);
Serial.print(” Inches “);
Serial.print(inches);
Serial.print(” cm “);
Serial.print(cm);

// Now run the motor and output debugging if detect something less than 5 in.
if (inches < 6) {
digitalWrite(motorPin,HIGH);
Serial.println(” Motor ON!”);
} else {
digitalWrite(motorPin,LOW);
Serial.println(” Motor OFF!”);
}

delay(100);

}

Here is a video of the circuit working. I put my hand in front. Note that the motor will run for at least 100 microseconds because of the delay at the end of the loop.

I also wrote some separate test code for Lab 13

const int trigPin = 2;
const int echoPin = 4;

void setup() {

// initialize serial communication:
Serial.begin(9600);
}

void loop()

{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print(“in, “);
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

delay(500);
}

long microsecondsToInches(long microseconds)

{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).  This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)

{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;

}

This site uses Akismet to reduce spam. Learn how your comment data is processed.