Ultrasonic Sensor DC Motor Arduino Code: Important Example

| | | | |

For the Ultrasonic Sensor DC Motor Arduino Code, you’ll need to connect both the ultrasonic sensor and the motor to your Arduino board. Then write the code to trigger motor movement based on distance measurements from the sensor.

Ultrasonic Sensor DC Motor Arduino Code - Visual representation of Arduino code for controlling a DC motor with an ultrasonic sensor.
Ultrasonic Sensor DC Motor Arduino Code – Illustrating how to control a DC motor using Arduino and an ultrasonic sensor.

What is an Ultrasonic Sensor?

An ultrasonic sensor is a device that utilizes sound waves at frequencies higher than the human audible range to measure distance, detect objects, or navigate within an environment. These sensors are commonly used in various applications, from industrial automation to robotics, due to their reliability, accuracy, and non-contact nature.

how does an ultrasonic sensor work?

Transmitter: The sensor consists of a transmitter that emits ultrasonic waves, which are sound waves with frequencies typically above 20,000 hertz (Hz), well beyond the range of human hearing.

Reflection: Once the ultrasonic waves are emitted, they travel through the air until they encounter an object. When the waves hit the object, they bounce back or reflect off it.

Receiver: The sensor also has a receiver that captures the reflected waves or echoes.

Time Measurement: The sensor measures the time it takes for the ultrasonic waves to travel from the transmitter, reflect off the object, and return to the receiver. This time measurement is crucial for calculating the distance between the sensor and the object.

Distance Calculation: Using the speed of sound in air (which is approximately 343 meters per second or 1125 feet per second at room temperature), the sensor calculates the distance to the object by multiplying the time taken for the round trip by half of the speed of sound.

The distance measured by the ultrasonic sensor is typically presented in centimeters or inches and can be used for various purposes, such as object detection, obstacle avoidance, or precise distance measurement. These sensors are especially popular in robotics for tasks like creating robots that can navigate through environments while avoiding collisions with objects.

Raspberry Pi vs Arduino

Ultrasonic Sensor DC Motor Arduino Code

Here’s a simple example code for this setup:.

// Include the Servo library to control the DC motor

#include <Servo.h>

// Create instances of the Servo library for motor control

Servo motor;

// Define the pins for the ultrasonic sensor

const int trigPin = 9; // Trigger pin

const int echoPin = 10; // Echo pin

// Variables for ultrasonic sensor

long duration;

int distance;

// Define the pin for the DC motor

const int motorPin = 6; // Connect the motor to pin 6

void setup() {

  // Initialize the motor


  // Initialize the ultrasonic sensor pins

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  // Serial communication for debugging (optional)



void loop() {

  // Trigger the ultrasonic sensor to send a pulse

  digitalWrite(trigPin, LOW);


  digitalWrite(trigPin, HIGH);


  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse

  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters (you can adjust this for your specific sensor)

  distance = duration * 0.034 / 2;

  // Print the distance to the serial monitor (optional)

  Serial.print("Distance: ");


  Serial.println(" cm");

  // Define a threshold distance for motor activation

  int thresholdDistance = 20; // Adjust this value as needed

  // Check if an object is within the threshold distance

  if (distance < thresholdDistance) {

    // If an object is detected, turn on the motor

    motor.write(90); // Set the motor to a specific angle (90 is the center position, adjust as needed)

    delay(1000); // Keep the motor on for 1 second (you can change the duration)

    motor.write(0); // Turn off the motor


  // Delay for a moment before taking the next measurement

  delay(100); // Adjust the delay time as needed


This Ultrasonic Sensor DC Motor Arduino code uses a simple threshold distance (defined by threshold distance) to determine when to activate the DC motor. When an object is detected within the specified threshold distance, the motor turns on for a specified duration (1 second in this example) and then turns off.

Ultrasonic Sensor DC Motor Arduino Code - Visual representation of Arduino code for precise control of a DC motor using an ultrasonic sensor.
Ultrasonic Sensor DC Motor Arduino Code – A step-by-step guide on controlling a DC motor with an ultrasonic sensor using Arduino.

Adjust the threshold distance and motor control parameters to suit your specific project requirements. Please make sure to connect your ultrasonic sensor and DC motor to the correct pins on your Arduino board and adjust pin assignments in the code if necessary.

Subscribe to our Newsletter “Electrical Insights Daily” to get the latest updates in Electrical Engineering. You can also Follow us on LinkedIn and Facebook to see our latest posts on Electrical Engineering Topics.

Worth Read Posts

  1. Ultrasonic Motor: A Comprehensive Overview
  2. Servo Motors: High-Precision Control in Modern Technology
  3. Arduino Stepper Motor Projects: A Quick Guide
  4. Induction Motor: Important Types, Construction & Working
  5. Wound Rotor Induction Motor: Working & Important Applications
  6. Squirrel Cage Induction Motor: Working and Best Applications
  7. Brushless Motor(BLDC): Construction, Types & Applications
  8. Split Phase Induction Motor: Working & Best Applications
  9. Stepper Motor Position Control with Arduino: A Quick Guide

Subscribe to our Newsletter “Electrical Insights Daily” to get the latest updates in Electrical Engineering. You can also Follow us on LinkedIn and Facebook to see our latest posts on Electrical Engineering Topics.

Related Posts

RN42 Bluetooth Module Arduino: Important Concepts

Using the RN42 Bluetooth module Arduino involves connecting the module to the Arduino…

DRV8825 Stepper Motor Driver: A Comprehensive Guide

The DRV8825 stepper motor driver, a technological marvel crafted by Texas Instruments…

A4988 Stepper Motor Driver with ESP32 Microcontroller: Important Guide

Using the A4988 Stepper Motor Driver with the ESP32 microcontroller is a straightforward…

A4988 Current Limit: How to Set A4988 Driver Current Limit?

Setting the A4988 current limit is crucial for ensuring optimal performance and preventing…

Raspberry Pi vs Arduino: ESP32, Beagle Board and PI PICO Best Guide

Within the sphere of open-source hardware, the comparative analysis between Raspberry Pi…

Servo Motor Arduino Code: Best Guide

Working with servo motors through Servo Motor Arduino code is like giving instructions to…
Sharing is Caring

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *