Cultivate Curiosity, Inspire Imagination.

Raspberry Pi Tutorial: Smart Dustbin


The Raspberry Pi Smart Dustbin is constructed by connecting an ultrasonic sensor and 3 LED lights to the Raspberry Pi to sense and indicate how full a dustbin is. In a real-life situation, a smart dustbin will allow users as well as cleaners to detect, at a glace, which dustbins in the vicinity are still usable, and which ones need to be emptied.

In this tutorial, we explain how to build a smart dustbin with Raspberry Pi, an ultrasonic sensor, LED lights, and a Python program.




Hardware Required 

  1. Raspberry Pi 4 4GB / 8GB
  2. Raspberry Pi 4 Power Supply 15.3W USB-C
  3. Micro SD Card 32GB Class 10
  4. Ultrasonic Sensor HC-SR04+ 3.3V Compatible
  5. LED Kit 5mm 3mm 100pcs
  6. Resistor Kit 1/4W 1-3M ohm 2600pcs
  7. Solderless Breadboard 830 Tie Point
  8. Raspberry Pi 4.3 inch DSI Interface Capacitive Touch Display with Case 800x480 (optional)
  9. Wireless Keyboard with Touchpad (optional)


Connecting the Hardware

Connect the Ultrasonic Sensor to the corresponding GPIO pins on the Raspberry Pi. There are 4 pins on the Ultrasonic Sensor, namely, the VCC (Power Supply), Trig (Sends out a "ping"), Echo (Receives an echo pulse) and ground. Connect these pins to the corresponding GPIO pins for VCC, Trig and Echo on the Raspberry Pi.

The Raspberry Pi will be instructing the Ultrasonic Sensor to send out a ping via Trig on GPIO 20 and receiving the echo on GPIO 21.

Connect the red, blue and green LEDs to the Raspberry Pi as follows:

  1. GPIO 17 to a 220Ω resistor to a Red LED
  2. GPIO 27 to a 220Ω resistor to a Blue LED
  3. GPIO 22 to a 220Ω resistor to a Green LED

Do note the orientation of the LEDs. The longer leg is positive and the shorter leg is negative.

Resistors must always be used when you are connecting LEDs to the Raspberry Pi. This is to limit the amount of current will be drawn by the LEDs, otherwise the LEDs will blow out very quickly due to high current.

Ground the LED lights.

Connect the Ultrasonic Sensor's ground pin to the Breadboard

The wiring diagram below shows the connections between the Raspberry Pi, the Ultrasonic Sensor, and the LED lights.

Adhere the ultrasonic sensor below the lid of a dustbin. The ultrasonic sensor will send a "ping" and receiving an "echo". The time it takes to receive the echo signal will determine how full the dustbin in. When it detects a relatively empty dustbin, the Raspberry Pi lights up the Green LED. When it detects a dustbin that contains a fair amount of trash, the Raspberry Pi lights up the Blue LED. When the dustbin is full, the Raspberry Pi lights up the Red LED.


Coding the Smart Dustbin Program

Let's start coding our smart dustbin project. We will be coding this program in Python. Open "Thonny Python IDE".

# include libraries
import RPi.GPIO as GPIO
import time

# set gpio mode and warnings

Import the necessary libraries and set the GPIO mode. We will be using the time library for delay when triggering the ultrasonic sensor.

# declare pins
trigPin = 20
echoPin = 21
redLedPin = 17
blueLedPin = 27
greenLedPin = 22

# initialize pins

Declare the pins we will be using and initialise them.

def get_range():
# create a 10 microsecond pulse to trigger the ultrasonic module

# set ultrasonic trigger pin to high
GPIO.output(trigPin, True)

# wait for 10 microsecond

# set ultrasonic trigger pin to low
GPIO.output(trigPin, False)

# after pulsing, we need to listen for a signal

# record start time of no signal
while GPIO.input(echoPin) == 0:
pulse_start = time.time()

# record end time of a received signal
while GPIO.input(echoPin) == 1:
pulse_end = time.time()

# find the time difference between the signals
pulse_duration = pulse_end - pulse_start

# multiply with the speed of sound (34300 cm/s)
# and divide by 2 to get distance, because there and back
distance = (pulse_duration * 34300) / 2

# return the calculated distance
return distance

Create a get_range method so we can call it in our program later to calculate the distance from the ultrasonic sensor.

def show_LED(colour):
# show only red LED
if colour == "red":
GPIO.output(redLedPin, True)
GPIO.output(blueLedPin, False)
GPIO.output(greenLedPin, False)

# show only blue LED
elif colour == "blue":
GPIO.output(redLedPin, False)
GPIO.output(blueLedPin, True)
GPIO.output(greenLedPin, False)

# show only green LED
elif colour == "green":
GPIO.output(redLedPin, False)
GPIO.output(blueLedPin, False)
GPIO.output(greenLedPin, True)

Create a show_LED method with a colour input so we can call it in our program later to set the respective LEDs on/off.

# main loop
while True:
# get distance from the ultrasonic sensor
distance = get_range()

# print distance out and format to 2 decimal places
print("Distance: %.2fcm" % distance)

if distance < 15:
elif distance < 25:

# add some delay so the previous signal does not interfere with new signal


In our main loop, we call the get_range method to calculate distance from the ultrasonic sensor. We then use the distance in our compare logic to show different LEDs based on different distances. This is so that we can indicate how full the dustbin is.

Depending on the size of your dustbin, you will need to calibrate the program by changing the distance that the ultrasonic sensor detects to show the corresponding LED lights (i.e. if your dustbin is big, you will need to increase the distance from 25 to, say, 20 for turning the blue LED light on.

Run the program, notice that the Raspberry Pi is reading and displaying the distance sensed by the ultrasonic sensor, and printing it to the console.

Based on the distance sensed, the Raspberry Pi sends a corresponding instruction to turn on and off the corresponding red, blue and green LED lights.