Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560-Based Sound and Distance Activated Motor with LED Indicator

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560-Based Sound and Distance Activated Motor with LED Indicator

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

  1. ULN 2003

    • Description: Darlington transistor array
    • Pins: IN1, IN2, IN3, IN4, IN5, IN6, IN7, GND, +, COIL1, COIL2, COIL3, COIL4, COIL5
  2. DC Motor

    • Description: Standard DC motor
    • Pins: pin 1, pin 2
  3. 3.7V Battery

    • Description: Lithium-ion battery
    • Pins: +, -
  4. MT3608

    • Description: DC-DC step-up converter
    • Pins: VOUT+, VIN+, VOUT-, VIN-
  5. TP4056

    • Description: Lithium battery charger module
    • Pins: OUT-, B-, B+, OUT+, IN-, IN+
  6. Ultrasonic Sensor

    • Description: Distance measuring sensor
    • Pins: +VCC, Trigger, Echo, GND
  7. Arduino Mega 2560

    • Description: Microcontroller board
    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, SDA, SCL, AREF
  8. Sound Sensor

    • Description: Sound detection sensor
    • Pins: Analog, GND, VCC, Digital
  9. LED: Two Pin (green)

    • Description: Green LED
    • Pins: cathode, anode
  10. Resistor

    • Description: 220 Ohms resistor
    • Pins: pin1, pin2
    • Properties: Resistance = 220 Ohms

Wiring Details

ULN 2003

DC Motor

3.7V Battery

MT3608

TP4056

Ultrasonic Sensor

Arduino Mega 2560

Sound Sensor

LED: Two Pin (green)

Resistor

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.