The if statement combines these distances with the thresh variable. Using the NewPing library, we can convert microseconds into distance in centimeters. These waves are measured in microseconds. The ultrasonic distance sensor works by sending out a “ping” of sound waves and sensing how long they take to return. The values in between are determined by the height of your bin. 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. To break up the total height, or distance, of our trash bin into quantiles, we need an array with five values. The thresh variable holds an array of distances. There are three main parts of the code: creating the thresh variable, converting sound to distance, and writing our if statement. Tip: If your labels do not make sense, consider changing the threshold based on the size of your bin.
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.
We want to sense the amount of distance available in our garbage bin, and respond by displaying how full the bin is. The code for our “warning system” has two basic functions: sense and response. 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 This is because the sensor is facing backwards in order to show the labels. (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.
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. The photo on the right shows the final circuit.
The Fritzing diagram on the left shows exactly how to make the connections. Green wire: Echo pin –> Arduino digital pin 10.Red wire: Trig pin –> Arduino digital pin 11.The ultrasonic distance sensor fits directly into the breadboard, and has four key connections: To build this circuit, I used an Arduino Uno with breadboard, but other models should work as well. 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. and local residents the the neighborhood dumpsters are filling up? What if one system could warn both the streets dept.
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. The idea for this tutorial came about as a way to prototype a solution to a common urban problem: overflowing dumpsters.