Saturday, December 12, 2015

arduino ultrasonic sensor ,simple working library

                                                    ULTRASONIC SENSOR PROJECT
       
the pin connection is here
the library download link is here 
http://j.gs/74qv

if we connect to the led in pin 8
we can swich the led with any distance for your like 
the code is follow 

#include <Ultrasonic.h>
#define ledPin  8
#define TRIGGER_PIN  12
#define ECHO_PIN    11

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

  if(cmMsec< 20){
   digitalWrite(8, HIGH);   }
  else{   digitalWrite(8, LOW);}}



if we have any problem please email,
wathu.eshan@gmail.com









1 comment: