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:
- Yellow wire: VCC pin –> Arduino 5V
- White wire: GND pin –> Arduino GND
- Red wire: Trig pin –> Arduino digital pin 11
- 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.)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Detecting a Full Trash Bin with an Ultrasonic Distance Sensor * by Justine Kone */ #include <NewPing.h> // This library allows us to search (or "ping") for the nearest object. #define TRIGGER_PIN 11 // Digital Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 10 // Digital Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. Trigger pin sends out // the sound wave and the echo pin recieves it when it bounces back. int thresh [5] = {24,18,12,6,0}; // Create an array for thresholds. Each number in the array represents a distance. void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see distance and threshold results. } void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec) so that there's time to respond. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). int distance = uS / US_ROUNDTRIP_CM; // Convert microseconds into distance in centimeters. Serial.print(distance); // Print distance. Serial.print("cm"); // if statement determines how full the bin is. If distance is between thresh[0], or 24cm, and thresh[1], or 18cm, // the bin is only 25% full. if(distance<thresh[0]&&distance>=thresh[1]){ Serial.print(", 25% Full"); } else if(distance<thresh[1]&&distance>=thresh[2]){ Serial.print(", 50% Full"); } else if(distance<thresh[2]&&distance>=thresh[3]){ Serial.print(", 75% Full"); } else if(distance<thresh[3]&&distance>=thresh[4]){ Serial.print(", 100% Full"); } else{ Serial.print(", Empty"); // Empty will print if you take the sensor out of the bin or the sensor is not getting a reading } Serial.println(); delay(1000); } |
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.
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.