DEV Community

Cover image for Arduino Nano 33 IoT - Ultrasonic Sensor - Servo Motor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Nano 33 IoT - Ultrasonic Sensor - Servo Motor

This build turns an ultrasonic distance sensor into a trigger for a servo motor on the Arduino Nano 33 IoT. Wave your hand in front of the sensor and the servo swings to 90 degrees; move away and it returns to 0. It's a compact example of reading an analog-ish sensor through timing and using that value to drive a motor, a pattern you'll reuse for all sorts of proximity-triggered hardware.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano 33 IoT - Ultrasonic Sensor - Servo Motor

What you need

Wiring

Arduino Nano 33 IoT - Ultrasonic Sensor - Servo Motor wiring diagram

Connect the ultrasonic sensor's TRIG pin to GPIO 5 and its ECHO pin to GPIO 4 on the Nano 33 IoT. The servo motor's signal pin goes to GPIO 2. If you want the wiring to stay put, a Screw Terminal Expansion Board lets you land each wire in a terminal instead of a breadboard, using the same pin numbers. Note that the board's 5V pin is actually VUSB, it only outputs 5V once you've closed the VUSB solder jumper on the bottom of the board and the board is powered over USB.

How it works

The ultrasonic sensor measures distance by sound. The Arduino sends a short pulse out on TRIG, the sensor emits an ultrasonic burst, and ECHO goes high for exactly as long as it takes the echo to bounce back. Reading that pulse width with pulseIn() and multiplying by a constant gives the distance in centimeters. The sketch compares this distance against a threshold of 50cm: closer than that, the servo rotates to 90 degrees; farther, it returns to 0.

Code

The loop fires a 10-microsecond pulse on TRIG_PIN, times the echo with pulseIn(), and converts that duration into centimeters. It then calls servo.write() with either 90 or 0 depending on whether the distance is under DISTANCE_THRESHOLD. Each reading is also printed to the Serial Monitor so you can watch the distance values update as you move your hand, which is handy for sanity-checking the sensor before trusting the servo's behavior.

arduino-nano-33-iot-ultrasonic-sensor-servo-motor.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-servo-motor
 */

#include <Servo.h>

#define TRIG_PIN  5  // The Arduino Nano 33 IoT pin connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN  4  // The Arduino Nano 33 IoT pin connected to Ultrasonic Sensor's ECHO pin
#define SERVO_PIN 2  // The Arduino Nano 33 IoT pin connected to Servo Motor's pin
#define DISTANCE_THRESHOLD  50 // centimeters

Servo servo; // create servo object to control a servo

// variables will change:
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       // initialize serial port
  pinMode(TRIG_PIN, OUTPUT); // set Arduino Nano 33 IoT pin to output mode
  pinMode(ECHO_PIN, INPUT);  // set Arduino Nano 33 IoT pin to input mode
  servo.attach(SERVO_PIN);   // attaches the servo on pin 9 to the servo object
  servo.write(0);
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if (distance_cm < DISTANCE_THRESHOLD)
    servo.write(90); // rotate servo motor to 90 degree
  else
    servo.write(0);  // rotate servo motor to 0 degree

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If the servo doesn't move, check that it's getting enough current, Arduino pins aren't meant to power a motor directly, and for multiple servos a driver like the PCA9685 is recommended to avoid overloading the board. If the Serial Monitor shows wildly inaccurate or zero distances, double check the TRIG and ECHO wiring, since swapping them gives pulseIn() nothing to measure.

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-servo-motor

Top comments (0)