Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Phuong Vo
Published © MIT

How to Measure Distance using Ultrasonic Sensor

This project will demonstrate ultrasonic sensors in action and how to use interrupts.

BeginnerFull instructions provided1 hour19,779
How to Measure Distance using Ultrasonic Sensor

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Jumper wires (generic)
Jumper wires (generic)
The amount of wires needed depends on the way you connect your circuit.
×15
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 221 ohm
Resistor 221 ohm
×1

Story

Read more

Schematics

Ultrasonic Sensor Schematic

Ultrasonic Sensor LCD

Interrupt Ultrasonic Sensor LCD

Code

Ultrasonic Sensor Code

Arduino
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
   
  // Sets the trigPin on HIGH state for 10 micro seconds
  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;
  
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(10);
}

Ultrasonic Sensor Code LCD Code

Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(1,3,4,5,6,7);

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distanceCm, distanceInch;

void setup() {
  lcd.begin(16,2);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;
  distanceInch = duration * 0.0113 / 2;
  
  lcd.setCursor(0,0); 
  lcd.print("Distance: "); 
  lcd.print(distanceCm); 
  lcd.print("cm   ");
  delay(10);
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
  lcd.print(distanceInch);
  lcd.print("inch ");
  delay(10);
}

Interrupt Ultrasonic Sensor LCD Code

Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(1,3,4,5,6,7);

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buttonPin = 2;

// defines variables
long duration;
int distanceCm, distanceInch;

void setup() {
  lcd.begin(16,2);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buttonPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(buttonPin), pin_ISR, FALLING);
}

void loop() {

}

void pin_ISR(){
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;
  distanceInch = duration * 0.0113 / 2;
  
  lcd.setCursor(0,0); 
  lcd.print("Distance: "); 
  lcd.print(distanceCm); 
  lcd.print("cm   ");
  delay(10);
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
  lcd.print(distanceInch);
  lcd.print("inch ");
  delay(10);
}

Interrupt Ultrasonic Sensor LCD Code Improved Code

Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(1,3,4,5,6,7);

// defines pins numbers
volatile bool buttonPressed = false;
const int trigPin = 9;
const int echoPin = 10;
const int buttonPin = 2;

// defines variables
long duration;
int distanceCm, distanceInch;

void setup() {
  lcd.begin(16,2);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(buttonPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(buttonPin), pin_ISR, FALLING);
}

void loop() {

  if (buttonPressed) {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;
  distanceInch = duration * 0.0113 / 2;
  
  lcd.setCursor(0,0); 
  lcd.print("Distance: "); 
  lcd.print(distanceCm); 
  lcd.print("cm   ");
  delay(10);
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
  lcd.print(distanceInch);
  lcd.print("inch ");
  delay(10);

  buttonPressed = false;
  }
}

void pin_ISR(){
  buttonPressed = true;
}

Credits

Phuong Vo
4 projects • 10 followers
Hi

Comments