Table of Contents
Introduction
In this tutorial we will interface HCSR04 ultrasonic sensor with raspberry pi pico and find the distance of the object Infront of the sensor. This will include two parts first we will check the sensor data in serial monitor and then we will display the distance data in an SSD1360 0.96″ OLED display.
Ultrasonic sensor uses ultra sound which is a 40KHz frequency sound wave is transmitted from the module and whenever this sound wave bounce back from an object or an obstacle. The time taken to receive the sound wave is calculated and then using the velocity formulae we will calculate the distance. So, the ultrasonic sensor is used to find out the distance of any obstacle or an object Infront of the sensor.
Hardware Requirements
Disclaimer: It may contains Amazon affiliate links. which means I can get a small commission on the sale. This might be your indirect help to me. Thank You 🙏
- Raspberry Pi Pico Board (HERE)
- Micro-USB Cable (HERE)
- Ultrasonic Sensor HC-SR04 (HERE)
- 0.96″ OLED Display (HERE)
- Breadboard (HERE)
- Connecting Wires (HERE)
Software Requirements
About HCSR04 Ultrasonic Sensor
The HC-SR04 sensor uses ultra sound / sonar to find out the distance of an object. It emits short ultrasonic burst and try to receive the reflected sound to find the distance. The sensor is consists of an ultrasonic transmitter and a receiver. The transmitter transmit a burst of sound waves at a frequency of 40khz and the receiver receives the reflected sound. The time between transmission and reception of the sound wave is proportional to the distance of the object. The sensor calculates this time and converts it into distance value. The sensor output can be converted to the desired distance measurement units.
Pinout of HC-SR04 sensor
HCSR04 | Description |
VCC | 5v |
GND | Common Ground |
Trig | OUTPUT |
Echo | INPUT |
How ultrasonic sensor works?
As we know the ultra sonic burst is nothing but the sound wave and sound wave travels at a speed of 340 m/s. And it is the basic speed of sound in air. So, we can calculate the distance by using the formulae
Distance = Velocity of sound x The total time taken to receive back the sound wave.
So, the logic is we need to calculate the time take to hit the object and we can achieve this by calculating the total time take by the sound wave. The sensor will measure the total time taken to hit the object and receive back. So, to get the time only for the hitting to the object we need to divide by 2.
SSD1306 OLED Display
The Organic Light Emitting Diode(OLED) is an 0.96 inch, 128×64 resolution display with thin multi layered organic film sandwich between anode and cathode. It is based on self emitting diode technology and it does not require any back light like LCD. This module is interfaced using SPI communication that requires only two wire of communication. This display can be interfaced with any microcontroller using SPI/IIC communication protocols.
SSD1306 OLED | Description |
VCC | 3.3v – 5v |
GND | Common Ground |
SCK | Serial Clock |
SDA | Serial Data |
Circuit Diagram
Circuit Explanation
Follow the circuit diagram and build your circuit correctly.
HC-SR04: The sensor TRIG pin is connected to pin number GP19 and the ECHO pin is connected to pin number GP18 of the Raspberry Pi Pico board. And the VCC and GND pin of the sensor is connected to the VSYS and GND of the Raspberry Pi Pico board respectively.
OLED Display: Connect the SCL and SDA pin of the display to the Raspberry Pi Pico GP17 and GP16 respectively. And the VCC and GND pin of the sensor is connected to the VSYS and GND of the Raspberry Pi Pico board respectively.
VSYS is the system voltage here you can supply external 5v input also and GND is the common ground of the board.
Source Code Explanation
MicroPython Code: The code we are using here is a composition of mainly three parts.
- OLED display library code.
- SSD1306 library
- The main code (Download HERE)
The OLED library code: (oled need to install from Thonny IDE)
To install oled library follow the steps. In Thonny IDE go to Tools-> Manage Packages-> search “oled” for the library find the “micropython-oled” library from the search items by the author: Yeison Cardona and you can find an Install button below, just click and install it. It will be installed inside the Raspberry Pi Pico. So, no need to check, It will work.
SSD1306 display library code:(ssd1306.py)
Now create a new file in Thonny IDE and open the ssd1306.txt file and copy the code to the new file and save it as a ssd1306.py file in the Raspberry Pi Pico board. This is the SSD1306 library file.
Main Code: (main.py)
So, this is the final code that is responsible for the sensor data fetching and displaying the data to the OLED display. Now create one more file in the Thonny IDE and copy/paste the code given in the download section as sensorDisplayData.txt and save it as a main.py file in the Raspberry Pi Pico board.
Why the main.py file name?
You can give any name to the file but when running the code from Thonny IDE by clicking the green button it will run once. But when we give the name main.py to the file, the Raspberry Pi Pico board will automatically detect and run the code whenever you want. You can say that this is a process of AutoStart system.
Video Tutorial
Download Source Code
Download the complete source code: click here