Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 35: Smart trashcan

This project revolves around the concept of a smart trash can. The primary aim is to have the trash can’s lid automatically open when an object approaches within a set distance (20cm in this case). The functionality is achieved by using an ultrasonic distance sensor paired with a servo motor. The distance between the object and the sensor is continually measured. If the object is close enough, the servo motor is triggered to open the lid.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Ultrasonic Sensor Module (HC-SR04)

BUY

Servo Motor (SG90)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_35_smart_trashcan_esp32_bb.png

Code

Code Analysis

The project is based on real-time monitoring of the distance between an object and a trash can. An ultrasonic sensor continuously measures this distance, and if an object approaches within 20cm, the trash can interprets it as an intention to dispose of waste and automatically opens its lid. This automation adds smartness and convenience to a regular trash can.

  1. Initial Setup and Variable Declaration

    Here, we’re including the ESP32Servo library and defining the constants and variables we’ll use. The pins for the servo and the ultrasonic sensor are declared. We also have an array averDist to hold the three distance measurements.

    #include <ESP32Servo.h>
    
    // Set up the servo motor parameters
    Servo servo;
    const int servoPin = 27;
    const int openAngle = 0;
    const int closeAngle = 90;
    
    // Define the minimum and maximum pulse widths for the servo
    const int minPulseWidth = 500; // 0.5 ms
    const int maxPulseWidth = 2500; // 2.5 ms
    
    
    // Set up the ultrasonic sensor parameters
    const int trigPin = 26;
    const int echoPin = 25;
    long distance, averageDistance;
    long averDist[3];
    
    // Distance threshold in centimeters
    const int distanceThreshold = 20;
    
  2. setup() Function

    The setup() function initializes serial communication, configures the ultrasonic sensor’s pins, and sets the initial position of the servo to the closed position.

    void setup() {
      Serial.begin(9600);
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      servo.attach(servoPin);
      servo.write(closeAngle);
      delay(100);
    }
    
  3. loop() Function

    The loop() function is responsible for continuously measuring the distance, computing its average, and then making a decision whether to open or close the trash can’s lid based on this averaged distance.

    void loop() {
        // Measure the distance three times
        for (int i = 0; i <= 2; i++) {
            distance = readDistance();
            averDist[i] = distance;
            delay(10);
        }
    
        // Calculate the average distance
        averageDistance = (averDist[0] + averDist[1] + averDist[2]) / 3;
        Serial.println(averageDistance);
    
        // Control the servo based on the averaged distance
        if (averageDistance <= distanceThreshold) {
            servo.attach(servoPin);  // Reattach the servo before sending a command
            delay(1);
            servo.write(openAngle);  // Rotate the servo to the open position
            delay(3500);
        } else {
            servo.write(closeAngle);  // Rotate the servo back to the closed position
            delay(1000);
            servo.detach();  // Detach the servo to save power when not in use
        }
    }
    
  4. Distance Reading Function

    This function, readDistance(), is what actually interacts with the ultrasonic sensor. It sends a pulse and waits for an echo. The time taken for the echo is then used to calculate the distance between the sensor and any object in front of it.

    You can refer to the Principle of the ultrasonic sensor.

    float readDistance() {
        // Send a pulse on the trigger pin of the ultrasonic sensor
        digitalWrite(trigPin, LOW);
        delayMicroseconds(2);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);
    
        // Measure the pulse width of the echo pin and calculate the distance value
        float distance = pulseIn(echoPin, HIGH) / 58.00;  // Formula: (340m/s * 1us) / 2
        return distance;
    }
    
  5. Servo Write Function

    This function maps the angle value to pulse width and calls the writeMicroseconds(pulseWidth) function to deflect the servo to a specific angle.

    // Function to make the servo work
    void servoWrite(int angle){
        int pulseWidth = map(angle, 0, 180, minPulseWidth, maxPulseWidth);
        servo.writeMicroseconds(pulseWidth);
    }