Intro: HC-SR04 Ultrasonic Sensor With Raspberry Pi 2
Hello every one ... my name is Ahmed Darwish ... this is my first experience with the Raspberry Pi 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 VDC for the sensors instead of taking the power from the Pi.
Step 1: What Shall We Need for the Project?
First: for the Raspberry Pi 2:
- Raspberry Pi 2 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-Male & 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 2: 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 like me 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 3: 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 4: 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 5: 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.
HAVE FUN AND THANKS FOR READING MY PAGE. FOR ANY QUESTION PLEASE DON’T HESITATE TO CONTACT ME ON MY EMAIL: email@example.com