MicroPython: HC-SR04 Ultrasonic Sensor with Loron and Cellron Boards

    In this section, you will learn how to use the Loron and Cellron boards and the HC-SR04 Ultrasonic Sensor to obtain the distance to an object using MicroPython firmware. This tutorial covers how to connect the sensor Loron and Cellron boards and provides a simple MicroPython script to get and display the distance to an object.

Prerequisites

To follow this tutorial you need MicroPython firmware installed on our cards. You also need an IDE to write the code and upload it to your board. We recommend using the Thony IDE:

The necessary information to download the Thonny IDE is available from the documentation section of our site for Windows and MacOS.

HC-SR04 Ultrasonic Sensor Introduction

The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from 2 cm to 400 cm (0.8 inch to 157 inch) with an accuracy of 0.3 cm (0.1 inch), which is fine for most hobby projects. Also, this special module comes with ultrasonic transmitter and receiver modules.

The picture below shows the front and back of the HC-SR04 ultrasonic sensor.

HC-SR04 Ultrasonic Sensor Technical Data

The table below shows the key features and technical specifications of the HC-SR04 ultrasonic sensor.

Power5v DC
Working Current15 mA
Operating Frequency40 kHz
Max Distance4 m
Minimum Range2 cm
Measuring Angle15 degree
Trigger Input Signal10uS TTL pulse
Echo Output SignalTTL pulse proportional to Distance Interval
Dimensions45 mm x 20 mm x 15 mm

HC-SR04 Ultrasonic Sensor Output

VCC Powers the Sensor
Trig Trigger Input Pin
Echo Echo Output Pin
GND Common GND

How Does the HC-SR04 Ultrasonic Sensor Work?

The ultrasonic sensor uses sonar to determine the distance to an object. Here’s how it works:

The ultrasound transmitter (trig pin) emits a high frequency sound (40 kHz).

Sound propagates in air. If it finds an object, it returns to the module.

The ultrasound receiver (echo pin) receives the reflected sound (echo).

PERIPHERALS

We can calculate the distance to an object by taking into account the speed of sound in air and its travel time (the time since the signal was transmitted and received). Here is the formula:

Distance to an object = (( Speed of ound in air)* Time)/2
At 20ºC(68ºF) Speed of sound in air = 343m/s

Required Parts

You need the following parts to complete this tutorial:

  • HC-SR04 Ultrasonic Sensor
  • Loron or Cellron card
    breadboard
  • Jumper wires

Schematic

Connect the HC-SR04 ultrasonic sensor to your board as shown in the schematic diagram below. We connect the Trig pin to GPIO 5 and the Echo pin to GPIO 18, but you can also use other suitable pins.

Ultrasonic Sensor Loron or Cellron
VCC VIN
Trig GPIO 5
Echo GPIO 18
GND GND

HC-SR04 MicroPython Library

There are multiple ways to get the distance to an object using the HC-SR04 and Loron/Cellron boards using MicroPython firmware. We will use this HC-SR04 MicroPython Library, which makes it easy to interface with the sensor and take measurements.

The library we will be using is not part of the standard MicroPython library by default. So you need to install the following library on your board (save as hcsr04.py).

https://github.com/rsc1975/micropython-hcsr04 You can access the HC-SR04 MicroPython Library from this link.

Follow the next step for the Thonny IDE you are using:

Before moving on to the next section, make sure the MicroPython firmware is loaded on your card. You can follow one of the following tutorials:

TELL US TO INSTALL PORTS HERE

Install HC-SR04 library with Thonny IDE

Copy the library code to a new file. The HC-SR04 library code can be found here. (https://github.com/rsc1975/micropython-hcsr04)
Go to File > Save as….
Choose to save to “MicroPython device”:

4. Name your file as hcsr04.py and press the OK button:

And that’s it. The library has been uploaded to your clipboard. Go to File > Save as and select the MicroPython device to make sure it is successfully installed. Your file should be listed there:

After uploading the library to your clipboard, you can import the library and use the library functions in your code.

Code

After uploading the library to your clipboard, you can import the library and use the library functions in your code.

# First you need to import the required libraries:

#Import HCSR04 class from hcsr04 library.

#You also need to import the time library to add delays to our code.                      

from hcsr04 import HCSR04
from time import sleep

#Next, create an HCSR04 object named sensor that refers to the HCSR04 sensor.

#Pass the trigger pin, the echo pin, and the timeout as arguments.

sensor=HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)
while True:

#To get the distance in cm, simply call the distance_cm method on the sensor object.

#Save the result in the distance variable.

distance = sensor.distance_cm()

#Print the distance to Micropython shell.

print(‘Distance:’, distance, ‘cm’)

#Finally, we add a one second delay (distance updates every second):

sleep(1)

After uploading the code to your card, press the Run button to run the code. The distance to the nearest object will be printed.

We thank you.

FOLLOW US

Follow us on our social media accounts for our current news.