Categories
TUTORIALS

Detecting a Full Garbage Bin Using an Ultrasonic Distance Sensor

Introduction

The idea for this tutorial came about as a way to prototype a solution to a common urban problem: overflowing dumpsters. City streets departments usually pick up trash on a fixed schedule that does not take into account how much trash there is, and residents dump trash whenever their household bins are full. What if one system could warn both the streets dept. and local residents the the neighborhood dumpsters are filling up?

In this tutorial, we will build the very beginnings of this “alert system” by using an ultrasonic distance sensor to tell you how full your home garbage bin is.

Required Parts
Step 1: Circuitry

To build this circuit, I used an Arduino Uno with breadboard, but other models should work as well. The ultrasonic distance sensor fits directly into the breadboard, and has four key connections:

  1. Yellow wire: VCC pin –> Arduino 5V
  2. White wire: GND pin –> Arduino GND
  3. Red wire: Trig pin –> Arduino digital pin 11
  4. Green wire: Echo pin –> Arduino digital pin 10

The Fritzing diagram on the left shows exactly how to make the connections. The photo on the right shows the final circuit. For the purposes of this tutorial, you will need the USB cable to hook up to your computer’s serial monitor,  as well as the battery for additional power. (Some ultrasonic sensors do not need more than the ~5V from your computer, but many do.)

Note: The yellow (VCC) and white (GND) wires appear to be reversed in the diagram. This is because the sensor is facing backwards in order to show the labels. Follow the photo for the correct setup.

If you are still confused about the exact setup, or your board is configured in a slightly different way, feel free to refer to this schematic for additional guidance: 

Step 2: The Code

The code for our “warning system” has two basic functions: sense and response. We want to sense the amount of distance available in our garbage bin, and respond by displaying how full the bin is. In order to ensure the code and sensor are working properly, I have also included the distance in the display, so that you can see the amount of centimeters that are empty next to the corresponding “percent full” label.

Tip: If your labels do not make sense, consider changing the threshold based on the size of your bin.

There are three main parts of the code: creating the thresh variable,  converting sound to distance, and writing our if statement.

The thresh variable holds an array of distances. To break up the total height, or distance, of our trash bin into quantiles, we need an array with five values. The largest value represents the total height of the inside of the trash bin, and the smallest value should always be zero, indicating that the bin is completely full. The values in between are determined by the height of your bin.

The ultrasonic distance sensor works by sending out a “ping” of sound waves and sensing how long they take to return. These waves are measured in microseconds. Using the NewPing library, we can convert microseconds into distance in centimeters.

The if statement combines these distances with the thresh variable. If the detected distance is less than the zero value in the array (24, in our case), but greater than or equal to the first value (18), we print that the bin is “25% Full”. If the distance is less than 18, but greater than or equal to 12, it’s “50% Full”. And so on…If the sensor detects a greater distance than you have in the array, or it sends out a sound wave but never receives one back, the code will print “Empty” to alert you to the mistake. Feel free to tinker with this error message, and change the number of values in the array, in order to create a warning system that suits your needs.

Step 3. In Action

First, we need a garbage bin. To test this out, I’m going to use a laundry hamper since it’s about the same size and a lot less messy.

Our “garbage” bin.

Next, attach the circuit to the inside of the bin so that the sensor is facing the bottom. For the purposes of this tutorial, we are going to keep the Arduino plugged into the computer for serial port readings. If you want to take this idea into practice, a wifi shield should be an easy substitution that allows for more remote warnings.

Once everything is in place, you can start to take readings. Because of how often the sensor is sending out a ping, and how infrequently the levels in the bin actually change, your read-out will look something like this:

As an example of the error message built into the code, I left the Arduino hooked up to my computer as I was removing it from the bin, and sure enough, the distance was too far:   .

Step 4. Scale It Up!

Now that you know how to sense the available space left in a garbage bin, and respond with an alert of how full the bin is, take it to the streets (literally)! With some simple modifications, like the wifi shield mentioned earlier, you can switch from an in-home bin and computer to city dumpsters and cell phones. If residents and streets dept. workers know when the trash is almost full, everyone can make smarter, more proactive decisions, and smelly, overflowing dumpsters can be a thing of the past.

Leave a Reply

Your email address will not be published. Required fields are marked *