Raspberry Pi Tutorial: Smart Dustbin

Introduction

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

In this example we are using Raspberry Pi Starter Kit, which includes various sensors and modules to get started right out of the box.

You may also choose to purchase the individual components here:

  1. Raspberry Pi 4 4GB or Raspberry Pi 8GB
  2. Raspberry Pi 4 Power Supply 15.3W USB-C
  3. Micro SD Card 16GB or Micro SD Card 32GB
  4. Ultrasonic Sensor HC-SR04+ 3.3V Compatible
  5. LED Kit 5mm/3mm 125 pcs
  6. Resistor Kit 1/4W 1280 pcs
  7. Solderless Breadboard 830 Tie Point
  8. Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case 1080×1920 (optional)
  9. Wireless Keyboard with Touchpad for Raspberry Pi and LattePanda (optional)

 

Raspberry Pi 4 4GB/8GB

A Raspberry Pi is a compact computer board offering endless opportunities. From the beginning, the Raspberry Pi was designed to be simple to use and simple to adapt to what you want to do with it.

Simply plug into a TV or monitor, keyboard, mouse, and power supply, and you are ready to go. The great thing about Raspberry Pi is that it is suitable for almost all age groups. Whether it’s introducing programming to children, or used by engineers to make complex computer-controlled systems, anyone can use one.

Raspberry Pi 4 Power Supply 15.3W USB-C

The Raspberry Pi 4 USB-C Power Supply provides the necessary 5.1V/3.0A DC power to power the Raspberry Pi 4.

Micro SD Card 16GB/32GB

The Micro SD Card stores the Raspberry Pi's operating systems as well as files that you will be creating and reading from in your projects. Read more about how you can install the Raspberry Pi OS here. Or better still purchase an SD Card with the Raspberry Pi OS installed at our online store!

Ultrasonic Sensor HC-SR04+ 3.3V Compatible

The Ultrasonic Sensor HC-SR04+ uses sonar to determine the distance to objects like bats or dolphins do. This module offers excellent range accuracy and stable readings in an easy-to-use package. Its operation is not affected by sunlight or black material (although acoustically soft materials like cloth can be difficult to detect). Ranging accuracy can reach up to 3mm.

LED Kit 5mm/3mm 125 pcs

An assortment of red, yellow, green, white and blue LEDs packed into a kit. Save the hassle of trying to purchase individual LEDs. Having extra LEDs around is always handy, almost all project requires some sort of LED whether its for indication light, alarm light, feedback light and many more.

Resistor Kit 1/4W 1280 pcs

An assortment of 1/4W resistors packed into a kit. Save the hassle of trying to purchase individual resistors. Having extra resistors around is always handy for any prototyping, especially if you're trying to figure out what resistor values to use for voltage dividers.

Solderless Breadboard 830 Tie Point

Solderless breadboard is perfect for prototyping. No hassle of soldering wires. All connections can be connected via jumper wires. There is a strip of double-sided tape at the bottom of the breadboard so you can fix the breadboard in place.

Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case 1080×1920

The Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case is an all-in-one LED display with a capacitive touchscreen as well as an enclosure to keep your Raspberry Pi all together in an attractive-looking package.

Wireless Keyboard with Touchpad for Raspberry Pi and LattePanda

This is a wonderful combo, 2.4GHz Mini Wireless QWERTY keyboard mouse, TouchPad combo, with USB interface adapter for Raspberry Pi, Cubieboard, Mini PC, and others. The Ergonomically handheld design is easy to carry and operate. Build-in removable rechargeable Li-ion battery that has a longer standby time. Perfect for PC, Pad, Andriod TV Box, Google TV Box, Xbox360, PS3, HTPC/IPTV, and Raspberry Pi, etc.

 

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
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

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
GPIO.setup(trigPin,GPIO.OUT)
GPIO.setup(echoPin,GPIO.IN)
GPIO.setup(redLedPin,GPIO.OUT)
GPIO.setup(blueLedPin,GPIO.OUT)
GPIO.setup(greenLedPin,GPIO.OUT)

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
   time.sleep(0.00001)

   # 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:
       show_LED("red")
   elif distance < 25:
       show_LED("blue")
   else:
       show_LED("green")

   # add some delay so the previous signal does not interfere with new signal
   time.sleep(0.1)

GPIO.cleanup()

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. 

Resources