ESP32: Pocket Size Distance Measuring and Logger

by TechMartian in Circuits > Microcontrollers

43379 Views, 52 Favorites, 0 Comments

ESP32: Pocket Size Distance Measuring and Logger

J3qRcsK%QXGDDYkB7jbquQ_thumb_766.jpg
ESP32 Ultrasonic Sensor

This is a sonic (sound-based), pocket-size measuring tool, accurate up to 3mm. It is useful for applications where you want to log the distances or find distances that are inaccessible, therefore measuring tapes, rulers, and callipers are out of the question!

Since the ESP32 is such a tiny micro controller powered by a micro usb port, you can grab a small power bank, plug it in, and start measuring distances normally inaccessible with conventional means of measuring i.e. rulers.

Materials and Tools

UNADJUSTEDNONRAW_thumb_761.jpg

  • ESP32
  • Ultrasonic Sensor (HC-SR04)
  • Breadboard
  • Breadboard wires

Connecting the Ultrasonic Sensor

PqZ+QzBsS8ayxq7rKMCULg_thumb_763.jpg
ZJg682n4TqqKWKdURH+feA_thumb_764.jpg
klHmxjWWQcSju9Veh1t7oA_thumb_765.jpg
iOOkXjlDS2+Uj7G3%+R07A_thumb_762.jpg

  • Connect the VCC pin to 3.3V on the ESP32
  • Connect the GND pin to ground on the ESP32
  • Connect the Trig pin to D2 on the ESP32
  • Connect the Echo pin to D5 on the ESP32

Coding and Uploading

Screen Shot 2017-08-18 at 11.14.17 PM.png

Plug in your ESP32 to the computer and upload the following code to the ESP32 board. Make sure you have selected the correct board and port both will be labeled with ESP32.

// defines pins numbers
const int trigPin = 2;
const int echoPin = 5;
// 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);
}

Downloads

Aim and Test!

J3qRcsK%QXGDDYkB7jbquQ_thumb_766.jpg
cOUZYPpOQCSC23%b8Zd6ng_thumb_767.jpg

Aim it at a distant object, like the wall or ceiling and measure! Try blocking it with your hand and watch the values change accurately and precisely.