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>hello guys i need some help plz new user or arduino</p><p>need to control servo motor to move 90 degree and stop 5 sec and back again with proximity sensor i try this but didn't work thx for help</p><p>#include &lt;Servo.h&gt;</p><p>Servo myservo; //creates a servo object</p><p> //a maximum of eight servo objects can be created</p><p>int pos = 0; //variable to store servo position</p><p>//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)</p><p>int calibrationTime = 1;</p><p>//the time when the sensor outputs a low impulse</p><p>long unsigned int lowIn; </p><p>//the amount of milliseconds the sensor has to be low</p><p>//before we assume all motion has stopped</p><p>long unsigned int pause = 1000; </p><p>boolean lockLow = true;</p><p>boolean takeLowTime; </p><p>int pirPin = 12; //digital pin connected to the PIR's output</p><p>int pirPos = 13; //connects to the PIR's 5V pin</p><p>void setup(){</p><p> myservo.attach(4); //attaches servo to pin 4</p><p> Serial.begin(9600); //begins serial communication</p><p> pinMode(pirPin, INPUT);</p><p> pinMode(pirPos, OUTPUT);</p><p> digitalWrite(pirPos, HIGH);</p><p> //give the sensor time to calibrate</p><p> Serial.println(&quot;calibrating sensor &quot;);</p><p> for(int i = 0; i &lt; calibrationTime; i++){</p><p> Serial.print(calibrationTime - i);</p><p> Serial.print(&quot;-&quot;);</p><p> delay(1000);</p><p> }</p><p> Serial.println();</p><p> Serial.println(&quot;done&quot;);</p><p> //while making this Instructable, I had some issues with the PIR's output</p><p> //going HIGH immediately after calibrating</p><p> //this waits until the PIR's output is low before ending setup</p><p> while (digitalRead(pirPin) == HIGH) {</p><p> delay(500);</p><p> Serial.print(&quot;.&quot;); </p><p> }</p><p> Serial.print(&quot;SENSOR ACTIVE&quot;);</p><p>}</p><p>void loop(){</p><p> if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo</p><p> /*turns servo from 0 to 180 degrees and back</p><p> it does this by increasing the variable &quot;pos&quot; by 1 every 5 milliseconds until it hits 180</p><p> and setting the servo's position in degrees to &quot;pos&quot; every 5 milliseconds</p><p> it then does it in reverse to have it go back</p><p> to learn more about this, google &quot;for loops&quot;</p><p> to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn</p><p> **/</p><p> //for(pos = 0; pos &lt; 180; pos +=1) //goes from 0 to 180 degrees</p><p> { //in steps of one degree</p><p> myservo.write(0); //tells servo to go to position in variable &quot;pos&quot;</p><p> delay(5) ; //waits for the servo to reach the position</p><p> myservo.write(180) ; //to make the servo go faster, decrease the time in delays for</p><p> delay(5); //to make it go slower, increase the number.</p><p> }</p><p> if(lockLow){ </p><p> //makes sure we wait for a transition to LOW before further output is made</p><p> lockLow = false; </p><p> Serial.println(&quot;---&quot;);</p><p> Serial.print(&quot;motion detected at &quot;);</p><p> Serial.print(millis()/1000);</p><p> Serial.println(&quot; sec&quot;);</p><p> delay(50);</p><p> } </p><p> takeLowTime = true;</p><p> }</p><p> if(digitalRead(pirPin) == LOW){ </p><p> if(takeLowTime){</p><p> lowIn = millis(); //save the time of the transition from HIGH to LOW</p><p> takeLowTime = false; //make sure this is only done at the start of a LOW phase</p><p> }</p><p> //if the sensor is low for more than the given pause,</p><p> //we can assume the motion has stopped</p><p> if(!lockLow &amp;&amp; millis() - lowIn &gt; pause){</p><p> //makes sure this block of code is only executed again after</p><p> //a new motion sequence has been detected</p><p> lockLow = true; </p><p> Serial.print(&quot;motion ended at &quot;); //output</p><p> Serial.print((millis() - pause)/1000);</p><p> Serial.println(&quot; sec&quot;);</p><p> delay(50);</p><p> }</p><p> }</p><p>}</p>
<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

13,463views

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: