Arduino MicroPython Ultrasonic Sensor

This tutorial teaches you how to measure the distance to an object using an ultrasonic sensor with an Arduino and MicroPython. Specifically, we will cover:

Arduino MicroPython ultrasonic sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Ultrasonic Sensor

The HC-SR04 ultrasonic sensor measures the distance to objects using ultrasonic waves. It sends out a ultrasonic wave that humans can't hear and listens for the echo that returns after hitting an object. By measuring the time it takes for the ultrasonic wave to come back, the sensor determines the distance from it to the object.

Pinout

The HC-SR04 ultrasonic sensor includes four pins.

  • VCC pin: Attach this pin to VCC (5 volts).
  • GND pin: Attach this pin to GND (0 volts).
  • TRIG pin: Link this to the Arduino to send control signals.
  • ECHO pin: This sends signals back to the Arduino, which detects these signals to determine distance.
Ultrasonic Sensor Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino MicroPython Ultrasonic Sensor

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • You can use any digial I/O pin of Arduino Giga to connect directly to the TRIG pin of ultrasonic sesor.
  • You can use any digial I/O pin of Arduino Giga to connect directly to the ECHO pin of ultrasonic sesor, except for the following pins: D5, D76, D77, D79, D84, D85, A0, A1, and A3. This is because these specific pins are only 3.3V tolerant. In case you need to use these pins, please use a voltage divider to reduce the 5V output from the sensor to 3.3V or lower, ensuring the voltage stays within the safe operating range for those pins.

Arduino MicroPython Code

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ultrasonic-sensor """ from DIYables_MicroPython_Ultrasonic_Sensor import UltrasonicSensor import time TRIG_PIN = 'D3' # The Arduino Giga WiFi pin D3 connected to TRIG pin of the ultrasonic sensor ECHO_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to ECHO pin of the ultrasonic sensor sensor = UltrasonicSensor(trig_pin=TRIG_PIN, echo_pin=ECHO_PIN) sensor.set_detection_threshold(140) # Set detection threshold to 140 cm sensor.enable_filter(num_samples=20) # Enable filtering and set number of samples to 20 while True: sensor.loop() # Perform measurement cycle distance = sensor.get_distance() if not distance: print('No object detected within the set distance') else: # Print the distance formatted to two decimal places using format() print('Distance: {:.2f} cm'.format(distance)) time.sleep(0.1)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the ultrasonic sensor as shown in the above diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • In Thonny IDE, Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Ultrasonic-Sensor”, then find the Ultrasonic Sensor library created by DIYables.
  • Click on DIYables-MicroPython-Ultrasonic-Sensor, then click Install button to install Ultrasonic Sensor library.
Arduino MicroPython Ultrasonic Sensor library
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Wave your hand in front of the ultrasonic sensor.
  • Check the Shell at the bottom of Thonny IDE for the Arduino output.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot No object detected within the set distance No object detected within the set distance No object detected within the set distance No object detected within the set distance Distance: 9.90 cm Distance: 10.34 cm Distance: 10.70 cm Distance: 11.83 cm Distance: 13.66 cm Distance: 14.18 cm Distance: 16.00 cm Distance: 17.64 cm Distance: 19.64 cm Distance: 21.21 cm Distance: 21.41 cm No object detected within the set distance No object detected within the set distance No object detected within the set distance
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Code Explanation

You can find the explanation in the comments of the Arduino MicroPython code mentioned above.

Video Tutorial

Ultrasonic Sensor Applications

  • Avoiding Accidents
  • Ensuring Capacity
  • Measuring Elevation
  • Detecting Proximity

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!