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
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);
}
the library file is here https://www.dropbox.com/s/8edr0zga6j3lf09/HCSR04Ultrasonic.rar?dl=0
if you want to change your distance like this
No comments:
Post a Comment