Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

 Circuits Workshop Craft Cooking Living Outside Teachers Login | Sign Up

instructables circuits Projects Community CONTESTS CLASSES PUBLISH Let's Make... 

Control a Motor Using Ultrasonic Distance Sensors (HC-SR04)


By Simple-Circuits in Circuits > Arduino 62,445 39 1

Download Favorite

By Simple-Circuits
SimpleCircuits More by
the author:
Follow

About: Youtube: www.youtube.com/channel/UCh5aXlcWXmVQ4ipj0y3qjbw?&ab_channel=SimpleCircuits More About


Simple-Circuits »

Hello everyone! In this project we will be controlling the direction of a motor using two
ultrasonic ranging sensors.

If the right sensor is triggered, the motor will spin counter-clockwise.

If the left sensor is triggered, the motor will spin clockwise.

If both of the sensors or none of the sensors are triggered at the same time, the motor will
not spin

This simple project is helpful if you want to build a car that is capable of avoiding obstacles.

The components that were used in this project are:

Everything from my last Instructable which includes:

Two 9v Battery

countless M-F jumper wires

A Motor

One 5v relay switch module

and...

Two Ultrasonic range sensor

One Breadboard or protoshield with breadboard

One SainSmart UNO or Leonardo

Youtube version of this project will be available soon in this channel:

https://www.youtube.com/channel/UCh5aXlcWXmVQ4ipj0...

 Add Tip V Ask Question | Comment Download

Teacher Notes

Teachers! Did you use this instructable in your classroom?


Add a Teacher Note to share how you incorporated it into your lesson.

Add Teacher Note

Step 1: Wire the Breadboard + Ultrasonic Sensors

wire Connection for the Ultrasonic sensors:

Ultrasonic sensor 1&2 VCC -> arduino 5v

Ultrasonic sensor 1&2 GND -> arduino GND

Ultrasonic sensor 1 (US1 Right) TRIG -> arduino 12

Ultrasonic sensor 1 (US1 Right) ECHO -> arduino 11

Ultrasonic sensor 2 (US2 Left) TRIG -> arduino 9

Ultrasonic sensor 2 (US2 Left) ECHO -> arduino 10

I used staple clips instead of jumper wires for optimized cable management. This is
completely optional and you can use jumper wires as an alternative.

 Add Tip V Ask Question | Comment Download

Step 2: Connect the Relay Module + Motor

Wire connection for the Relay switches & motor

motor + or - -> relay1 comm pin

motor - or + -> relay2 comm pin

external power supply (9v) + -> Relay1 NO & Relay2 NO

external power supply (9v) - -> Relay1 NC & Relay2 NC

Relay module VCC -> arduino 5v

Relay module GND -> arduino GND

Relay module IN1 -> arduino pin# 7

Relay module IN2 -> arduino pin#13

This was covered in the previous Instructable where we connected the 9v battery to the
DC motor. In this project, we will be using the left and right sensors to control the direction
of the motor.

Unlike the previous Instructable, IN 1 is connected to digital pin#7 while IN 2 is still


connected to digital pin#13

 Add Tip V Ask Question | Comment Download

Step 3: Code!

Here is the link to the code.

Copy it and upload it to your board.

Don't forget to include the New pinglibrary. This allows you to communicate with multiple
ultrasonic sensors easily.

//motor control:

//0 is LOW

//1 is HIGH

// motor spin (clockwise)

// digitalWrite(Relay1, 1);

// digitalWrite(Relay2, 0);

// motor spin (counter-clockwise)

// digitalWrite(Relay1, 0);

// digitalWrite(Relay2, 1);

// motor(off) //

digitalWrite(Relay1, 0);

// digitalWrite(Relay2, 0);

// motor (off)

// digitalWrite(Relay1, 1);

// digitalWrite(Relay2, 1);

 Add Tip V Ask Question | Comment Download

Step 4: Done

If everything was done correctly, the rotation of the motor will be controlled by the objects
that trigger the ultrasonic sensors.

As always, you can modify the code and change the voltage for your own projects.

Control a Motor Using Ultrasonic Distance Sensors (HC-SR04) by Simple-Circuits Follow


If you want to see this project in action, please click on the link below:
Download Favorite w I Made It

Ultrasonic Range sensor motor test


Watch later Share

 Add Tip V Ask Question | Comment Download

Participated in the
View Contest
Move It

Be the First to Share


Did you make this project? Share it with us!

I Made It!

Recommendations

Embedded LED 3D Printed Christmas Arduino Wedding Photo Booth - 3D Arduino and Raspberry Pi Powered Pet
Internet of Things
Tree by maketvee in LEDs Printed Parts, Automated... by DIY Monitoring System by Technovation in
Class
Machines in Arduino Arduino
23,719 Enrolled
w 1  66 8 4.1K  105 8 7.8K  90 8 9.1K

 V |
Add Tip Ask Question Post Comment

We have a be nice policy.


Add Images Post
Please be positive and constructive.

Discussions

romilyalice 3 years ago


Reply / Upvote
Hi,
I'm wanting to make a circuit whereby a motor is controlled using a proximity sensor. The closer an
object gets to the sensor, the further to the right the motor turns, as the object moves further away, the
motor graduates to the left. Any chance you could help me to work out how to do that?
Thanks so much!

Post Comment

Categories About Us Resources Find Us

 Circuits  Living Who We Are Sitemap

 Workshop  Outside Why Publish? Help

 Craft  Teachers Jobs Contact

 Cooking

© 2019 Autodesk, Inc. Terms of Service | Privacy Statement | Privacy settings | Legal Notices & Trademarks

You might also like