This circuit is designed to control a 12V geared motor and a blue LED using an Arduino UNO microcontroller based on input from a sound sensor and a limit switch. The motor's direction is controlled by an L298N DC motor driver, and the LED is switched on and off using a 1-channel 5V relay module. The system also includes a 12V power supply to power the motor and the LED. An I2C LCD screen is used to display the system status.
5V
to Relay Module VCC+
, Sound Sensor VCC
GND
to Limit Switch NO
, Relay Module VCC- (GND)
, COM
, I2C LCD Screen GND
, Sound Sensor GND
, L298N Motor Driver GND
Vin
to 12V Power Supply +
, 12V Blue LED VCC
, L298N Motor Driver 12V
A4
to I2C LCD Screen SDA
A5
to I2C LCD Screen SCL
D10
to L298N Motor Driver ENA
D6
to Limit Switch C
D5
to L298N Motor Driver IN2
D4
to L298N Motor Driver IN1
D3
to Relay Module IN
D2
to Sound Sensor Digital
Terminal 1
to L298N Motor Driver OUT1
Terminal 2
to L298N Motor Driver OUT2
-
to Arduino UNO GND
GND
to Relay Module N.O.
GND
to Arduino UNO GND
12V
to Arduino UNO Vin
ENA
to Arduino UNO D10
IN1
to Arduino UNO D4
IN2
to Arduino UNO D5
OUT1
to 12V Geared Motor Terminal 1
OUT2
to 12V Geared Motor Terminal 2
VCC
to Arduino UNO 5V
GND
to Arduino UNO GND
Digital
to Arduino UNO D2
VCC+
to Arduino UNO 5V
VCC- (GND)
to Arduino UNO GND
IN
to Arduino UNO D3
COM
to Arduino UNO GND
N.O.
to 12V Blue LED GND
C
to Arduino UNO D6
NO
to Arduino UNO GND
SDA
to Arduino UNO A4
SCL
to Arduino UNO A5
GND
to Arduino UNO GND
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
const int soundSensorPin = 2; // Digital pin for sound sensor
const int relayPin = 3; // Digital pin for relay (LED)
const int motorForwardPin = 4; // Motor driver forward control pin
const int motorBackwardPin = 5; // Motor driver backward control pin
const int limitSwitchPin = 6; // Limit switch pin
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address as necessary
// State Variables
bool drawerOpen = false;
void setup() {
// Initialize pins
pinMode(soundSensorPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(motorForwardPin, OUTPUT);
pinMode(motorBackwardPin, OUTPUT);
pinMode(limitSwitchPin, INPUT_PULLUP); // Use internal pull-up resistor
// Initialize LCD
lcd.begin();
lcd.backlight();
lcd.print("Ready to Clap");
}
void loop() {
// Check for clap
if (digitalRead(soundSensorPin) == HIGH) {
delay(200); // Debounce delay for sound sensor
if (!drawerOpen) {
openDrawer();
} else {
closeDrawer();
}
// Wait until clap is no longer detected
while (digitalRead(soundSensorPin) == HIGH) {
delay(50);
}
}
}
void openDrawer() {
digitalWrite(relayPin, HIGH); // Turn on LED
lcd.clear();
lcd.print("Opening Drawer");
digitalWrite(motorForwardPin, HIGH); // Move motor forward
// Run motor until limit switch is pressed
while (digitalRead(limitSwitchPin) == HIGH) {
delay(50); // Debounce limit switch
}
digitalWrite(motorForwardPin, LOW); // Stop motor
drawerOpen = true;
lcd.clear();
lcd.print("Drawer Open");
delay(1000); // Pause to display message
lcd.clear();
lcd.print("Ready to Clap");
}
void closeDrawer() {
digitalWrite(relayPin, LOW); // Turn off LED
lcd.clear();
lcd.print("Closing Drawer");
digitalWrite(motorBackwardPin, HIGH); // Move motor backward
// Run motor until limit switch is released
while (digitalRead(limitSwitchPin) == LOW) {
delay(50); // Debounce limit switch
}
digitalWrite(motorBackwardPin, LOW); // Stop motor
drawerOpen = false;
lcd.clear();
lcd.print("Drawer Closed");
delay(1000); // Pause to display message
lcd.clear();
lcd.print("Ready to Clap");
}
This code is designed to run on the Arduino UNO and controls the motor, LED, and LCD based on input from the sound sensor and limit switch. When a clap is detected by the sound sensor, the system will either open or close a drawer, indicated by the motor's movement and the LED's state. The LCD provides feedback on the system's status.