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.
#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.