Projects / Dustbin fill level indicator

Dustbin fill level indicator

Beginner2 hGuided buildReviewed

Build it step by step

0 of 9 done
  1. NANO

    Measure your bin

    Measure from where the sensor will sit to the bottom of the empty bin and write that into EMPTY_CM.

  2. NANO

    Wire the sensor

    VCC to 5V, GND to GND, TRIG to D9 and ECHO to D10.

  3. NANO18.4 mA

    Wire the three lights

    Green to D2, yellow to D3, red to D4, each through 220 ohms to GND.

  4. NANO

    Upload the sketch

    The monitor says Bin level ready and then prints distances.

  5. NANO

    Hold a book 30 cm below the sensor

    The reading shows about 30 cm and the green light stays on.

  6. NANO18.4 mA

    Move it to 15 cm

    The yellow light comes on.

  7. NANO

    Move it to 10 cm

    The red light comes on, and it blinks when the bin is almost full.

  8. NANO

    Wave your hand across the beam

    The lights hold still instead of flickering.

  9. NANO

    Fix the sensor in the lid

    Point it straight down, a little away from the walls.

In short

How do you build a dustbin fill level indicator?

An ultrasonic sensor in the lid measures how far down the rubbish is, and a green, yellow or red light tells you how full the bin is. It ignores the lid being opened and closed, so the light only changes when the reading is steady. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, USB A to micro-USB cable (data), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 9 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
9

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HC-SR04 VCC and GNDNano 5V and GNDPower
HC-SR04 TRIGNano D9Starts a measurement
HC-SR04 ECHONano D10Length of the echo pulse
Green LEDNano D2 through 220 ohms to GNDBin has room, long leg towards the resistor
Yellow LEDNano D3 through 220 ohms to GNDGetting full
Red LEDNano D4 through 220 ohms to GNDNearly full, blinks when over the line

Code

The sensor sends out a short burst of ultrasound and the sketch times how long the echo takes to come back with pulseIn(). Sound travels about 0.0343 cm in every microsecond, and because the trip is there and back, the distance is the time times 0.0343 divided by two. In a bin, the sensor looks down from the lid, so a large distance means an empty bin and a small distance means a full one. The two numbers EMPTY_CM and FULL_CM tell the sketch what those two ends are, and everything in between is turned into a percentage with a simple line. You must measure EMPTY_CM yourself, from the sensor to the bottom of your own bin. Real bins are messy, so the sketch takes five readings in a row and only trusts them if they agree to within 4 cm. When someone opens the lid or drops in a bag the readings jump about, the check fails and the last lights stay as they were. This is a very useful idea in all sensing: reject readings that are not steady. The three lights then show green below 40 percent, yellow up to 75, red above that, and the red light blinks when the bin is over 90 percent full. Note that the sensor cannot read closer than about 2 cm, which is why FULL_CM leaves 8 cm to spare. Soft rubbish such as plastic bags can absorb sound, so aim the sensor at the middle of the bin.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t TRIG = 9, ECHO = 10;
const uint8_t GREEN = 2, YELLOW = 3, RED = 4;

const float EMPTY_CM = 40.0;     // distance from the sensor to the bottom of an empty bin: measure yours
const float FULL_CM = 8.0;       // distance when the rubbish reaches the lid: leave this much for the sensor
const float STEADY_CM = 4.0;     // readings may differ by this much before we call them steady

float readCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(3);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000);   // 30 ms is about 5 m, then give up
  if (us == 0) return -1;                          // no echo
  return us * 0.0343 / 2.0;                        // speed of sound, there and back
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("Bin level ready"));
}

void loop() {
  float r[5];
  float lo = 1000, hi = 0, sum = 0;
  int n = 0;
  for (int i = 0; i < 5; i++) {
    float d = readCm();
    if (d > 0) {
      r[n++] = d;
      if (d < lo) lo = d;
      if (d > hi) hi = d;
      sum += d;
    }
    delay(60);
  }
  if (n < 4 || hi - lo > STEADY_CM) {              // lid moving or no echo: keep the last light
    delay(500);
    return;
  }
  float cm = sum / n;
  float level = (EMPTY_CM - cm) / (EMPTY_CM - FULL_CM) * 100.0;
  level = constrain(level, 0, 100);

  digitalWrite(GREEN, level < 40);
  digitalWrite(YELLOW, level >= 40 && level < 75);
  if (level >= 90) digitalWrite(RED, (millis() / 400) % 2);   // blink when nearly overflowing
  else digitalWrite(RED, level >= 75);

  Serial.print(cm, 1);
  Serial.print(F(" cm, bin "));
  Serial.print(level, 0);
  Serial.println(F("% full"));
  delay(1000);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Bin level ready
80.0 cm, bin 0% full
79.9 cm, bin 0% full
80.0 cm, bin 0% full

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Book at 30 cm, EMPTY 40 and FULL 8About 31 percent full, green
Book at 20 cmAbout 62 percent full, yellow
Book at 12 cmAbout 88 percent full, red steady
Book at 9 cmOver 90 percent, red blinks
Wave your hand in frontNothing changes until the reading steadies
Cover the sensor completelyLights hold, no wrong readings

If it does not work

Always reads 0 or nothing

Check TRIG goes to D9 and ECHO to D10, and that the sensor has 5 V.

Percentages are wrong

Measure the empty-bin distance again and set EMPTY_CM to it.

Lights never change

The steady check keeps failing, so raise STEADY_CM to 6 for a bumpy surface.

Red light does not blink

The level has to be above 90 percent, so lower FULL_CM a little.

Readings jump to the wall

The beam is wide, so point the sensor to the middle and tape a short tube around it.

Upload fails

Choose the Old Bootloader option for the Nano.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the dustbin fill level indicator?

Arduino Nano, HC-SR04 Ultrasonic Sensor, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, USB A to micro-USB cable (data), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the dustbin fill level indicator take to build?

About 120 minutes for a beginner maker, in 9 steps.

How do I know the dustbin fill level indicator works?

Book at 30 cm, EMPTY 40 and FULL 8: About 31 percent full, green. Book at 20 cm: About 62 percent full, yellow. Book at 12 cm: About 88 percent full, red steady.

Always reads 0 or nothing: what should I check?

Check TRIG goes to D9 and ECHO to D10, and that the sensor has 5 V.

Percentages are wrong: what should I check?

Measure the empty-bin distance again and set EMPTY_CM to it.

Lights never change: what should I check?

The steady check keeps failing, so raise STEADY_CM to 6 for a bumpy surface.

Red light does not blink: what should I check?

The level has to be above 90 percent, so lower FULL_CM a little.