Arduino uno +Hc-sr04 +Servo motor programming control



circuit-

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

Comments

  1. Sir AAP ke paje me jo programming cod hai usko hi copy karke device me daal sakte hain kya?

    ReplyDelete
    Replies
    1. Bilkul aap is Program ko Device me Upload kr sakte hai jaisa program aap Upload krenge usi tarh Device kaam kregi Kaise upload krte hai iske liye Read kre-How To Upload Program

      Delete

Post a Comment

1.जिस language में Page है उसी में Comment या Question करें जिससे सभी Users की help हो
2.Page के Title से Related Question करें
3.Page को अच्छी तरह Read करें उसके बाद Question करें
4.एक ही comment एक से ज्यादा बार न करें