There are millions of public dustbin out there that people use and are emptied in a few days by the public authorities. Now the problem is not all dustbins are filled at the same rate and the dump vehicle waste time checking each and every dustbin. This leads to more fuel usage, labour and cost.


IOT of dustbin can solve this issue by tracking the amount of garbage inside the bins and thus can be easily geotagged and the dump vehicle can plan its path around the city in a more optimised way to save time and fuel cost. The dump vehicle can decide which dustbins to clear first and whom to clear next time. This can be a smart city project.

The project involved an Arduino Nano with ESP8266 to talk to the internet and an Ultrasonic sensor to measure the depth of the garbage in the dustbin.

The system updates the server every 3 seconds and the user can track the quantity of garbage in the bins.


  • Arduino Nano
  • ESP8266
  • HC-SR04 Ultrasonic Sensor
  • 9v Battery
  • 3.3v Regulator
  • Wires
  • Plastic Bin
  • Wood / similar material for mounting the sensor


From the gallery below you could easily see it is an easy construction with a mounting for the Ultrasonic Sensor and the Arduino and ESP8266. A small 9v battery could also be seen stuck to one side of the bin. Though this project looks so easy to implement and deploy but in real life when this is made into a product and need to be deployed we have to take care of 100s of quality standards and performance metrics. In real life, this should be the most power efficient so that battery power lasts longer. In some places, there will not be any wi-fi available and thus alternate connectivity should also be made available. Accuracy and reliability is also an important parameter to consider.

The ESP8266 connects with a preprogrammed Wi-Fi SSID and starts polling the Ultrasonic Sensor, measures the depth and uploads it to the cloud.

I haven’t implemented any boundary conditions and fault tolerances in the code. This code is just good for POC and not for a product.



Download Code From >>

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

//*-- IoT Information
#define SSID "CrazyEngineer"
#define PASS ""
#define IP "" // ThingSpeak IP Address:

// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
String GET = "GET /update?key=VYJGZSHIYR7SMTX9"; // You have to get your key from the website
int prev = 0;
void setup() {
Serial.begin( _baudrate );


if (Serial.find("OK"))
debug.println("RECEIVED: OK\nData ready to sent!");


void loop()
int dat = sonar.ping_cm();
if (dat < 0 || dat > 37)
dat = prev;
String depth = String((37 - dat)); // turn integer to string
debug.print( "DEPTH = " );
//----- update the Thingspeak string with 3 values
void updateTS( String T)
// ESP8266 Client
String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
cmd += IP;
cmd += "\",80";
if ( Serial.find( "Error" ) )
debug.print( "RECEIVED: Error\nExit1" );

cmd = GET + "&field1=" + T + "\r\n";
Serial.print( "AT+CIPSEND=" );
Serial.println( cmd.length() );
if (Serial.find( ">" ) )
sendDebug( "AT+CIPCLOSE" );//close TCP connection
if ( Serial.find("OK") )
debug.println( "RECEIVED: OK" );
debug.println( "RECEIVED: Error\nExit2" );

void sendDebug(String cmd)
debug.print("SEND: ");

boolean connectWiFi()
Serial.println("AT+CWMODE=1");//WiFi STA mode - if '3' it is both client and AP
//Connect to Router with AT+CWJAP="SSID","Password";
// Check if connected with AT+CWJAP?
String cmd = "AT+CWJAP=\""; // Join accespoint
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
if (Serial.find("OK"))
debug.println("RECEIVED: OK");
return true;
debug.println("RECEIVED: Error");
return false;

cmd = "AT+CIPMUX=0";// Set Single connection
sendDebug( cmd );
if ( Serial.find( "Error") )
debug.print( "RECEIVED: Error" );
return false;


Git  >> 

Follow me on Instagram >>

Follow me on Twitter >>

Subscribe to my YouTube Channel >>

One Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.