iot
Make a smart dustbin using UNO arduino and some basic sensors
No one stays forever but you Your goals will happen, so focus
In this blog we will be discussing on how you can create your own smart dustbin which will be connected to a app and if someone tries to steal it, the app will notify you about it. We will be using Arduino UNO board, ultra sonic sensor, IR sensor and wifi module(ESP8266), it is a simple project.
In this blog, we'll be going over
Let’s get right into the first step!
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing.
A basic schematic diagram of Arduino UNO Board is shown below
Here, the USB Plug is used to upload the code in Arduino, there are in total 13 digital input output pins and 5 analog input output pins, a reset button to reset the code,Tx and Rx pins are used for serial communication , external power supply is used to connect to power external source.
You can download the Arduino UNO IDE from it's official website. The skeleton code in Arduino looks like
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
}
As You can see here there are two function i.e, setup and loop. The setup function contains the initializations of input, output pins and other setup data. Loop function basically contains the main code that has to executed repeatedly.
In this section I will be discussing-
Ultrasonic sensor
An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity.
It's a four pin setup Vcc, ground, trigger and echo, which is operated at 5V. Vcc and Gnd are used to provide potential difference to the ultrasonic sensor, when trig pin is a output pin, when it is high one of the transducer generates ultrasonic sound wave. Echo pin is a input pin, it sensors the ultrasonic sound and return a high response to arduino.
So, as you can see there are two transducer one end sends out the ultrasonic waves and other end receives the signal. The sensor basically calculates the time from when the ultrasonic wave was send to when it was received back, once we have the time, we know the speed of sound(340m/s) so, we can easily calculate the distance of the object from the sensor using the formula speed=distance/time.
Ultrasonic sensor circuit connections-
Code for distance calculation -
const int pingPin = 3; // Trigger Pin of Ultrasonic Sensor const int echoPin = 2; // Echo Pin of Ultrasonic Sensor
void setup()
{
Serial.begin(9600); // Starting Serial Terminal
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
long microsecondsToInches(long microseconds)
{ return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds)
{ return microseconds / 29 / 2; }
IR sensor
An infrared (IR) sensor is an electronic device that measures and detects infrared radiation in its surrounding environment. It have to LED's i.e, emitter and receiver. The emitter emits infrared radiation that are then reflected by object, to be precise some part of the radiation is absorbed by the object and a radiation of characteristic wavelength is reflected back to the sensor. Now the receive which is a photodiode senses the signal and generates a digital output. It can be used in obstacle detection, line following, and many other things.
IR sensor circuit connection-
IR sensor is three pin device namely Vcc, ground and output, it operates on 5V. Vcc and ground are used to provide potential difference across the device and output pin generates the output when IR sensor, senses infrared radiation reflected back from the object.
Basic object detection code-
void setup()
{ // put your setup code here, to run once:
pinMode(2,INPUT);
Serial.begin(9600);
}
void loop()
{ // put your main code here, to run repeatedly:
if(digitalRead(2)==LOW)
{
Serial.println("Object detected");
}
else
{
Serial.println("No object detected");
}
}
The design is quite simple, the sensors are places at the bottom and there are wheels. Out which one wheel is half black and half white for counting the number of rotations. All the wiring go through a hole in the center of the dustbin.
All the jumper cables are then connected to arduino inside the dustbin.
Here, IR sensor is used for counting the number of rotations that we will be discussing soon. Ultrasonic sensor simply calculates the distance of the sensor from the ground.
So, that's all about the Dustbin design.
Do you know you can set the sensitivity of IR sensor, using the rotator given on IR sensor. For counting the rotations we will set the sensitivity of the IR sensor to low so, that it doesn't sense black color at all so, this way we can sense if the colour placed in front of sense is black or any other colour.
As you can see in the design we have placed the IR sensor in front of the half black and half white wheel. So, when the wheel rotates the colour change from white to black and white again, we detect this change in colour using our IR sensor and calculate the number of rotations it's making.
Try out this code:
int irPin=3;
int LEDPin=13;
int white=0;
int black=0;
int count=0;
void setup() {
pinMode(LEDPin,OUTPUT);
pinMode(irPin,INPUT);
Serial.begin(9600);
}
void loop() {
if(digitalRead(irPin))
{
black++;
digitalWrite(LEDPin,LOW);
Serial.println("White");
}
else
{
white++;
digitalWrite(LEDPin,HIGH);
Serial.println("black");
}
if(white>20 && black>10)
{
white=0;
black=0;
count++;
}
Serial.println(count);
}
Now that we know how to setup our sensors and code them, let's now send that data from our arduino a server so that it can be shown in a app. We will be using a wifi module(ESP8266) for connecting to internet and sending data over internet.
You can follow up on this link to know how to set up your own ESP8266 module.
Now that you know how to setup your wifi module let's, connect to our adafruit server. In order to connect it to adafruit, go to there official website create a new account if you don't have one then you will get your own dashboard i.e,
Here you can add new dashboards and for each dashboards you can create multiple feed which can receive data from from your arduino board by connecting through wifi module.
In order to connect adafruit we use MQTT, so add Adafruit_MQTT_Library library to your arduino IDE by going to the following path ~/arduino-nightly/libraries. Once you have added the library try out the following code-
/***************************************************
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
Must use the latest version of ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
SSL/TLS additions by Todd Treece for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "WLAN_SSID"
#define WLAN_PASS "WIFI_PASSWORD"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
// Using port 8883 for MQTTS
#define AIO_SERVERPORT 8883
// Adafruit IO Account Configuration
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
/************ Global State (you don't need to change this!) ******************/
// WiFiFlientSecure for SSL/TLS support
WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// io.adafruit.com SHA1 fingerprint
static const char *fingerprint PROGMEM = "59 3C 48 0A B1 8B 39 4E 0D 58 50 47 9A 13 55 60 CC A0 1D AF";
/****************************** Feeds ***************************************/
// Setup a feed called 'test' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
/*************************** Sketch Code ************************************/
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// check the fingerprint of io.adafruit.com's SSL cert
client.setFingerprint(fingerprint);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// Now we can publish stuff!
Serial.print(F("\nSending val "));
Serial.print(x);
Serial.print(F(" to test feed..."));
if (! test.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// wait a couple seconds to avoid rate limit
delay(2000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
You finally did it !!!
Now go a ahead try making something cool from what you just learned
Here's GitHub repository for this project
Thank you for reading my blog, if you found it useful do share it with your friend. Also for more awesome blogs make sure to follow up on TechHub Community. TechHub is a great community to learn and explore new technologies. We also have a Discord Server, join today to get the latest updates.