Introduction: Setting Up an Ultrasonic Sensor for Arduino

This instructable will guide you through setting up and calibrating an ultrasonic sensor for use with an Arduino system in order to accurately measure distance.

Step 1: Setting Up the Breadboard

Connect the ultrasonic sensor to the Arduino system by attaching wires from the Arduino pins to holes on the breadboard which attach to the corresponding pins on the ultrasonic sensor. The pins should be connected as follows:

  • 5V (Arduino) ↔ VCC (ultrasonic)
  • GND (Arduino) ↔ GND (ultrasonic)
  • Any digital pin (2 in this example) (Arduino) ↔ TRIG (ultrasonic)
  • Any other digital pin (4 in this example) (Arduino) ↔ ECHO (ultrasonic)

Step 2: Coding the Ultrasonic Sensor

Now, to ensure that the ultrasonic sensor is working properly, make a program which makes the ultrasonic sensor emit a pulse and store the delay as a variable. The following code assumes you have the TRIG pin connected to the 2 pin and the ECHO pin connected to the 4 pin, and it prints the delay of the signal in microseconds to the serial output (Ctrl + Shift + M in your Arduino window).

const int TRIG = 2; // arduino pin from which the ultrasonic sensor gets its input<br>const int ECHO = 4; // arduino pin to which the ultrasonic sensor sends its output
long duration;      // create a variable for the duration; the initial value does not matter<br>void setup() {
  pinMode(TRIG, OUTPUT); // sets the TRIG pin as the arduino board's output (the ultrasonic sensor uses this as its input)
  pinMode(ECHO, INPUT);  // sets the ECHO pin as the arduino board's input (the ultrasonic sensor uses this for an output)
  Serial.begin(9600);    // starts serial communication
}

void loop() {
  digitalWrite(TRIG, LOW);                  // send a low pulse
  delayMicroseconds(2);                     // for 2 microseconds
  digitalWrite(TRIG, HIGH);                 // send a high pulse
  delayMicroseconds(10);                    // for 10 microseconds
  digitalWrite(TRIG, LOW);                  // send a low pulse again (will work without this line)
  duration = pulseIn(ECHO, HIGH);           // get the output of the ultrasonic sensor from the echo pin and store the voltage as a variable
  Serial.print("Delay in μs: " + duration); // print the delay of the pulse in microseconds
  Serial.println();                         // print a new line
  delay(100);                               // wait 0.1 seconds before finding the delay again
}

Step 3: Calibrating the Ultrasonic Sensor

Once your ultrasonic sensor is returning numbers, calibrate it by placing an object (such as a board) a known distance away using a ruler, and repeat for multiples of that distance. Make a table of your data, and make a graph plotting delay versus distance. There should be a rough linear relationship between the two values. Using Excel or Google Sheets, use the LINEST function to get the slope and y-intercept of the graph. To do this, have a column for the delays and a column for the distances, then type "=LINEST(", highlight the column of delays, type a comma ",", highlight the column of distances, and press enter. That box should become the slope of the trendline, and the box next to it should become the y-intercept. Try plugging in some of the distances you used to the equation "(slope) × distance + (y-intercept) = delay"; the numbers you get should be close to the delays you received during your testing.

The images above show some possible results by taking measurements every 5 centimeters until the target is 30 cm from the sensor.

Step 4: Finalizing Your Code

Now that you have the equation of the trendline of the relationship between distance and delay, you can calculate the distance by solving for it in the previous equation. By doing so, you get "distance = (delay - (y-intercept)) ÷ (slope)". Add some constants into your code for the trendline's slope and intercept, and modify your Serial.print() line to use the above equation to output distance. The example below uses values from the example in the previous step to output the distance in centimeters.

const int TRIG = 2;                      // arduino pin from which the ultrasonic sensor gets its input
const int ECHO = 4; // arduino pin to which the ultrasonic sensor sends its output long duration; // create a variable for the duration; the initial value does not matter const int trendlineSlope = 57.1314; // the slope of the line from calibrating the sensor const int trendlineIntercept = -99.4667; // the intercept of the line from calibrating the sensor
void setup() { pinMode(TRIG, OUTPUT); // sets the TRIG pin as the arduino board's output (the ultrasonic sensor uses this as its input) pinMode(ECHO, INPUT); // sets the ECHO pin as the arduino board's input (the ultrasonic sensor uses this for an output) Serial.begin(9600); // starts serial communication }
void loop() { digitalWrite(TRIG, LOW); // send a low pulse delayMicroseconds(2); // for 2 microseconds digitalWrite(TRIG, HIGH); // send a high pulse delayMicroseconds(10); // for 10 microseconds digitalWrite(TRIG, LOW); // send a low pulse again (will work without this line) duration = pulseIn(ECHO, HIGH); // get the output of the ultrasonic sensor from the echo pin and store the voltage as a variable Serial.print("Distance in cm: " + (duration - trendlineIntercept) / trendlineSlope); // print the distance to the target, in centimeters Serial.println(); // print a new line delay(100); // wait 0.1 seconds before finding the distance again }

Step 5: Try It Out!

Your ultrasonic sensor is now ready to be used to accurately measure distance (depending on the accuracy of your calibration). Now that you have it working, try it out at more distances. You could do further testing to check the precision as distance increases, re-check the distances you used to calibrate it at first, or use it for some other project.

I hope you enjoyed this instructable!