ESP32: Pocket Size Distance Measuring and Logger

34,962

45

14

Introduction: ESP32: Pocket Size Distance Measuring and Logger

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.

Step 1: Materials and Tools

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

Step 2: Connecting the Ultrasonic Sensor

  • 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

Step 3: Coding and Uploading

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);
}

Step 4: Aim and Test!

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.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Knitting and Crochet Speed Challenge

      Knitting and Crochet Speed Challenge
    • New Year, New Skill Student Design Challenge

      New Year, New Skill Student Design Challenge
    • One Board Contest

      One Board Contest

    14 Comments

    0
    snkgsanskarnaman
    snkgsanskarnaman

    9 months ago

    An Important note for people confused about powering up the SR04 sensor.........You can power up the sensor with the USB connected to ESP32....just connect the Vcc of the sensor with VIN of ESP32........(3v3 would never be able to power up the ultrasonic sensor)

    0
    techbatman29
    techbatman29

    Reply 6 months ago

    Ya Makers, do note this point and make VCC changes for HC-SR04 Connection[Connect VCC of Ultrasonic Sensor with Vin of ESP32 (USB Connected Case) ] or else you may have to logic shift for 5V requirement of Sensor.
    Thanks snkgsanskarnaman.

    0
    Kristian5
    Kristian5

    3 years ago

    Worked fine when I used V in instead of 3,3 volt .
    Thank you for this guide and the code.

    0
    zach.vogl.35
    zach.vogl.35

    Reply 11 months ago

    do you mean 5V or Vext. Becouse i dont see a pin called V

    0
    jhdehart1
    jhdehart1

    Reply 1 year ago

    I was banging my head against the wall until I saw your comment. Thanks!

    0
    octrout
    octrout

    1 year ago

    Thank you for this tutorial and your code, I used it and it worked well!

    0
    DIYGuy33
    DIYGuy33

    1 year ago

    You should remove this instructable as the SR-04 is not compatible with an ESP. SR-04 requires 5v to work properly.

    0
    notronrj
    notronrj

    Tip 2 years ago

    The HC-SR04 is a 5V part and will not always work reliably on an ESP32 without level shifters. I would recommend using a RCWL-1601 instead which is drop-in compatible with the HC-SR04 but works on 3-5V.

    Nice write up though!

    0
    Kristian5
    Kristian5

    3 years ago

    Tried this but My HC-SR04 Sensor did not work with 3,3 volt that esp 32 deliver.
    Worked fine with my arduinio uno and 5 volts and of course different code.

    0
    ArduinoElectronics
    ArduinoElectronics

    4 years ago

    Hey TechMartian, Its an amazing project but I would recommend exterior designing by 3D Printing. It would be better if you even add a lcd display or oled small display. Keeping an external power supply would be even better.

    But still your project is amazing. Keep it up!!!

    0
    TechMartian
    TechMartian

    Reply 4 years ago

    I thought about this too! Unfortunately I don't own a 3D printer so I couldn't make it. Maybe if our team wins the makerspace contest I can make an Instructables on it!

    Thanks,

    Tech Martian

    0
    watchmeflyy
    watchmeflyy

    4 years ago

    Handy gadget! If I may offer a suggestion: it'd be nice to have some sort of indicator or laser attachment to clearly identify what object's distance is being measured. Might make the "pocket-sized" criteria a bit more difficult, but just a thought. :)

    0
    TechMartian
    TechMartian

    Reply 4 years ago

    Yes that's coming up but since it's gonna be big anyway gonna make it on my Arduino 101. Stay Tuned!