Introduction: UCL - Embedded Automatic - Trashcan

About: A project by Simon. simo898p

We are two students from Automation Technology 2. semester at UCL, who has chosen a project with an arduino and have created a case/story for the project.

The initial idea was to make a automated trashcan who works in a environment with high hygiene requirement. With could be organic material, that start to rotten fast or chemist substances which had to be held in a closed container. The trashcan should open when you get near and be able to tell when it’s time to empty it.

Step 1: The Project

The trashcan is designed to open when a person is detected by a ultrasonic distance sensor. When the sensor haven’t detected anyone for 3 seconds it runs the motor backwards until it hits a push button. A temperature and humidity sensor (DHT) registers the condition in the trashcan and calculates when the contents will be rotten. A LCD displays time until it should be emptied.

Step 2: Parts List

1 - Arduino MEGA 2560

1 - Breadboard

1 - 10k ohm potentiometer

1 - LCD screen (compatible with Hitachi HD44780 driver)

1 - DHT11

3 - 10k ohm resistor

1 - 220 ohm resistor

1 - Motor control L298N with H-bridge

1 - 12 volt DC motor

1 - 12 volt power supply

1 - Ultrasonic sensor HC-SR04

1 - Push button

Step 3: Mechanical Setup

To open the trashcan we made a mechanical arm. It has two joints which is connected to the motor and the lid of the trashcan. Like in the first picture.

In the seconds picture it can be seen mounted on the motor.

For the motor we drilled some holes and used zip ties. It can be seen mounted inside the trashcan in the third picture. The arm is glued to the top of the lid

For the ultrasound rangefinder, we drilled 2 holes in the front of the trashcan of a diameter = 16mm with 1 cm between (see 4 and 5 picture).

For the rest of the components, we have just drilled holes for zip ties or screws. The LCD are mounted on the lid with bolts and knots after drilling holes for it. The arduino and motor driver with zip ties.

The arm parts is 3D printed. The schematics was made in Autofusion 360 and the following files were used:

Step 4: Fritzing

The first picture shows how the system is wired.

Step 5: The Code

The code is split into five different functions, each controlling a different sensor or part of one. In order to utilize the code a “new tab” must be created for each function.

The arduino project folder must contain the code for each function as a file.

In order for the program to run without interruption we use millis() and micros() instead of delay().

The program also requires the following libraries, which can all be found in the library manager:

LiquidCrystal v1.0.7 (built-in)

Adafruit Unified Sensor 1.0.2

DHT sensor library v1.3.0

The main program:

//Global variables are defined here. They're read through out the entire program.
//Local variables are defined within each function. They can only be changed or read within the function.

//Records whether the ultrasonic sensor has detected something. //It's controlled within the DistanceSensor function. It will stay on for four seconds when something is within range. //Everytime it registers something it refreshes the timer. boolean Distance_Close;

//Remembers if trashcan is open or closed. One is open and zero is closed. bool TrashcanState;

//Monitors for local variables. int ButtonMonitor; int DistanceMonitor; int CloseMonitor; unsigned long TimeOpenMonitor; unsigned long TimeUsedMonitor; int DetectedMonitor; int TemperatureMonitor; int HumidityMonitor;

//Sets the pin numbers. const int DCmotorIn1 = 5; const int DCmotorIn2 = 6; const int TrigPin = 8; const int EchoPin = 7; const int ButtonPin = 9; const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 22, d7 = 24; //Pin numbers for the LCD.

//Libraries, make sure to install all of them. //All libraries can be found inside the Library Manager. //"Adafruit Unified Sensor Library" must also be installed in order for dht to work even though it is not loaded. #include #include "DHT.h"

#define DHTPIN 3 //Defines which pin should be used for the temperature and humidity sensor #define DHTTYPE DHT11 //Defines which type of DHT is used. If you have a DHT22 replace DHT11 with DHT22.

DHT dht(DHTPIN, DHTTYPE); //Loads the defined pin and type.

LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Tells the program which pins are installed.

void setup() { // Code that needs to be run once: Serial.begin(9600); //Starts communication with the monitor. Make sure that the monitor have the same BAUD speed.

//Determine if pins are reading or writing. pinMode(TrigPin,OUTPUT); pinMode(EchoPin,INPUT); pinMode(DCmotorIn1, OUTPUT); pinMode(DCmotorIn2, OUTPUT); pinMode(ButtonPin,INPUT);

dht.begin(); lcd.begin(16, 2); //Sets the dimension of the LCD


void loop() { // put your main code here, to run repeatedly: DistanceSensor(); //Constantly checks if someone is near the trashcan OpenTrashcan(); //Checks if the trashcan should opened. CloseTrashcan(); //Checks if the trashcan should be closed. LCD(); TempHumid();

//Creates a table for monitoring all the different variables. Serial.print("Trashcan State : "); Serial.print(TrashcanState); Serial.print(" "); Serial.print("Distance_Close: " ); Serial.print(Distance_Close); Serial.print(" "); Serial.print("Button: " ); Serial.print(ButtonMonitor); Serial.print(" "); Serial.print("Temperature: " ); Serial.print(TemperatureMonitor); Serial.print(" "); Serial.print("Humidity: " ); Serial.print(HumidityMonitor); Serial.print(" "); Serial.print("Distance: " ); Serial.println(DistanceMonitor);

Function that controls the distance sensor:

int DistanceSensor(){
//Local variables used only in this function unsigned long TimeUsed; unsigned long Wait; unsigned long WaitExtra; unsigned long TimeOpen; bool Detected; long Duration; int Distance;

TimeUsed = micros(); //Reads how long the arduino have been on in microseconds.

//Writes the local variables into global variables for monitoring DetectedMonitor = Detected; DistanceMonitor = Distance;

//Make the ultrasonic sensor send out a sonic wave and starts two timers. //It is activated after 12 microseconds as the timer is exceeded and the function restarts. if (WaitExtra < TimeUsed){ digitalWrite(TrigPin,HIGH); //Sends out a sonic wave Wait = TimeUsed + 10; //Takes the current time and adds 10 microseconds to create a timer WaitExtra = TimeUsed + 12; //Takes the current time and adds 12 to create a timer }

//Turns off the sonic wave and reads how long it took for it to return. //Afterwards it calculates the distance it have traveled. //The distance is calculated in cm. if (Wait >= TimeUsed){ digitalWrite(TrigPin,LOW); // Stops the sonic wave Duration = pulseIn(EchoPin,HIGH); //Reads how long it took for the wave to return in microseconds. Distance = Duration*0.034/2; //Calculates the distance }

//Checks if anyone is within a certain distance. //This can be changed depending on when you want it to trigger. //Remember this can't be less than 2 cm (0.79 inches) unless you use a different sensor. if ((Distance >= 10) && (Distance <= 30)){ Detected = 1; //Something is detected } else{ Detected = 0; //Nothing has been detected }

//Allows the Distance_Close to be set if someone if within the distance set. //Refreshes if something is detected again. if (Detected == 1){ TimeOpen = TimeUsed + 4000000; //Refreshes or sets the time that must pass to reset Distance_Close. }

//Sets Distance_Close to 1, if the time set in detected hasn't been exceed. if (TimeOpen > TimeUsed){ Distance_Close = 1; }

//Sets Distance_Close to 0, if the time set in detected has been exceed. if (TimeOpen <= TimeUsed){ Distance_Close = 0; }


Function that controls the LCD:

int LCD(){
lcd.setCursor(0, 0); //Begins writing at the top row, from the beginning lcd.print("Temperature: "); //Writes "temperature" on the LCD lcd.print(TemperatureMonitor); //Writes the number measured by the DHT lcd.setCursor(0,1); //Begins writig at the bottom row, from the beginning lcd.print("Humidity: "); //Writes "humidity" on the LCD lcd.print(HumidityMonitor); //Writes the number measured by the DHT }

Function that opens the trashcan:

bool OpenTrashcan() {

//If someone is detected within a set range in DistanceSensor, the trashcan open. if ((TrashcanState == 0) && (Distance_Close == 1)){ //Sets the motor to run at full speed in a certain direction. In our case it is CLOCKWISE??? analogWrite(DCmotorIn1, 0); analogWrite(DCmotorIn2, 255);

//Stops the entire program to make sure it doesn't stop while opening. delay(600);

//After the delay is over the motor is turned off. analogWrite(DCmotorIn1, 0); analogWrite(DCmotorIn2, 0);

//Tells the rest of the program that the trashcan is open. TrashcanState = 1;

} }

Functions that closes the trashcan:

bool CloseTrashcan() {
//Local variable to control the button. int button; button = digitalRead(9); //Continually check if the button is pressed or not

//Local variable saved as a global varaible for monitoring ButtonMonitor = button;

//If the trashcan is open and no one has been within the range of the DistanceSensor for the set time the trashcan closes. if ((TrashcanState == 1) && (Distance_Close == 0)){

//Starts the motor at full power. It turns COUNTER CLOCKWISE??? analogWrite(DCmotorIn1, 255); analogWrite(DCmotorIn2, 0); }

//If the trashcan is open and the button is pressed, the trashcan will close. if ((button == 1) && (TrashcanState == 1)){ //Stops the motor analogWrite(DCmotorIn1, 0); analogWrite(DCmotorIn2, 0); TrashcanState = 0; //Sets the trashcan as open.

} }

Functions that reads the temperature and humidity:

int TempHumid(){
//Local variables used only in this function unsigned long TimeUsed; unsigned long Wait; TimeUsed = millis(); //Counts how long the program have been running in millis

//Reads the temperature and humidity every 2.5 seconds. if (Wait < TimeUsed){ int Humidity; int Temperature; Humidity = dht.readHumidity(); //Reads the current humidity in percentage. Temperature = dht.readTemperature(); //Reads the current temperature in celcius. HumidityMonitor = Humidity; // The humidity is saved in a global variable for monitoring. TemperatureMonitor = Temperature; // The temperature is saved in a global variable for monitoring. Wait = TimeUsed + 2500; //Takes the current time and adds 2500 milliseconds to create a timer. } }

For inspiration and help we have used the following sources:

Ultrasonic sensor


The code is based on an example provided with the DHT library. It’s named DHTtester.

L298 motor driver module


Step 6: Evaluation

The automatic trash can is functional but leaves a lot to be desired.

The first issue is that it can’t sense when it’s open. This can be resolved by attaching a servo instead of a DC motor, by having a button or similar which can detect when it opens.

The seconds issue is that the ultrasonic sensor has a hard time with angles and some materials are too sound absorbing. It can be solved by using a PIR sensor instead. If a PIR sensor is installed it could be angled to have a higher chance of detecting people.

The code is also missing the part that shows when the trashcan should be emptied and something that can tell when the trashcan have been emptied.