Introduction: Garbage System

We decided to try to make a way to track either a neighborhood’s community garbage fill or a sensor in every garbage in the neighborhood to try to make garbage disposals more efficient. We thought that if a truck comes every two weeks for a collection, what if I or my neighbor only ended up throwing out a little bit. Wouldn’t it be inefficient to send a truck where half of the neighborhood didn’t’ send full trash cans? It would be great if it was possible to see our neighbor’s garbage can fill and then use their garbage if mine was full and theirs was empty and vice versa. We decided to use the ultrasonic sensor, HC-SR04 along with a raspberry pi to try to tackle this project.

Supplies

Ultrasonic sensor (HC-SR04)

Raspberry Pi (we used Pi 4 Model B)

Breadboard

Jumper cables

Couple of resistors (3 x 1k ohm)

Step 1: Connecting HC-SR04

Since we are using the Raspberry Pi, we need to use a voltage divider to regulate the voltage going into the Pi's GPIO pins as they only allow 3.3v. The HC-SR04 uses 5V but needs to be brought down to 3.3V when connecting it to the Pi. Connect the 5V and ground pins respectively and according to your program attach the echo and trigger pins to the respectful pins. In our program we used pin 23 and 24 for echo and trigger respectively.

Step 2: Mosquitto and Paho MQTT

Before we start programming on Python to get the ultrasonic sensor to work with the Pi, we should install these applications to get the ultrasonic sensor to communicate with our programming software Node-RED. Mosquitto is a MQTT broker that you can use on the Pi while Paho MQTT is the library that allows you to code in Python to get the sensor to communicate with the MQTT broker. To install both of these you would type these commands on your Pi terminal

sudo apt update

sudo apt install -y mosquitto mosquitto-clients

sudo apt-get install python3-pip

sudo pip3 install paho-mqtt

Step 3: Python Program for Ultrasonic Sensor

This is the program that I used to read the incoming data from the sensor and also publish to the MQTT broker.

Step 4: Node-RED

Some nodes do not come preinstalled in the program so you may need to install it from the palettes. The ones that you need to install are node-red-dashboard, and node-red-node-sqlite.

This is where we start using our programming software and the sensor. The first node you would need is the MQTT-in Node and that allows us to use our sensor running the program from above to send data to this software. The range node we used flips the values we have (ie. 5cm is full from program so we flip it to 100%). Following the range node we have 2 function nodes, one to display the message to our dashboard and one to show a visual that the garbage is full. The program for the function nodes are attached.

If possible, this flow would be able to be used for multiple ultrasonic sensors. For our project however we had to do simulation data as we weren't able to get our hands on more sensors. The way we did this is very similar but we have buttons that the user can click to randomly add 1-10 percent of the garbage in each of the garbages. We used 2 buttons, one to add garbage, and one to clear. The gauge, messages and the indicator are still exactly the same the code to count and keep count of the garbage is a little different though.

Step 5: Logging Data

We decided that it would be a good idea to log how full the garbages are when the truck comes to empty the garbages. With the help of the sqlite node we are able to read and write data which also saves it to the Pi. You would need to install this node as I've said before.

The steps to creating and logging data goes as follows:

1. Create the database

2. Log the data

3. Pull the data to show on our dashboard

4. Clear and delete the data

The way SQL works is that you need to create execute the topic which are CREATE TABLE, INSERT INTO, SELECT FROM, and DELETE FROM. Using timestamp nodes we can execute topics to the sqlite node which does each of those functions (create, insert, select, and delete). We only need to create the database once and once its done we can log data to it. Once the database is created, we can log data and we used user input again to log when the truck has come. We made it so you aren't able to log data until the truck is allowed to come which is 5 garbages at 80% capacity (considered full). We also used the range node again to scale the 500 back to a 0-100%. We then have the option to delete all data from the database if we would like. The UI table node is a node to allow us to see the table in a nicely formatted way on our dashboard.

Step 6: Layout

After all this has finished you are able to create a layout you would like with the help of Node-RED. On the side tab you will be able to space them however you like and there are plenty more customization options you have. Also attached is my flow for my entire program.

Step 7: Conclusion

Upon completing this project, there are areas where we can see the program be grown further. I never found a way to make the logging automatically as the only way we could do it was for it to log at an interval and we wouldn't need any repeat numbers if the garbage truck came once. I think this is partly due to how we decided to make it heavily reliant on the function nodes and the programming as we are more comfortable with that programming. After exploring after we were done, it was clear that there are nodes made for everything and it could've made life a whole lot easier if we found the function of switch and rbe nodes earlier. There was also a node made for ultrasonic sensors which we didn't get to work. It would've made things easier as there would be no need for MQTT or the Python program as it is just a node with the trigger and echo pins. We decided to work around it by making the Python program as you saw above. A huge tip for anyone who wants to dive into Node-RED is that you should use a lot of debug nodes to figure out if each flow is working and outputting what you want/need.