Smart Bin

1. Introduction

What it does The “Smart Bin” is an automated trash receptacle that opens its lid automatically when it detects a user's hand approach. Additionally, it continuously monitors the internal trash level, providing comprehensive feedback through an LCD display (showing the exact fill percentage and system status), an LED interface (Green, Yellow, Red for quick visual capacity indication), and an active buzzer (emitting an auditory warning while refusing to open if the bin is completely full).

The purpose of the project The main purpose is to create a hygienic, hands-free waste disposal experience while preventing the common issue of overfilling the bin.

The initial idea The idea stems from the everyday inconvenience of disposing of messy waste (e.g., while cooking) without dirtying the bin's lid. Furthermore, in shared spaces (offices, kitchens), bins often get overfilled because people don't realize the capacity is reached until they force the lid open. Combining motion-sensing opening with a precise, screen-based capacity-monitoring system solves both issues effectively.

Why it is useful This project is highly useful for improving hygiene and sanitation in environments like hospitals, kitchens, or public restrooms by eliminating cross-contamination. For the user, the addition of an LCD display provides clear, precise data regarding the bin's status and when it's time to empty the trash, ultimately promoting better waste management habits.

2. General Description

The system is built around a central microcontroller (AVR ATmega328p) which acts as the main processing unit, orchestrating the inputs and outputs through a non-blocking state machine design. The main loop evaluates sensor data every 500ms to ensure real-time responsiveness without overloading the processor.

Interaction Flow: The system operates in three main states: CLOSED, OPEN, and COOLDOWN.

  • Input Layer: Two HC-SR04 ultrasonic sensors act as the “eyes” of the system. The exterior sensor continuously scans for a user's hand (triggering at a distance of ⇐ 20cm). The interior sensor, mounted under the swing-top lid, measures the distance to the trash. To ensure accurate readings (since the sensor tilts when the lid opens), the internal measurement is continuously polled only while the system is in the CLOSED state.
  • Processing Layer: The microcontroller receives the distance data. It features a self-healing logic failsafe: if the interior sensor reads a distance greater than the bin's physical depth, it clamps the value to prevent state deadlocks. It calculates the internal trash percentage dynamically. If a hand is detected, it evaluates this percentage to decide the next state.
  • Output Layer: * Mechanical Action: If the bin is not full, the microcontroller sends a PWM signal to the SG90 Micro Servo Motor, which acts as a mechanical lever to push the swing-top lid open to 180 degrees. It waits for the user to remove their hand, initiates a 4.5-second timer, and then closes. A 1.5-second cooldown follows to let the lid mechanically settle.
    • Textual Feedback: A 16×2 LCD Display connected via an I2C module shows real-time information, such as the exact trash percentage and system status. The I2C module drastically reduces the number of pins required to interface the screen with the microcontroller (using only SDA and SCL pins).
    • Visual & Auditory Warnings: If the bin is full (>= 85%), the system locks the opening mechanism. The Red LED lights up, and an Active Buzzer emits an error sequence. The fullness level is continuously displayed using 3 LEDs (Green < 50%, Yellow < 85%, Red >= 85%).

 Block Diagram

3. Hardware Design

3.1. List of Components

To build this project, the following hardware components are used:

  • 1x ATmega328p XPlained Mini Microcontroller Board
  • 1x Small Swing-Top Plastic Trash Bin
  • 2x HC-SR04 Ultrasonic Sensors
  • 1x 16×2 Character 1602A LCD Display
  • 1x I2C Serial Interface Module for LCD
  • 1x SG90 Micro Servo Motor
  • 3x LEDs (Green, Yellow, Red)
  • 3x 220Ω Resistors
  • 1x TMB12A05 Active Buzzer
  • 1x Breadboard
  • Assorted Dupont Jumper Wires (Male-to-Male, Male-to-Female)

3.2. Current Status of Hardware Implementation

The project is currently in the advanced breadboard prototyping phase. All critical hardware subsystems (distance measurement, visual/auditory feedback, and PWM mechanical actuation) have been successfully wired, integrated, and validated against the microcontroller's logic. The finite state machine correctly drives the hardware inputs and outputs without blocking the CPU execution. The next planned step is the physical mechanical assembly onto the bin chassis and soldering the I2C backpack to the LCD module for the final interface integration.

3.3. Component Roles

  • ATmega328P Xplained Mini: The main processing unit executing the state machine, reading sensors, and driving actuators.
  • HC-SR04 Sensors: Used as digital inputs. The exterior one triggers the bin opening, while the interior one acts as a dynamic level gauge.
  • SG90 Servo: Used as a mechanical output to transform the PWM signal into a 180-degree physical rotation for the lid mechanism.
  • LEDs & Resistors: Provide immediate visual feedback. The 220Ω resistors are strictly required to limit the forward current (~15mA) and prevent overloading the microcontroller's GPIO pins or burning the LEDs.
  • Active Buzzer: Acts as a critical error indicator (auditory output) when a user tries to interact with a completely full bin.
  • LCD 1602 + I2C Module: Provides the detailed Human-Machine Interface (HMI). The I2C backpack serializes the data, translating it into parallel instructions for the HD44780 controller on the screen.

3.4. Pin Mapping and Justification

The hardware pinout was carefully designed to map specific components to their required internal AVR hardware modules, while explicitly avoiding the SPI pins (PB2-PB5) to prevent programming (ISP) conflicts during development.

Component ATmega328P Pin Signal Type Technical Justification
Green LED PD2 Digital OUT Standard GPIO for state indication.
Yellow LED PD3 Digital OUT Standard GPIO for state indication.
Red LED PD4 Digital OUT Standard GPIO for state indication.
Active Buzzer PD5 Digital OUT Standard GPIO. Active buzzers require only a high DC voltage to emit sound.
Trig 0 (Exterior) PC0 Digital OUT Moved to Port C to avoid SPI line interference during USB code flashing.
Echo 0 (Exterior) PC1 Digital IN General input for reading the 10µs ultrasonic echo pulse length.
Trig 1 (Interior) PC2 Digital OUT Mapped to Port C for grouping and clean breadboard wiring.
Echo 1 (Interior) PC3 Digital IN Input for reading the interior trash level echo.
Servo Motor PB1 (OC1A) PWM OUT Driven by the 16-bit Timer1 Output Compare register (OC1A) to ensure a highly stable and precise 50Hz PWM signal for servo angle control.
LCD I2C SDA PC4 I2C Data Hardwired internal TWI (Two-Wire Interface) data line on the AVR.
LCD I2C SCL PC5 I2C Clock Hardwired internal TWI (Two-Wire Interface) clock line on the AVR.

3.5. Electrical Schematic & Explanations

The electrical circuit utilizes the breadboard's side power rails as a central power distribution bus.

  • Power Bus: The 5V and GND pins from the ATmega328p board are connected directly to the red (+) and blue (-) rails of the breadboard. All sensors and actuators draw power from these rails in parallel to ensure uniform voltage and avoid stressing a single ground pin on the microcontroller.
  • Protection Circuits: Each LED anode is connected to a PD GPIO pin, while the cathode routes through a 220Ω current-limiting resistor to the common GND bus.
  • I2C Architecture: The LCD module is connected in parallel on the I2C bus (PC4/PC5), taking advantage of the microcontroller's internal pull-up resistors for the serial communication lines.

 Project Electrical Schematic

3.6. Hardware Assembly & Proof of Concept

The images below demonstrate the successful wiring and the functional proof of the hardware logic running on the microcontroller.

1. General Breadboard Prototyping Setup  Complete Breadboard Setup

Explanation: This image presents the complete hardware prototype. The components are wired using the power rails for optimal cable management. The HC-SR04 sensors, the servo, and the LED/Buzzer sub-circuit are successfully interfaced with the Xplained Mini board.

2. Proof of Functionality: The Interlocking Logic (Full Capacity)  System detecting a full bin

Explanation: This test validates the interlocking safety state. An obstacle is placed physically close (< 7cm) to the interior ultrasonic sensor (simulating a trash capacity of > 85%). The microcontroller correctly processes the echo timing, updates the logic state, and physically turns on the Red LED. Simultaneously, the active buzzer is triggered on pin PD5, and the servo motor is software-locked, proving that the hardware responds accurately to environmental inputs.

4. Software Design

4.1. Current Status of Software Implementation

Currently, the software component is fully functional and successfully integrated with the hardware. The project has been migrated to a modern development environment (PlatformIO), utilizing a hybrid C/C++ architecture. The core logic of the system (sensor reading, PWM signal generation, LED and buzzer control) is implemented bare-metal (in pure C language, through direct manipulation of AVR registers). To ensure the stability of the Human-Machine Interface (HMI), the upper level of the application uses the Arduino framework (main.cpp), allowing the integration of previously written C modules via the extern “C” directive and managing the LCD screen through a dedicated C++ library. The finite state machine runs correctly, with the system being able to process data in real time without blocking.

4.2. Motivation for Chosen Libraries

Although a large part of the code was written from scratch to maintain strict control over hardware resources, certain libraries were strategically chosen to streamline development and guarantee stability:

  • LiquidCrystal_I2C.h (and the Wire.h dependency): Chosen for controlling the 1602 LCD screen via the PCF8574 module. During the bare-metal prototyping phase, although the I2C hardware bus was successfully validated (confirming the 0x27 / 0x4E address), the initialization of the HD44780 controller proved to be extremely sensitive to wait times (delays) and 4-bit masking sequences. Using this established library abstracts the timing logic and prevents I2C Bus Hang phenomena, ensuring robust data display without jeopardizing the main program loop.
  • Arduino.h (PlatformIO): Included to provide easy access to the basic functions required by the screen library and to unify the compilation process in the C++ environment.

4.3. Project Novelty

Compared to a standard automatic trash bin (which only opens a lid upon motion detection), the novelty of this project lies in the dynamic internal volume calculation and monitoring system, supported by a “self-healing” algorithm (self-healing logic). The system doesn't just measure a distance; it continuously interpolates the fill percentage, automatically adjusting outlier values (e.g., clamping values that exceed the physical depth of the bin). Additionally, the implementation of an Interlocking (Safety Lock) state represents a practical innovation: if the system detects a capacity over 85%, it mechanically refuses to open the lid to prevent overfilling and physical jamming of the trash, simultaneously alerting the user visually and acoustically.

4.4. Justification for Using Lab Functionalities

The project integrates multiple concepts studied in the laboratory, utilized as follows:

  • Hardware Timers / Counters: * Timer1 (16-bit): Configured at the register level (TCCR1A, TCCR1B) in Fast/Phase Correct PWM mode to generate a precise 50Hz signal on pin PB1 (OC1A). The ICR1 register defines the period, and OCR1A is dynamically manipulated to control the pulse width (1ms - 2ms), determining the exact angle of the servo motor.
    • Timer0 / Tick System: Used for counting milliseconds (get_milis()), essential for the non-blocking operation of the system.
  • Interrupts: Globally activated via the sei() function, these allow timers to run in the background (e.g., generating overflows for time calculation and ultrasonic sensor pulses) without blocking the main program execution.
  • Serial Communication (USART): Configured by manipulating the UBRR0 and UCSR0 registers, the USART module was vital during development for debugging. All sensor data and state changes were printed in real-time to the terminal, allowing for precise distance calibration.
  • TWI Module (I2C): Used internally by C++ libraries to communicate with the LCD screen, exclusively utilizing pins PC4 (SDA) and PC5 (SCL), thus saving valuable GPIO pins.
  • Direct Port Manipulation (GPIO): Interaction with the LEDs, buzzer, and ultrasonic sensor Trigger pins is done by directly writing to the direction (DDRx) and data (PORTx) registers, guaranteeing instantaneous execution and a minimal memory footprint.

4.5. Project Structure, Interaction, and Validation

Code Architecture: The project is strictly modularized, with each hardware component having its own set of header and source files (e.g., usart.h, timer.h, ultrasonic.h, servo.h, leds.h, buzzer.h). This structure decouples the sensor logic from the main application. The main.cpp file acts as an orchestrator.

Feature Interaction: The core logic is based on a Finite State Machine (FSM) with three main states: CLOSED_STATE, OPEN_STATE, and COOLDOWN_STATE. The heart of the system is a completely non-blocking loop. Instead of using blocking functions like delay(), the system uses asynchronous time evaluations (current_time - last_read >= 500).

  • When the system is in the CLOSED_STATE, it reads both ultrasonic sensors. It calculates the trash level and updates the interface (LEDs and LCD).
  • If the exterior sensor detects a presence (⇐ 20cm) and the internal percentage is safe (< 85%), the actuator is triggered (the Servo receives the corresponding value via OCR1A), and the system transitions to the OPEN_STATE.
  • After the user moves away, a logical timer moves the system to the COOLDOWN_STATE to allow the lid to close smoothly.

Validation Methodology: Each module was independently tested and validated (Unit Testing at the hardware level).

  1. Ultrasonic Sensors: Validated by printing the read distance on the USART and comparing it with a physical ruler.
  2. Servo Motor: Validated by sending static angles (0, 90, 150 degrees) on an oscilloscope/logic analyzer to confirm the 50Hz frequency, followed by connecting the real mechanical load.
  3. LCD/I2C Integration: Validated through manually written I2C scanners to confirm the physical address, resolving timing conflicts before implementing the final library.
  4. Final System: Validated through “Black Box” testing – simulating the filling of the bin with a physical obstacle to demonstrate the successful triggering of the visual (Red LED), acoustic (Buzzer), and mechanical (servo lock) interlocking.
pm/prj2026/jan.vaduva/nicolas.costescu.txt · Last modified: 2026/05/24 20:52 by nicolas.costescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0