---
# System prepended metadata

title: Monitoring system for recycling bins in recycling stations.

---

# Monitoring system for recycling bins in recycling stations.
By Abdlkarim Alsaleh (aa225mg)

![Recycling bin monitoring sysyem](https://i.imgur.com/XuHyLti.png)
###### *The monitoring system installed on a model recycling container*.


The monitoring system is used to remotely monitor the recycling bins located in recycling stations especially the bins used to collect the clothes, textile, and shoes similar to this shown in the next picture.

![](https://i.imgur.com/PtWdZEX.jpg)
###### *A container used for collecting clothes and shoes managed by  [Human Bridge](http://www.humanbridge.se/aktuellt/category/textilinsamling/).*

The estimated time to build this system and test it at home is about 10 hours.

## Objective
The goal of this project is to provide a mechanism for the organizations that are collecting clothes, textile, and shoes to monitor their containers at the recycling sites remotely. They can use the monitoring system to empty the containers on demand rather than on a set schedule, and this can help them in reducing the recycling cost and also to increase the efficiency of their recycling system. 

## Material
All the material used to build and test the monitoring system are listed down in the next table.


| No. | Description | Quantity |Source|
| -------- | -------- | -------- |--------|
| 1     | LoPy4 development board     | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 2     | Extention board V3.1     | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 3     | LoRa/Sigfox external antenna     | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 4     | Breadboard (400 Pin)     | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 5     | Tilt Switch Sensor SW-200D   | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 6     |  Ultrasonic distance sensor HC-SR04  |1|[Amazon.se](https://www.amazon.se/gp/product/B07KPJNLSS/ref=ppx_yo_dt_b_asin_title_o01_s01?ie=UTF8&psc=1) |
| 7    | Resistor 1k ohm   | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 8     | Resistor 10k ohm     | 1    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 9     | Jumber wire     | 14    |[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |
| 10    | Battery holder 3xAAA with switch and JST-connector    | 1    |[Electrokit](https://www.electrokit.com/en/product/battery-holder-3xaaa-with-switch-and-jst-connector/) |
| 11     | Recycling bin    | 1    |[IKEA](https://www.ikea.com/se/sv/p/filur-tunna-med-lock-vit-20193899/) |

* #### **LoPy4 module + expansion board 3.1 + LoRa/Sigfox antenna:** 
This bundle can be bought from [Pycom](https://pycom.io/product/lopy4-multipack/) for about 600 crowns. LoPy4 module is an IoT platform used to connect and manage the system different sensors. The expansion board allows for easy connections between a breadboard and LoPy4 development board. The antenna is required to connect the development board to the Sigfox network.
![](https://i.imgur.com/h6XU88p.png)
###### *The development board bundle sold by [Pycom](http://www.humanbridge.se/aktuellt/category/textilinsamling/).*


*  #### **Breadboard + resistors + jumper wires:**
The breadboard and the jumper wires are mainly used to build and test the system quickly before finalizing the system design. The breadboard and the wires can be bought for about 50 crowns. 

The resistor used in this project to build a voltage divider which is in its turn used to turns the battery voltage into smaller one that can be fed directly to the ADC in order to measure the battery voltage. Resistors are very chip components and one can buy a resistor kit (300 resistors of different values) from [amazon](https://www.amazon.se/motst%C3%A5nd-motst%C3%A5ndskit-tolerans-metallfilm-sortiment/dp/B07VDWSQBH/ref=sr_1_19?crid=3DR1301UGIF1P&dchild=1&keywords=resistor+kit&qid=1626604599&sprefix=resistor+%2Caps%2C189&sr=8-19) for 50 crowns.  

![](https://i.imgur.com/aSaDFRn.jpg)


* #### **Tilt Switch Sensor SW-200D:** 

The tilt switch is useful for detecting orientation or inclination and it is used in this project to detect if the cover of the recycling bin is open or not. This sensor can be bought from [Electrokit](https://www.electrokit.com/produkt/tilt-switch/) for only 12 crowns.
![](https://i.imgur.com/7Aa7ZC0.jpg)


* #### **Ultrasonic distance sensor HC-SR04:** 

The ultrasonic sensor is used to measure the distance to an object using ultrasonic sound waves. The sensor is used in this project to find the available space in the recycling bin. The  HC-SR04 ultrasonic distance sensor can be bought from [Electrokit](https://www.electrokit.com/en/product/distance-sensor-ultrasound-hc-sr04-2-400cm/) for 59 crowns.
![](https://i.imgur.com/LGQ5qMZ.jpg)

* #### **Battery holder and batteries:** 
The monitoring system uses batteries as a main power source. In this project, we choose to power the system using 3XAAA battereis which give us about 4.5 volt when they are new or full charged . The battery holder can be bought from [Electrokit](https://www.electrokit.com/en/product/battery-holder-3xaaa-with-switch-and-jst-connector/) for 29 crowns. 
![](https://i.imgur.com/QvcNJke.jpg)


* #### **Recycling bin:** 
To test the implemented monitoring system, a 10-liter indoor recycling bin is used as a model for the recycling site container. The bin can be bought from [IKEA](https://www.ikea.com/se/sv/p/filur-tunna-med-lock-vit-20193899/) for less than 100 crowns. 
![](https://i.imgur.com/XM4lDN5.jpg)


## Computer setup
MicroPython programming language is used during this project to program our development board (Lopy4). Atom IDE is used to write, test, and debug the code required to operate the system. Atom is a free text editor, and it can be download using the following link: [Atom](https://atom.io/). A complete walk-through guide on how to Install and setup Atom can be found [here](https://flight-manual.atom.io/getting-started/sections/installing-atom/#platform-windows). After installing Atom, we need to install the Pymakr plugin for Atom using the following guide: [Setup Pymakr for Atom](https://docs.pycom.io/gettingstarted/software/atom/).

To be able to program the Lopy4 module, we need to connect our development board to Atom. This can be done by attaching Lopy4 module to the expansion board and connect the expansion board to the computer hosting the Atom software through USB cable. It is also recommended to upgrade the Lopy4 firmware using Pycom Firmware Update tool. A guide about how to install the updating tool and how to update the firmware can be found [here](https://docs.pycom.io/updatefirmware/device/).

## Connecting components
The connection diagram of this project is shown in the following image.

![](https://i.imgur.com/rre42zL.png)

The following two images show how the physical components of the monitoring are mounted on the recycling bin cover.

![](https://i.imgur.com/Aes4FwQ.jpg)

The physical connections of the monitoring system circuit is shown in follwing images.

![](https://i.imgur.com/gJs1LNK.jpg)


## Platform
For this project, we choose to use the Pybytes platform to manage and control our Pycom device. Pybytes is a free and easy to use platform that enable the registered user to:
* Check the status of the device.
* Execute commands directly from Pybytes terminal shell.
* Visualise device data using a customisable dashboard.
* Track the device location using the built-in Google Map API.
* Upgrade or downgrade firmware versions with the exclusive firmware update

[Getting Started with Pybytes](https://docs.pycom.io/pybytes/gettingstarted/) guides you to create Pybytes account and to setup your device on the Pybytes platform.


## The code
The code required to operate the monitering system is built based on the following flowchart. 

![](https://i.imgur.com/IMB8cTz.png)

All the code required to control the monitoring system is written into the "main.py" Python file listed bellow.



``` Python=
import utime
import pycom
from machine import Pin
from machine import ADC

#This class is used to wrap and manipulate the monitoring system data.
class Monitor:
    """class constants"""
    REF_DIST = 5 #The reference empty distance (cm) assigned to recycling bin
    LOOP_DELAY = 2 # delay in seconds add to every loop iteration
    DATA_DELAY = 30*60 # data will be sent every 30 minuits to PyBytes platform
    GREEN = 0x001100 # green value for the builtin rgbled
    BLUE = 0x000011  # blue value for the builtin rgbled
    RED = 0x110000   # red value for the builtin rgbled
    OFF = 0x000000   # value for turning the builtin rgbled off

    """Class variables"""
    # Define and initialize the LoPy pin used as an input to the tilt sensor
    tilt_Sensor= Pin('P20', mode=Pin.IN, pull=Pin.PULL_UP)
    # Initialize the ADC and define the pin used to measure the battery voltage
    apin = ADC().channel(pin='P16')
    # define Ultrasonic Sensor pins
    echo = Pin('P11', mode=Pin.IN)
    trigger = Pin('P10', mode=Pin.OUT)

    """Class methods"""
    #Return the battery voltage in millivolt
    @classmethod
    def FindBatteryVoltage(cls):
        return int(cls.apin.voltage()*10.5)

    #Check if the bin cover i open
    @classmethod
    def IsBinCoverOpen(cls):
        return not(cls.tilt_Sensor())

    # Send the system data to PyBytes platform
    @classmethod
    def SendData(cls):
        print("Sending data...\n")
        empty_space = Monitor.MeasureEmptySpace() # measure the available space
        battery_voltage = Monitor.FindBatteryVoltage() # read battery voltage
        pybytes.send_signal(1, empty_space)
        pybytes.send_signal(2, battery_voltage)

    # Measure the container avalible space using the ultrasonic sensor
    @classmethod
    def MeasureEmptySpace(cls):
        # trigger pulse LOW for 2us (just in case)
        cls.trigger(0)
        utime.sleep_us(2)
        # trigger HIGH for a 10us pulse
        cls.trigger(1)
        utime.sleep_us(10)
        cls.trigger(0)

        # wait for the rising edge of the echo then start timer
        while cls.echo() == 0:
            pass
        start = utime.ticks_us()

        # wait for end of echo pulse then stop timer
        while cls.echo() == 1:
            pass
        finish = utime.ticks_us()

        # pause for 20ms to prevent overlapping echos
        utime.sleep_ms(20)

        # calculate distance by using time difference between start and stop
        # speed of sound 340m/s or .034cm/us. Time * .034cm/us = Distance sound travelled there and back
        # divide by two for distance to object detected.
        distance = -((utime.ticks_diff(start, finish)) * .034)/2

        return int(distance)

    #Add delay in seconds
    @staticmethod
    def AddDelay(delay):
        utime.sleep_ms(1000*delay)

    #Turn on the built_in rgbled with the color definde in color parameter
    @classmethod
    def TurnOnLed(cls,led):
        pycom.rgbled(led)
        utime.sleep_ms(100)
        pycom.rgbled(cls.OFF)

timer = 0 # reset the timer used to send the data to PyBytes platform.

# this code will run forever
while True:
    # check if the bin cover is open
    if Monitor.IsBinCoverOpen(): # the cover is open
        while Monitor.IsBinCoverOpen():
            print("The cover is open\n")
            Monitor.TurnOnLed(Monitor.BLUE)
            Monitor.AddDelay(Monitor.LOOP_DELAY)
    else: # the cover is closed
        print("The avalable distance = " + str(Monitor.MeasureEmptySpace()) +" cm")
        print("Battery voltage = " + str(Monitor.FindBatteryVoltage()) + " millivolt\n")
        # if the avalable distance is less than or equal to the reference
        # distanc then turn on the red led otherwise turn the green led on.
        if Monitor.MeasureEmptySpace() <= Monitor.REF_DIST:
            Monitor.TurnOnLed(Monitor.RED)
        else:
            Monitor.TurnOnLed(Monitor.GREEN)

        # if the timer is reset then sen the data to the PyBytes platform.
        if timer == 0:
            Monitor.SendData()

    # update the timer
    timer = timer + 1
    # reset the timer every 30 minuts

    if timer >= Monitor.DATA_DELAY/Monitor.LOOP_DELAY:
        timer = 0

    # add loop delay
    Monitor.AddDelay(Monitor.LOOP_DELAY)

```
The main functions in the code are **MeasureEmptySpace()** and **SendData()**.
*  **MeasureEmptySpace()** function is used to measure the empty distance in the container using the HC-SR04 Ultrasonic sensor. The code and description of the function code can be found [here](https://core-electronics.com.au/tutorials/hc-sr04-ultrasonic-sensor-with-pycom-tutorial.html).  
*  **SendData()** function is used to send the container data to the PyBytes platform. The container data is the container empty space in cm and the battery voltage in millivolt.  



## Transmitting the data / connectivity
Device data in this project are sent to the Pybytes platform using Sigfox network. There are two key reason behind choosing Sigfox technology. The first reason is that coverage of this network is good in the area where I live, and the other reason is that the Lopy4 device used in this project includes one year of Sigfox connectivity. 

Sigfox has two limitations. We cannot send mor than 12 bytes in one message and we cannot send more than 140 message per day. This limitations have no effect in our project because our device sends two messages every 30 minutes which means that there are only 48 messages sent per day.

The device data is sent through two messages of integers: ***empty_space*** and ***battery_voltage***. empty_space indicates the available space in the recycling bin measured in cm while battery_voltage  is the voltage of the batteries that power the device measured in millivolt.

The next image shows some of the device data sent to the Pybytes platform through the Sigfox network.
![](https://i.imgur.com/0RCGWih.jpg)


See the [Sigfox tutorial](https://hackmd.io/@lnu-iot/SyUxJU7pu) to get help with setting up and connecting to Sigfox.

## Presenting the data
In this tutorial, we use Pybytes platform to present and visualize  the device data. Go to [Pybytes](https://docs.pycom.io/pybytes/dashboard/) for instruction on how to  create widgets for data visualization and how to set up your device’s dashboard on Pybytes.

The device data are sent every 30 minutes through two signals. The first signal is the available distance in the recycling bin and the other signal is the batteries voltage. The following images shows some of the data sent to Pybytes.


![](https://i.imgur.com/WAvSWjC.jpg)


![](https://i.imgur.com/AjQXXGD.jpg)

For demonstration purposes

## Finalizing the design

![](https://i.imgur.com/MImnlzx.jpg)

 

---

{%youtube VJlZDpSa6Ko %}

The code implemented in this project is well-suited for testing the system on a small model of a real container. With the real container, some functions need to be modified or simply omitted. 

In the current implementation, the ultrasonic sensor is mounted on the bin cover, and it is therefore important to check the cover status to prevent the code from sending faulty measurements while the cover is open. With the real container, the sensor is not mounted on the cover and there is, therefore, no need to check the cover status (if the container has a cover at all).

To save the battery power, it is very important to deactivate all the system LEDs if there is no one using or trying to use the recycling container.  This can be achieved by adding a motion sensor to the system to detect nearby people and activate/deactivate the LEDs accordingly.

For big containers, it is possible to add one or two ultrasonic sensors to make the measurement more efficient and reliable.





