Arduino Mega 2560-Based Sound and Distance Activated Motor with LED Indicator
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to detect sound and measure distance using an ultrasonic sensor. When sound is detected and an object is within a specified range, a DC motor is activated, and an LED is turned on. The circuit is powered by a 3.7V battery pack, which is managed by a TP4056 charging module and boosted to 5V using an MT3608 module. The control logic is implemented using an Arduino Mega 2560 microcontroller.
Component List
ULN 2003
- Description: Darlington transistor array
- Pins: IN1, IN2, IN3, IN4, IN5, IN6, IN7, GND, +, COIL1, COIL2, COIL3, COIL4, COIL5
DC Motor
- Description: Standard DC motor
- Pins: pin 1, pin 2
3.7V Battery
- Description: Lithium-ion battery
- Pins: +, -
MT3608
- Description: DC-DC step-up converter
- Pins: VOUT+, VIN+, VOUT-, VIN-
TP4056
- Description: Lithium battery charger module
- Pins: OUT-, B-, B+, OUT+, IN-, IN+
Ultrasonic Sensor
- Description: Distance measuring sensor
- Pins: +VCC, Trigger, Echo, GND
Arduino Mega 2560
- Description: Microcontroller board
- Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, SDA, SCL, AREF
Sound Sensor
- Description: Sound detection sensor
- Pins: Analog, GND, VCC, Digital
LED: Two Pin (green)
- Description: Green LED
- Pins: cathode, anode
Resistor
- Description: 220 Ohms resistor
- Pins: pin1, pin2
- Properties: Resistance = 220 Ohms
Wiring Details
ULN 2003
- IN1: Connected to Arduino Mega 2560 pin D22
- IN2: Connected to Arduino Mega 2560 pin D23
- IN3: Connected to Arduino Mega 2560 pin D24
- GND: Connected to common ground
- +: Connected to 5V power supply
- COIL1: Connected to DC Motor pin 1
- COIL2: Connected to DC Motor pin 2
DC Motor
- pin 1: Connected to ULN 2003 COIL1
- pin 2: Connected to ULN 2003 COIL2
3.7V Battery
- +: Connected to TP4056 B+
- -: Connected to TP4056 B-
MT3608
- VOUT+: Connected to 5V power supply
- VIN+: Connected to TP4056 OUT+
- VOUT-: Connected to common ground
- VIN-: Connected to TP4056 OUT-
TP4056
- OUT+: Connected to MT3608 VIN+
- OUT-: Connected to MT3608 VIN-
- B+: Connected to 3.7V Battery +
- B-: Connected to 3.7V Battery -
Ultrasonic Sensor
- +VCC: Connected to 5V power supply
- Trigger: Connected to Arduino Mega 2560 pin D7 PWM
- Echo: Connected to Arduino Mega 2560 pin D6 PWM
- GND: Connected to common ground
Arduino Mega 2560
- 5V: Connected to 5V power supply
- GND: Connected to common ground
- D22: Connected to ULN 2003 IN1
- D23: Connected to ULN 2003 IN2
- D24: Connected to ULN 2003 IN3
- D7 PWM: Connected to Ultrasonic Sensor Trigger
- D6 PWM: Connected to Ultrasonic Sensor Echo
- D8 PWM: Connected to Sound Sensor Digital
- D13 PWM: Connected to LED anode
- A9: Connected to Sound Sensor VCC
- A10: Connected to Sound Sensor GND
Sound Sensor
- VCC: Connected to Arduino Mega 2560 pin A9
- GND: Connected to Arduino Mega 2560 pin A10
- Digital: Connected to Arduino Mega 2560 pin D8 PWM
LED: Two Pin (green)
- anode: Connected to Arduino Mega 2560 pin D13 PWM
- cathode: Connected to Resistor pin1
Resistor
- pin1: Connected to LED cathode
- pin2: Connected to common ground
Code Documentation
#define SOUND_SENSOR_PIN 8
#define TRIG_PIN 7
#define ECHO_PIN 6
#define MOTOR_IN1 9
#define MOTOR_IN2 10
#define MOTOR_IN3 11
#define MOTOR_IN4 12
#define LED_PIN 13
long duration;
int distance;
bool soundDetected = false;
// Motor steps
int motorSteps[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(SOUND_SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
for (int i = MOTOR_IN1; i <= MOTOR_IN4; i++) {
pinMode(i, OUTPUT);
}
Serial.begin(9600);
}
void loop() {
// Measure distance
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
// Check for sound
soundDetected = digitalRead(SOUND_SENSOR_PIN);
if (soundDetected && distance > 0 && distance < 50) { // Sound detected and object is nearby
Serial.println("Sound detected! Activating motor and LED.");
digitalWrite(LED_PIN, HIGH); // Turn on LED
spinMotor();
} else {
Serial.println("No sound or object out of range.");
digitalWrite(LED_PIN, LOW); // Turn off LED
stopMotor();
}
delay(100);
}
void spinMotor() {
for (int step = 0; step < 4; step++) {
for (int i = 0; i < 4; i++) {
digitalWrite(MOTOR_IN1 + i, motorSteps[step][i]);
}
delay(2);
}
}
void stopMotor() {
for (int i = MOTOR_IN1; i <= MOTOR_IN4; i++) {
digitalWrite(i, LOW);
}
}
This code defines the pins for the sound sensor, ultrasonic sensor, motor, and LED. It measures the distance using the ultrasonic sensor and checks for sound using the sound sensor. If sound is detected and an object is within a specified range, the motor is activated, and the LED is turned on. The motor is controlled using a step sequence, and the LED is turned off when no sound is detected or the object is out of range.