Sunday, December 20, 2015

arduino ultrasonic sensor project

       Arduino ultrasonic sensor project 

the circuit is follow


in this project we program for when the distance <100cm the led is on 
and else led is off 

in this project we need 
  • arduino board
  • ultrasonic sensor
  • led 
in this project the distance can show on the serial monitor 
the code is follow 

#include <Ultrasonic.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13
int ledPin =10;
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
  {
    pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
  }

void loop()
  {
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();

  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  Serial.print("MS: ");
  Serial.print(microsec);
  Serial.print(", CM: ");
  Serial.print(cmMsec);
  Serial.print(", IN: ");
  Serial.println(inMsec);
  delay(100);
  if(cmMsec<100){
    digitalWrite(ledPin,HIGH);
  }else 
   digitalWrite(ledPin,LOW);
  }


if you want to change your distance like this


  if(cmMsec<100){
    digitalWrite(ledPin,HIGH);
  }else 
   digitalWrite(ledPin,LOW);
  }





No comments:

Post a Comment