loading

Arduino and ultrasonic sensor control servo and distance meter

In this project you can see how to control servo motor and tu calculate distance using arduino and ultrasonic sensor. You need: 1.Arduino (uno,nano,pro mini)

Hardware
Put the trigpin of ultrasonic sensor in pin 7 on arduino. Put the echopin of ultrasonic sensor in pin 6 on arduino. Put vcc pin of ultrasinic sensor to 5v on arduino. Put servo to pin 8 on arduino. Put gnd of ultrasonic to gnd on arduino. and connect arduino with Pc.

code:

#include <Servo.h>
#define trigPin 7

#define echoPin 6

Servo servo;

void setup()

{

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

servo.attach(8);

}

void loop()

{

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 5)

{

Serial.println("the distance is less than 5");

servo.write(120);

}

else

{

servo.write(0);

}

if (distance > 60 || distance <= 0)

{

Serial.println("The distance is more than 60");

}

else

{

Serial.print(distance);

Serial.println(" cm");

}

delay(500);

}

Step 1: See the Video

<p>Please remove the link for for your project and other projects. It redirects you to malicious websites.</p>
<p>Im sorry becuase my website is block</p>
<p>Nice Project, can you please give me the code. It's exactly what I need for a school project. </p>
<p>#include &lt;Servo.h&gt;</p><p>#define trigPin 7</p><p>#define echoPin 6</p><p>Servo servo;</p><p>int sound = 250;</p><p>void setup() {</p><p> Serial.begin (9600);</p><p> pinMode(trigPin, OUTPUT);</p><p> pinMode(echoPin, INPUT);</p><p> servo.attach(8);</p><p>}</p><p>void loop() {</p><p> long duration, distance;</p><p> digitalWrite(trigPin, LOW); </p><p> delayMicroseconds(2);</p><p> digitalWrite(trigPin, HIGH);</p><p> delayMicroseconds(10);</p><p> digitalWrite(trigPin, LOW);</p><p> duration = pulseIn(echoPin, HIGH);</p><p> distance = (duration/2) / 29.1;</p><p> if (distance &lt; 5) {</p><p> Serial.println(&quot;the distance is less than 5&quot;);</p><p> servo.write(120);</p><p>}</p><p> else {</p><p> servo.write(0);</p><p> }</p><p> if (distance &gt; 60 || distance &lt;= 0){</p><p> Serial.println(&quot;The distance is more than 60&quot;);</p><p> }</p><p> else {</p><p> Serial.print(distance);</p><p> Serial.println(&quot; cm&quot;);</p><p> }</p><p> delay(500);</p><p>}</p>
<p>nice project, can you give me the code of this project. It's exactly what i need for a school project! </p>
<p>yes see the code</p>
<p>can you provide the code please?</p>
<p>see the code</p>
<p>code :</p><p>#include &lt;Servo.h&gt;</p><p>#define trigPin 7</p><p>#define echoPin 6</p><p>Servo servo;</p><p>int sound = 250;</p><p>void setup() {</p><p> Serial.begin (9600);</p><p> pinMode(trigPin, OUTPUT);</p><p> pinMode(echoPin, INPUT);</p><p> servo.attach(8);</p><p>}</p><p>void loop() {</p><p> long duration, distance;</p><p> digitalWrite(trigPin, LOW); </p><p> delayMicroseconds(2);</p><p> digitalWrite(trigPin, HIGH);</p><p> delayMicroseconds(10);</p><p> digitalWrite(trigPin, LOW);</p><p> duration = pulseIn(echoPin, HIGH);</p><p> distance = (duration/2) / 29.1;</p><p> if (distance &lt; 5) {</p><p> Serial.println(&quot;the distance is less than 5&quot;);</p><p> servo.write(120);</p><p>}</p><p> else {</p><p> servo.write(0);</p><p> }</p><p> if (distance &gt; 60 || distance &lt;= 0){</p><p> Serial.println(&quot;The distance is more than 60&quot;);</p><p> }</p><p> else {</p><p> Serial.print(distance);</p><p> Serial.println(&quot; cm&quot;);</p><p> }</p><p> delay(500);</p><p>}</p>
<p>Can you Pleeeaaasssse provide the code? The link above takes me to a malicious site.</p><p>Thank you </p>
<p>can we build a variation of this which would control a servo using a very accurate maf mass airflow sensor from a car?</p>
<p>plz code</p>

About This Instructable

12,842views

13favorites

License:

More by Electronice:How to make lucid dream Arduino Control Servo Motor Using Ultrasonic Sensor Arduino and temperature sensor dht11 
Add instructable to: