0% found this document useful (0 votes)
5 views2 pages

Touchless Dustbin with Ultrasonic Sensor

Uploaded by

sagarshreyas8983
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Touchless Dustbin with Ultrasonic Sensor

Uploaded by

sagarshreyas8983
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Touchless Dustbin using Ultrasonic Sensor + DC Motor (L293D)

const int trigPin = 6;


const int echoPin = 5;

// L293D motor driver pins


const int motorPin1 = 13; // IN1
const int motorPin2 = 12; // IN2
const int enablePin = 11; // ENA (optional PWM speed control)

// Distance thresholds
const int openThreshold = 25; // cm
const int closeThreshold = 30; // cm

bool lidOpen = false;

void setup() {
[Link](9600);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);

stopMotor();
}

void loop() {
int distance = getDistance();
[Link]("Distance: ");
[Link](distance);
[Link](" cm");

if (distance > 0) {
if (!lidOpen && distance <= openThreshold) {
openLid();
lidOpen = true;
delay(2000); // Keep lid open for 2s
} else if (lidOpen && distance > closeThreshold) {
closeLid();
lidOpen = false;
}
}
}

// Measure distance in cm
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH, 30000);


if (duration == 0) return -1;

return duration * 0.034 / 2;


}

// Motor control functions


void openLid() {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 200); // Motor speed (0-255)
delay(500); // Time to fully open lid (tune this value)
stopMotor();
}

void closeLid() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin, 200);
delay(500); // Time to fully close lid
stopMotor();
}

void stopMotor() {
analogWrite(enablePin, LOW);
}

You might also like