HC-SR04 Ultrasonic Sensor With Raspberry Pi

Introduction: HC-SR04 Ultrasonic Sensor With Raspberry Pi

About: Hello My name is Ahmed Darwish. I’m 39 years old, married and have two wonderful children (Abdulrahman and Emma). I’m an Egyptian electronics and telecommunications engineer. I live and work in State of Kuwa...

Hello every one ... my name is Ahmed Darwish ... this is my project using Raspberry Pi with the Ultrasonic sensor and I'd like to share it with you all. I have been asked to prepare a code that works on Python to control 8 ultrasonic sensors connected to a Raspberry Pi board. The system shall be connected to a screen via an HDMI and it should show something like a radar monitor.

The sensor I choose for such a project is the HC-SR04 sensor. I shall use only one sensor in this experiment and if you want more sensors to be connected to your Pi, you should take into consideration to provide an external power source of 5 V for the sensors instead of taking the power from the Pi.

Step 1: Contacts

So glad to hear feedback from you. Please don't hesitate to join my channels on:

Instagram: @simplydigital010

Twitter: @simply01Digita

Step 2: What Shall We Need for the Project?

First: for the Raspberry Pi:

- Raspberry Pi board

- HDMI cable

- TV or monitor with an HDMI port

- Mouse & Keyboard

- DSL connection for internet access

- 8 GB micro SD card

- Micro USB cable

Second: for the sensor:

- HC-SR04 sensor

- Breadboard of any size

- Connection wires (Male-Female)

- Resistors (1 k ohm & 2 k ohm)

Third: for you:

- Cup of coffee or glass of juice

- Nice chair

- Table or desk

Step 3: Preparing My Raspberry Pi Board to Work:

First I had to download the operating system from the web through (https://www.raspberrypi.org/downloads/noobs/). For more help to get familiar with the Raspberry Pi board for beginners, you can see the following page (https://www.raspberrypi.org/help/videos/).

After downloading the operating system, I made a copy on an 8 GB memory card in order to install it on the board and start working. The video above is really helpful for first time users and helps you during the installation.

Please note that the memory card SHOULD NOT be removed from the board for any reason. Otherwise that card shall not operate.

Step 4: Next Step: Setting Up Your System:

Now after you finish installing the system and the system is up, we can start the real work.

First we start with the breadboard and the sensor. This page is really helpful for this step (https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi). Fix the sensor on the breadboard as shown with the resistors and connect to your Pi. You may copy the code in the page to test your sensor. Just open your Python page and copy the code (get rid of the words in the top of the page). NOTE that this code is made for a single run. Meaning that the code shall record only one reading and then halt.

Now after the code is working fine with your system, you shall go for the next step that is to have a continuous running measuring. This page is really helpful for this step (https://electrosome.com/hc-sr04-ultrasonic-sensor-raspberry-pi/). Just go direct to the code because you don’t need the explanation any more as you got the idea from the first site. BUT you may need to read the comments as they have some useful tips.

Step 5: How to Show Figures?

The system is fine and the sensor readings are fine. Next step is to convert the readings into shapes with colors. First you have to download a library called matplotlib. Just open your Pi Command Prompt page and write: sudo apt-get install python-matplotlibor follow the instructions in this site. After installing the library, just test the code in this site. The code I mean is number 15 with a green check next to it.

Step 6: Final Step: Testing the Sensor and Make a Figure:

Please note that the code below is my own. I had to merge the continuous running sensor code with the figure plotting code to get it works as I want. You may change it as you want to suit your demands. Just copy the code to a new Python file and run it.

Be the First to Share

    Recommendations

    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020

    5 Discussions

    0
    Prakash ganesan
    Prakash ganesan

    3 years ago

    what is the use of the below line in the code?

    distance = round(distance, 2)

    0
    e_Darwish
    e_Darwish

    Reply 3 years ago

    Hi there,

    Thanks for your question.

    The waves go out of the sensor and reaches the target then get reflected back to the sensor again. So the distance from the sensor to the target shall be half the distance of the waves journey.

    You shall find your answer here:

    https://www.modmypi.com/blog/hc-sr04-ultrasonic-ra...

    The code is explained step by step.

    Darwish :)

    0
    e_Darwish
    e_Darwish

    Reply 3 years ago

    Sorry. I forget to say that the number 2 means we need the distcnce to be scaled down to 2 decimal places.

    0
    Swansong
    Swansong

    3 years ago

    Thanks for sharing :)

    0
    e_Darwish
    e_Darwish

    Reply 3 years ago

    :)