Arduino uno +Hc-sr04 +Servo motor programming control

circuit-


(adsbygoogle = window.adsbygoogle || []).push({});

Hc-sr04 vcc pin conect- Arduino pin 5v+
Hc-sr04 trigger pin     -Arduino pin13
Hc-sr04  echo pin       -Arduino pin12
Gdn-Gnd

servo yallow wire connect -Arduino Pwm pin9
servo Red wire connrect    -Arduino pin 5v+
servo Black or Brown wire connect -Gnd

Programming-

#include <Servo.h> 
Servo myservo; 

const int pingPin = 13;//pin13 से ultrasonic sound sensor की triggers pin को connect करें

int inPin = 12;//pin 12 से echo pin connect

int Limit = 10;

int pos=0;

void setup() {

  Serial.begin(9600); // initialize serial communication
  myservo.attach(9);//Pwm pin9 से servo का yallow wire connect
}

void loop()
{
  //raw duration in milliseconds, cm is the
  //converted amount into a distance
  long duration, cm;

  //sending the signal, starting with LOW for a clean signal
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  //setting up the input pin, and receiving the duration in
  //microseconds for the sound to bounce off the object infront
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  //printing the current readings to ther serial monitor
  Serial.print(cm);
  Serial.print(“cm”);
  Serial.println();

  //यदि ultrasound ping sensor के सामने कोई बस्तु की distance limit=10cm से कम है
  //तो condition true होगी और servo motor statement से रन होगी 
  if (cm > Limit)
  {
   for(pos = 0; pos <= 180; pos += 1) 
  {                                  
    myservo.write(pos);             
    delay(15);                       
  } 
  for(pos = 180; pos>=0; pos-=1)    
  {                                
    myservo.write(pos);              
    delay(15);                       
  } 
  }
  else
  {
   
  }

  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  //ultrasound wave की speed 340 m/s or 29 microseconds/centimeter है 
  
  return microseconds / 29 / 2;
}

Share करें

2 comments on “Arduino uno +Hc-sr04 +Servo motor programming control

Leave a Reply

Your email address will not be published. Required fields are marked *