Monday 20 April 2020

USING SERVO MOTOR WITH ULTRASONIC SENSOR AND ARDUINO


COMPONENTS REQUIRED:

1. ULTRASONIC SENSOR(HC-SR04)

2. SERVO MOTOR

3. ARDUINO UNO BOARD


CONNECTIONS:


CONNECTIONS

copy code from here

#include <Servo.h>
Servo servo1;
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
void setup() 
{
servo1.attach(7); 
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);// put your setup code here, to run once:
}
void loop()
{
  ultra_sonic();
  servo1.write(90);
  if(distance <=9)
  {
  servo1.write(270);
  }
}
void ultra_sonic()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  }

4 comments:

  1. thanks man the code came through...would it be possible to intergrade it with a load cell weight sensor 1kg

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Good day i'm not getting my swervo motor to work just the swervo is working

    ReplyDelete