Arduino Project

Trash Can

Course Introduction

In this lesson, you’ll learn how to use an ultrasonic sensor module, a digital servo motor, and an Arduino board to build a smart trash can.

When the ultrasonic sensor module detects trash being thrown in, the digital servo motor opens the lid of the trash can.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima

1

2

USB Type-C cable

1

×

3

Breadboard

1

4

Wires

Several

5

1kΩ resistor

2

6

Ultrasonic Sensor Module

1

7

LED

2

8

Digital Servo Motor

1

Wiring

9.webp__PID:aa56e75f-f9f9-47e3-9feb-e6ccaffd4a9f

Common Connections:

Digital Servo Motor
Connect to breadboard’s positive power bus.
Connect to breadboard’s negative power bus.
Connect to 9 on the Arduino.

Ultrasonic Sensor Module
Trig:
Connect to 6 on the Arduino.
Echo:Connect to 5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Writing the Code

Note
You can copy this code into the Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima/WIFI) and the correct port before clicking the Upload button.


#include 

// Set up the servo motor parameters
Servo servo;
const int servoPin = 9;
const int openAngle = 0;
const int closeAngle = 90;

// Set up the ultrasonic sensor parameters
const int trigPin = 5;
const int echoPin = 6;
long distance, averageDistance;
long averDist[3];

// Distance threshold in centimeters
const int distanceThreshold = 20;

void setup() {
  // Initialize serial communication with the computer at 9600 baud rate
  Serial.begin(9600);

  // Configure the trigger and echo pins of the ultrasonic sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach the servo to its control pin and set its initial position
  servo.attach(servoPin);
  servo.write(closeAngle);
  delay(100);
  servo.detach();  // Detach the servo to save power when not in use
}

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
  }
}

// Function to read the sensor data and calculate the distance
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;
}