How to build a Smart Dustbin with Raspberry Pi

ajaykumar1018

Newcomer
Staff member
Moderator
Joined
Apr 23, 2020
Posts/Comments
5
Received Likes
5
Location
India
Website
ajaykumar1018.github.io
ETX
600
In this guide, we will be discussing about building a smart dustbin using a Raspberry Pi. Traditional dustbin requires human efforts to for opening and closing the lid, also the location of the dustbins is quite unhygienic sometimes. This guide will help you with building an autonomous dustbin which can move around and reach out to you in your study room and opens the lid on its own for you.

img1.png
Figure 1. Smart Dustbin​


Materials required:
  • Raspberry Pi
  • Servo Motor
  • 2WD Chassis
  • DC Motors - 2
  • Motor Driver
  • IR Sensors - 2

Building the Rover
  • Mount the DC motors and the motor driver on the chassis.
  • Wire up the motors with the output pins of the motor driver.
  • Connect the input pins of the motor driver with the GPIO pins of the raspberry pi. And VCC pin with 5V pin of the raspberry pi and GND pin to the ground pin of the raspberry pi.
  • Attach 2 IR sensors to the front of the chassis. IR sensors should be facing the ground.
  • Connect the data wires of IR sensors to the GPIO pins of the raspberry pi. And ground wires to the GND pin of the raspberry pi.
  • Mount a cardboard box to the chassis, this will be utilized as the dustbin. Attach the lever of the servo motor to the lid of the box such that when the servo motor rotates in the clockwise direction then it should open and when it rotates in an anticlockwise direction then it should close.

Rover Movement Code

  • Create a directory named sdustbin. Create a python file named rover in that directory. Import RPi.GPIO as GPIO and set its mode as GPIO.BCM
    Code:
    import RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)
  • Setup the GPIO pins of the raspberry pi connected with IR sensors as GPIO.IN, similarly setup the GPIO pins of raspberry pi connected with motor driver as GPIO.OUT
    Code:
    GPIO.setup(number,GPIO.IN)
  • After the completion of the setup of GPIO pins, add an if condition stating if the input from both the IR sensors is TRUE value then the rover should move forward. If it’s false then they should move backward.
  • If the left IR sensor value is True and right IR sensor value is False then it should turn left. If the right IR sensor is True and left IR sensor value is False then it should turn right.
  • Code:
    if GPIO.input(leftIRnumber)==True and GPIO.input(rightIRnumber)==True :
        GPIO.output(number,True)
        GPIO.output(number,True)
        GPIO.output(number,True)
        GPIO.output(number,True)

Accordingly change the values of pin number and Truth values (True/False) for other directional movements.​
Telegram Bot Integration

Let’s create a telegram bot for ordering the dustbin to come to your desk and return to it’s position.

Create a bot:
  • Create a telegram account if you don’t have one.
  • Search for “@botfather” telegram bot, and type “/help” to see all the commands.
  • Type “/newbot” to create a new bot.
  • Choose a name and a simple username for your bot.
  • You can see that an API token is generated for your bot. Kindly note it somewhere accessible easily, we will need it for writing our code for our smart dustbin ahead.

Commands for our bot:
Let’s add some commands like “come”, “go”, “open” and “close” for our bot. The commands “come” and “go” will make our bot to reach your desk from its location and also return back. And the commands “open” and “close” will open and close the lid of our dustbin.

  • Install the module named telepot in your raspberry pi with a simple command line “pip install telepot” by executing it in the terminal.
  • Create a new python file named tele in the sdustbin directory.
  • Open up the file and import telepot module in it. Authenticate your code with the API token of telegram bot.
    Code:
    bot = telepot.Bot(API TOKEN HERE)
  • Get updates from the bot and extract the recent message from the updates. Convert those messages to string format.
    Code:
    A = bot.getUpdates()[-1]
  • If the message if “come” then send a reply message of your choice like, “will be there in a minute”, else if the message is “go” then send a reply message of your choice like, “Ok Bye!”.
    Code:
    bot.sendMessage(userID,'Ok Bye!')


Combining the code

Now we will be combining the code of rover.py and the code of tele.py
  • Create a python file named ccfinal in the sdustin directory.
  • Copy and paste all the import statements from both the files. Also import time here.
  • Define a function named setup and paste the lines of code of GPIO setup from the rover.py file. Call the setup function.
  • Paste the Authentication line of code form the tele.py file.
  • Create an infinite while loop having True as a condition. This loop will keep receiving live updates from our telegram user. Paste the code of getting updates from the user.
  • Now apply an if condition stating if the message is “come” then bot should send a reply message. Beneath that create another infinite while loop, and paste the code of rover movement from rover.py.
  • After that add another if condition stating if the message is “go” then the bot should send a reply message. Beneath that create another infinite while loop, and write the code of right turn with the sleep time of 3 seconds. This will make the dustbin take a turn of 180 degrees and move back to its position.
After that paste the code of rover movement from rover.py.
  • Add another if condition for the message “open” and under it paste code of lid opening.
  • Add another if condition for the message “close” and under it paste code of lid closing.

Hence this completes the building of our smart dustbin, now draw a black line or paste a black tape from the start point of the dustbin to your desk. The dustbin will follow this line to reach your desk upon calling.

Power the Pi from a power bank and mount it with the chassis. Run the ccfinal.py file and give commands to the dustbin using the telegram bot.

You can also create a small bash script for always running the ccfinal.py file whenever the Pi is powered up.

Cheers and Happy Learning!
Ajay Kumar
 
  • Like
Reactions: Quantum