PostsWifi

Smart trash can using Ultrasonic sensor + Sending/Recieving RTW data


In this tutorial I will show you how to connect your ultrasonic range sensor to ESP32 displaying the data though serial monitor.I will be using the HC-SR04 ultrasonic sensor, which is a module that measures the distance from an object using sonar. It works by sending out a high pulse and calculating how long it takes before it returns.

Hardware

1 x ESP32 OLED

1 x HC-SR04

8 x Wire

1 x 200 Ohm Resistor

Schematics

The HC-SR04 has 4 pins which includes Vcc, Gnd, Trig and Echo. And the led has a GND and a pin for Vcc.

HC-Sro4

GND Gnd
5V Vcc
Pin 10 Echo
Pin 9 Trig

Led

GND Gnd
Pin 13 Vcc

Trig and echo need to be connected to digital pins, so on this example I have connected them to 9 and 10 on the ESP32. I also have a the VCC for the Led on Pin 13 Which is also a digitalpin. This may vary depending on what kind of microcontroller you have.

Source Code

Start with defining each pin, echo and trig. After that you need to define the type of variables that will be used, which is long duration and int distance , initial distance and initial duration too which is of the same type and String level. In setup() the serial monitor is initialized and set the trigPin to OUTPUT and echoPIN to INPUT ledPin as Output.
Then you will setup the Wifi to connect to the Wifi so you can send data to RTW.
After you will do your first reading by clearing the trigger with digitalWrite(trigpin,low) with a second delay. There after you do the same but with just (trigpin,high) and a 10 microsecond delay then (trigpin,low).

Then to get the duration is with the pulse you will be using pulseIn(echoPin,HIGH). It will then give you the duration and store it in the initial duration. Then you can use the initial duration to calculate the distancec with this formula init_distance= init_duration*0.034/2;.
With this you will get the initial duration and initial distance when you first start the program. This will be the value you reference to , to check how full the trash can is.
In loop() you will do pretty much the same but you will keep looping the trig pin on and off . the same with echo pin. So you keep doing readings and as the values change the level of the trash can will change.
Finally, the result is ready to be printed out in serial monitor using Serial.print(distance).

#include <Nornir_ESP32.h>
// RTW config
const String host = "http://[domain].rtw.no/[service]?objectID=[id number]&token=[your token]"

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 13;

// defines variables
long duration;
int distance;
long init_duration;
int init_distance;
String level;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode (ledPin, OUTPUT); //Sets the ledPin to output
Serial.begin(115200);

//Paste Wifi Code here check Wifi post for the code
//Depends on what board you are using
//You may not need to manually config wifi
WifiCode

// Sets the pins to low to make sure
// That no pin is high when starting.
digitalWrite(ledPin, LOW);
digitalWrite(trigPin, LOW);

delay(1000);
//Here sets pin to high for 10 microseconds to do a read.
//Then set it back to low.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
init_duration = pulseIn(echoPin, HIGH);

// Calculating the distance
init_distance= init_duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.println(init_distance);
}
void loop() {
// Sets the pins to low to make sure
// That no pin is high when starting.
digitalWrite(ledPin , LOW);
digitalWrite(trigPin, LOW);

delay(1000);
//Here sets pin to high for 10 microseconds to do a read.
//Then set it back to low.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance and initial distance on the Serial Monitor (Just to keep track)
Serial.println(distance);
Serial.println(init_distance);
// PS !!!! This Code need to be adjusted depending on your thrash can length !
// Here we are substracting the distance we read from initial distance
// We do this to find out how much "distance" is actually in the thrash can
// F.example if there are nothing in the trash can and the init distance is 21
// Then it means that it will be 21 - 21 which is 0 which is also less then 5
// Which means that it will write out thrash can level 1
// Though you can make this dynamic by dividing init_distance by 4,3,2,1 etc to make lvls
// F.example init_distance - distance >= init_distance/4
// This will be lvl 4 etc.

if(init_distance - distance >= 15){
  digitalWrite(ledPin , HIGH);
  level = "Level 4";
  Serial.println("Thrash can level 4 almost full please consider changing bags");
  RTW_send_data(host, "Level= " + level);
  }
  else if(init_distance - distance <= 5)
  {Serial.println("Thrash can level 1 keep thrashing");
  level = "Level 1";
  RTW_send_data(host, "Level= " + level);
  }
    else if(init_distance - distance > 5 && init_distance - distance < 10)
  {Serial.println("Thrash can level 2 soon half full");
  level = "Level 2";
  RTW_send_data(host, "Level= " + level);
  }
  else if(init_distance - distance < 15 && init_distance - distance > 10)
  {
  digitalWrite(ledPin , HIGH);
  Serial.println("Thrash can level 3 3 quarters now");
  level = "Level 3";
  RTW_send_data(host, "Level= " + level);
  }
}
Related posts
IntegrationPostsThe Things Network

How we integrated an external service to RTW using Webhooks and PhP

PostsWifi

Code used to connect to WIFI with Esp32 (Referenced as WifiCode)

PostsWifi

Automatic trash can opener based on distance

PostsWifi

Automatic adjust brightness according to how bright the environment is

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *