Mini Project 51
Mini Project 51
Mini Project 51
Roll no: 51
MINI PROJECT
Objective: Use Arduino in order to detect an object using Ultrasonic Sensor and
alert using led
Circuit Design:
Code:
int distanceThreshold = 0;
int cm = 0;
int inches = 0;
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
distanceThreshold = 350;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
if (cm > distanceThreshold) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
FYMCA A
Roll no: 51
digitalWrite(4, LOW);
}
if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
if (cm <= distanceThreshold - 350) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(100); // Wait for 100 millisecond(s)
}
void setup() {
Serial.begin(9600);
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(buzzer_pin, OUTPUT);
pinMode(led_pin, OUTPUT); }
void loop() {
digitalWrite(trigger_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);
time = pulseIn(echo_pin, HIGH);
distance = (time * 0.034) / 2;
digitalWrite(buzzer_pin, HIGH);
digitalWrite(led_pin, HIGH);
delay(500);
} else {
Serial.println("Door closed");
Serial.print("Distance: ");
Serial.println(distance);
FYMCA A
Roll no: 51
digitalWrite(buzzer_pin, LOW);
digitalWrite(led_pin, LOW);
delay(500);
}
}
Output:
FYMCA A
Roll no: 51
FYMCA A
Roll no: 51