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 Kuwait…

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 ( For more help to get familiar with the Raspberry Pi board for beginners, you can see the following page (

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 ( 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 ( 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.