Arduino-Based Bottle Detection and Reward Dispensing System with Ultrasonic and IR Sensors
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
This circuit is designed to detect the presence of an object, such as a bottle, using an HC-SR04 Ultrasonic Sensor and confirm its presence with an IR sensor. Upon confirmation, a reward is dispensed using an SG90 servo motor. The system's status and actions are displayed on a 16x2 LCD screen with I2C interface. The entire circuit is controlled by an Arduino UNO microcontroller, which manages sensor readings, controls the servo motor, and updates the LCD display.
Component List
HC-SR04 Ultrasonic Sensor
- Description: A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
- Pins: VCC, TRIG, ECHO, GND
SG90 Servo Motor
- Description: A small and lightweight servo motor capable of precise angular rotation.
- Pins: PWM, 5V, GND
LCD Screen 16x2 I2C
- Description: A liquid crystal display capable of displaying two lines of 16 characters each, with an I2C interface for communication.
- Pins: SCL, SDA, VCC, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P, with digital and analog I/O pins.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
IR Sensor
- Description: A sensor that detects infrared light reflecting off an object to determine its presence.
- Pins: OUT, GND, VCC
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC connected to Arduino UNO 5V
- TRIG connected to Arduino UNO D9
- ECHO connected to Arduino UNO D10
- GND connected to Arduino UNO GND
SG90 Servo Motor
- PWM connected to Arduino UNO D6
- 5V connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
LCD Screen 16x2 I2C
- SCL connected to Arduino UNO A5
- SDA connected to Arduino UNO A4
- VCC connected to Arduino UNO 5V
- GND connected to Arduino UNO GND
IR Sensor
- OUT connected to Arduino UNO D11
- GND connected to Arduino UNO GND
- VCC connected to Arduino UNO 5V
Documented Code
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;
// Infrared sensor pin
const int irSensorPin = 11;
// Servo motor for paper reward
Servo rewardServo;
const int servoPin = 6;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 chars, 2 lines
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize IR sensor pin
pinMode(irSensorPin, INPUT);
// Initialize servo motor
rewardServo.attach(servoPin);
rewardServo.write(0); // Start with the servo at 0 degrees
// Initialize LCD display
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("System Ready!");
Serial.println("System Initialized");
}
void loop() {
// Ultrasonic sensor reading
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Convert to cm
// Check if a bottle is detected within 20 cm
if (distance < 20) {
Serial.println("Bottle detected by Ultrasonic Sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bottle Detected");
// Check IR sensor for confirmation
if (digitalRead(irSensorPin) == LOW) { // LOW indicates object detected
Serial.println("Bottle confirmed by IR Sensor!");
lcd.setCursor(0, 1);
lcd.print("Bottle Confirmed");
// Dispense the reward
dispenseReward();
// Provide feedback on reward
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reward Dispensed!");
Serial.println("Reward Dispensed!");
delay(3000); // Wait to show message
}
} else {
lcd.setCursor(0, 0);
lcd.print("Insert Bottle...");
Serial.println("No bottle detected");
}
delay(1000); // Delay to avoid rapid sensor readings
}
// Function to handle reward dispensing
void dispenseReward() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing Reward...");
// Move servo to 90 degrees to dispense reward (paper)
rewardServo.write(90);
delay(2000); // Wait for 2 seconds
// Move the servo back to 0 degrees (reset)
rewardServo.write(0);
}
This code is responsible for initializing the sensors, servo motor, and LCD screen. It continuously checks for the presence of an object using the ultrasonic sensor and confirms it with the IR sensor. Upon confirmation, it activates the servo motor to dispense a reward and updates the LCD display accordingly.