The HC-SR04 ultrasonic sensor measures distance by sending out a sound pulse and timing how long it takes to bounce back. Paired with an Arduino Nano 33 IoT, it gives you a simple way to detect how far away an object is, which is the basis for things like parking sensors, liquid level checks, or obstacle-avoiding robots.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano 33 IoT - Ultrasonic Sensor
What you need
- Arduino Nano 33 IoT
- Micro USB Cable
- Ultrasonic Sensor
- Breadboard
- Jumper Wires
- DC Power Jack
- Screw Terminal Expansion Board for Arduino Nano
- Breakout Expansion Board for Arduino Nano
- Power Splitter for Arduino Nano
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The HC-SR04 has four pins. VCC connects to the 5V supply and GND to ground. TRIG receives a start signal from the Arduino, and ECHO sends a signal back showing how long the sound pulse took, which the Arduino converts into a distance. In the examples, TRIG goes to pin D2 and ECHO to pin D3.
Note that on the Nano 33 IoT, the 5V pin is actually the VUSB pin. It only outputs 5V once you've closed the VUSB solder jumper on the bottom of the board, and only while the board is powered over USB.
How it works
The sensor works like a tiny sonar. The Arduino triggers it with a short pulse on TRIG, the sensor emits a sound wave, and it listens for the echo bouncing off whatever is in front of it. The time between sending and receiving the echo is proportional to the distance to the object, so by measuring that time in microseconds you can calculate how far away the object is.
Code
The sketch sends a 10-microsecond pulse on the TRIG pin using digitalWrite() and delayMicroseconds(). It then uses pulseIn() on the ECHO pin to measure how long the returning pulse lasts, in microseconds. That duration is converted into a distance in centimeters and printed to the serial monitor so you can watch readings update as objects move closer or farther away.
arduino-nano-33-iot-ultrasonic-sensor.ino
/*
* This Arduino Nano 33 IoT code was developed by newbiely.com
*
* This Arduino Nano 33 IoT code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-ultrasonic-sensor
*/
#define TRIG_PIN 2 // The Arduino Nano 33 IoT pin connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN 3 // The Arduino Nano 33 IoT pin connected to Ultrasonic Sensor's ECHO pin
float duration_us, distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
// Configure the trigger pin to output mode
pinMode(TRIG_PIN, OUTPUT);
// Configure the echo pin to input mode
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// Produce a 10-microsecond pulse to the TRIG pin.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse duration from the ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(500);
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-ultrasonic-sensor

Top comments (0)