Arduino and Ultrasonic Sensor Control Servo and Distance Meter

Published

Introduction: Arduino and Ultrasonic Sensor Control Servo and Distance Meter

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

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest

    14 Discussions

    It says error comping to board nano

    hello guys i need some help plz new user or arduino

    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

    #include <Servo.h>

    Servo myservo; //creates a servo object

    //a maximum of eight servo objects can be created

    int pos = 0; //variable to store servo position

    //amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)

    int calibrationTime = 1;

    //the time when the sensor outputs a low impulse

    long unsigned int lowIn;

    //the amount of milliseconds the sensor has to be low

    //before we assume all motion has stopped

    long unsigned int pause = 1000;

    boolean lockLow = true;

    boolean takeLowTime;

    int pirPin = 12; //digital pin connected to the PIR's output

    int pirPos = 13; //connects to the PIR's 5V pin

    void setup(){

    myservo.attach(4); //attaches servo to pin 4

    Serial.begin(9600); //begins serial communication

    pinMode(pirPin, INPUT);

    pinMode(pirPos, OUTPUT);

    digitalWrite(pirPos, HIGH);

    //give the sensor time to calibrate

    Serial.println("calibrating sensor ");

    for(int i = 0; i < calibrationTime; i++){

    Serial.print(calibrationTime - i);

    Serial.print("-");

    delay(1000);

    }

    Serial.println();

    Serial.println("done");

    //while making this Instructable, I had some issues with the PIR's output

    //going HIGH immediately after calibrating

    //this waits until the PIR's output is low before ending setup

    while (digitalRead(pirPin) == HIGH) {

    delay(500);

    Serial.print(".");

    }

    Serial.print("SENSOR ACTIVE");

    }

    void loop(){

    if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

    /*turns servo from 0 to 180 degrees and back

    it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180

    and setting the servo's position in degrees to "pos" every 5 milliseconds

    it then does it in reverse to have it go back

    to learn more about this, google "for loops"

    to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn

    **/

    //for(pos = 0; pos < 180; pos +=1) //goes from 0 to 180 degrees

    { //in steps of one degree

    myservo.write(0); //tells servo to go to position in variable "pos"

    delay(5) ; //waits for the servo to reach the position

    myservo.write(180) ; //to make the servo go faster, decrease the time in delays for

    delay(5); //to make it go slower, increase the number.

    }

    if(lockLow){

    //makes sure we wait for a transition to LOW before further output is made

    lockLow = false;

    Serial.println("---");

    Serial.print("motion detected at ");

    Serial.print(millis()/1000);

    Serial.println(" sec");

    delay(50);

    }

    takeLowTime = true;

    }

    if(digitalRead(pirPin) == LOW){

    if(takeLowTime){

    lowIn = millis(); //save the time of the transition from HIGH to LOW

    takeLowTime = false; //make sure this is only done at the start of a LOW phase

    }

    //if the sensor is low for more than the given pause,

    //we can assume the motion has stopped

    if(!lockLow && millis() - lowIn > pause){

    //makes sure this block of code is only executed again after

    //a new motion sequence has been detected

    lockLow = true;

    Serial.print("motion ended at "); //output

    Serial.print((millis() - pause)/1000);

    Serial.println(" sec");

    delay(50);

    }

    }

    }

    Please remove the link for for your project and other projects. It redirects you to malicious websites.

    1 reply
    0
    user
    Irisd7

    2 years ago

    Nice Project, can you please give me the code. It's exactly what I need for a school project.

    1 reply

    #include <Servo.h>

    #define trigPin 7

    #define echoPin 6

    Servo servo;

    int sound = 250;

    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);

    }

    0
    user
    Irisd7

    2 years ago

    nice project, can you give me the code of this project. It's exactly what i need for a school project!

    1 reply

    code :

    #include <Servo.h>

    #define trigPin 7

    #define echoPin 6

    Servo servo;

    int sound = 250;

    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);

    }

    Can you Pleeeaaasssse provide the code? The link above takes me to a malicious site.

    Thank you

    can we build a variation of this which would control a servo using a very accurate maf mass airflow sensor from a car?