Raspberry Pi & UltraSonic Sensor Distance Measurer

By | October 18, 2017

Hey folks ..welcome back

In the previous post, we learnt how we blink an LED with the help of Pi. In this tutorial, We will teach you how can we find the distance of a point. We will be using the ultrasonic sensor for this. There are a lot of ways in which you can use this code/ setup in your projects such as a motion detector, a pathfinder bot and many others.

Things required

  1. Pi 1/2/3
  2. Ultrasonic sensor HC-SR04
  3. Connectin wires(Female to Female)

The circuit

  1. Connect the Vcc of the sensor to pin no.2
  2. Connect GND of sensor to pin no. 6
  3. Connect Echo of the sensor to pin no. 12
  4. Connect trig of sensor to pin no. 16

Code

Code explanation

The basic explanation has already been discussed about the headers in the previous post. The algorithm is simple, pi notes the time when the signal was sent and then notes the time when it received the signal back, it subtracts both the values to get the time the sound waves required to travel from the sensor, hit an object and then come back to sensor, it is then multiplied by 1700 because speed of sound is 3400(approx) and the wave as gone and travelled back, so the original distance will be the half it actually travelled, so 3400/2 = 1700 and distance =speed * time, so we get the formula as mentioned above.

This is how you can make a basic range finder in minutes and this sensor has a lot of use even in complicated projects. Hope you learned something new and your day just became more productive as compared to a few minutes back. Live happy folks.