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.



1 X Microcontroller

1 X Ultrasonic sensor

1 X Servo/Motor



Ultrasonic sensor       sensor

GND(-)                       GND(-)

3.3/5V(+)                   VCC(+)

trigPin                        7

echoPin                      6


Servo                          sensor

GND(-)                       GND(-)

5V(+)                          VCC(+)

servoPin                     11




#include <Servo.h>

// Declare the Servo pin

int servoPin = 11;

// Create a servo object

Servo Servo1;


// defines pins numbers

const int trigPin = 7;

const int echoPin = 6;


// defines variables

long duration;

int distance;


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




void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);



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

digitalWrite(trigPin, HIGH);


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;


if(distance <= 60){






// Prints the distance on the Serial Monitor

Serial.print("Distance: ");




Related posts
IntegrationPostsThe Things Network

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


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


Automatic adjust brightness according to how bright the environment is


Doorlock using Keypad with RTW

Legg igjen en kommentar

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