Trash bins that open automatically when approached, and when full, make bags ready to be thrown away, found buyers at prices far above their cost. In addition, automatic disinfectant machines provided contactless hygiene by pouring a certain amount of liquid into our palms when we held our hands under them. Automatic disinfectant machines took place on the shelves at prices well above their cost. These two products have similarities in terms of their working principles. In automatic disinfectant machines, a pump with an electric motor or a servo motor, dispenses the liquid. In automatic trash bins, a servo motor that opens the lid was used, and infrared or ultrasonic sensors were used to detect hand movement.
In this project, you will make an automatic stylish trash bin for your room using an ultrasonic sensor and servo motor with PicoBricks.
3: Pass the cables of the ultrasonic distance sensor through the hole in the box and connect them to the pins shown in the PicoBricks circuit diagram, make the servo motor and motor driver connections. 4: Fix the servo motor, PicoBricks and motor driver parts to the box with hot glue.
If everything went well, when you put your hand close to the garbage can, the lid of the bucket will open and it will close again after you throw the garbage away.
MicroBlocks Codes of the PicoBricks
You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.
MicroPython Codes of the PicoBricks
from machine import Pin, PWM
from utime import sleep
servo = PWM(Pin(21, Pin.OUT))
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
servo.freq(50)
servo.duty_u16(1920) # 15 degree
def getDistance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("The distance from object is ", distance, "cm")
return distance
while True:
sleep(0.01)
if int(getDistance()) <= 10:
servo.duty_u16(4010) # 70 degree
utime.sleep(0.3)
servo.duty_u16(1920)
Arduino C Codes of the PicoBricks
#include <Servo.h>
#define trigPin 14
#define echoPin 15
Servo servo;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(21);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 80) {
Serial.print(distance);
Serial.println(" cm");
servo.write(179);
} else if (distance < 180) {
Serial.print(distance);
Serial.println(" cm");
servo.write(100);
}
}
GitHub Project Page