PostsWifi

Automatic trash can opener based on distance

This is a guide for an automatic trashcan opener, that will automatically open when it sense that someone is in front of it. In this guide I will be using an Arduino Uno to showcase how this can be done.  The idea behind this is pretty simple as you have an ultrasonic sensor that can detect object in front of it  and when it detects something within a certain radius, it will activate a servo or a motor to rotate a certain amount.

 

Hardware

1 X Microcontroller

1 X Ultrasonic sensor

1 X Servo/Motor

 

Schematics

Ultrasonic sensor       sensor

GND(-)                       GND(-)

3.3/5V(+)                   VCC(+)

trigPin                        7

echoPin                      6

 

Servo                          sensor

GND(-)                       GND(-)

5V(+)                          VCC(+)

servoPin                     11

 

Code


</pre>

#include <Servo.h>

// Declare the Servo pin

int servoPin = 11;

// Create a servo object

Servo Servo1;

&nbsp;

// defines pins numbers

const int trigPin = 7;

const int echoPin = 6;

&nbsp;

// defines variables

long duration;

int distance;

&nbsp;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

Servo1.attach(servoPin);

}

&nbsp;

void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

&nbsp;

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

&nbsp;

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

&nbsp;

// Calculating the distance

distance= duration*0.034/2;

&nbsp;

if(distance <= 60){

Servo1.write(0);

}else{

Servo1.write(90);

}

&nbsp;

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

}

<pre>

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 adjust brightness according to how bright the environment is

PostsWifi

Doorlock using Keypad with RTW

Legg igjen en kommentar

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