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

Arduino-Controlled Interactive LED and Sound System with Motion Detection

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Interactive LED and Sound System with Motion Detection

Circuit Documentation

Summary

This circuit integrates various components to perform multiple functions. It includes an Arduino UNO as the central microcontroller, interfaced with an Adafruit ADXL345 accelerometer, an HC-SR04 ultrasonic sensor, two SG90 servomotors, a DFPlayer Mini MP3 player, an Adafruit Proto Shield R3 for prototyping, a loudspeaker, and a WS2812 RGB LED strip. The Arduino UNO manages sensor data processing, controls the servomotors and LED strip, and communicates with the DFPlayer Mini to play sounds. The circuit is designed to react to distance measurements and motion detection.

Component List

Arduino UNO

Adafruit ADXL345

HC-SR04 Ultrasonic Sensor

Servomotor SG90 (2 units)

DFPlayer Mini

Adafruit Proto Shield R3

Loudspeaker

WS2812 RGB LED Strip

Wiring Details

Arduino UNO

Adafruit ADXL345

HC-SR04 Ultrasonic Sensor

Servomotor SG90

DFPlayer Mini

Loudspeaker

WS2812 RGB LED Strip

Documented Code

#include <Adafruit_NeoPixel.h>
#include <Servo.h>
#include <Wire.h>
#include <SoftwareSerial.h>  // Used for communication with DFPlayer Mini
#include <DFRobotDFPlayerMini.h>

// RGB LED strip and servomotor settings
#define LED_PIN 11      // Data pin for the RGB LED strip is Pin 11
#define NUMPIXELS 14    // Assuming there are 14 LEDs (adjust according to your strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);  // Create RGB LED strip object

const int trigPin = 9;         // Ultrasonic sensor Trig pin
const int echoPin = 8;         // Ultrasonic sensor Echo pin

Servo servo1;  // Define the first servomotor object
Servo servo2;  // Define the second servomotor object

int currentAngle1 = 0;  // Current angle of servomotor 1
int currentAngle2 = 0;  // Current angle of servomotor 2

// DFPlayer Mini settings
SoftwareSerial mySoftwareSerial(5, 4); // RX, TX are on Pin 5 and Pin 4
DFRobotDFPlayerMini myDFPlayer;

// ADXL345 accelerometer address and registers
#define ADXL345_ADDRESS 0x53
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define DATAX0 0x32

void setup() {
  // Set pin modes
  pinMode(trigPin, OUTPUT);  // Ultrasonic sensor Trig pin as output
  pinMode(echoPin, INPUT);   // Ultrasonic sensor Echo pin as input
  
  // Connect two servomotors to Pin 12 and Pin 13
  servo1.attach(12);  // First servomotor connected to Pin 12
  servo2.attach(13);  // Second servomotor connected to Pin 13

  strip.begin();  // Initialize RGB LED strip
  strip.show();   // Initialize all pixels to 'off'

  Serial.begin(9600);  // Initialize serial communication at 9600 baud for debugging
  mySoftwareSerial.begin(9600); // DFPlayer serial communication speed
  if (!myDFPlayer.begin(mySoftwareSerial)) {  // Initialize DFPlayer Mini
    Serial.println(F("DFPlayer Mini init failed!"));
    while(true);
  }

  Serial.println(F("DFPlayer Mini online."));
  myDFPlayer.volume(10);  // Set volume, range is 0-30

  Wire.begin();  // Start I2C for accelerometer

  // Initialize accelerometer
  setupADXL345();
  
  Serial.println("System Initialized.");
}

void loop() {
    long duration, distance;
    digitalWrite(trigPin, LOW);    // Ensure Trig pin is low
    delayMicroseconds(2);          // Wait for 2 microseconds
    digitalWrite(trigPin, HIGH);   // Send ultrasonic signal
    delayMicroseconds(10);         // Last for 10 microseconds
    digitalWrite(trigPin, LOW);    // Stop signal
  
    duration = pulseIn(echoPin, HIGH);  // Read Echo pin, return duration
    distance = (duration * 0.034) / 2;  // Convert time to distance (cm)

    // Print distance data to the serial monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    // Control RGB LED strip color mapping, mapping range adjusted to 0-150cm
    if (distance <= 150) {  
      int redValue = map(distance, 0, 150, 255, 0);    // The closer the distance, the more red
      int greenValue = map(distance, 0, 150, 0, 255);  // The farther the distance, the more green
      int blueValue = map(distance, 0, 150, 255, 0);   // The farther the distance, the more blue
      uint32_t color = strip.Color(redValue, greenValue, blueValue);  // Generate RGB color

      // Update the color of all pixels
      for (int i = 0; i < NUMPIXELS; i++) {
        strip.setPixelColor(i, color);  // Set the color of each pixel
      }
      strip.show();  // Send updated color data
    } else {
      // Turn off the strip if over 150cm
      for (int i = 0; i < NUMPIXELS; i++) {
        strip.setPixelColor(i, strip.Color(0, 0, 0));  // Turn off the strip
      }
      strip.show();
    }

    // Control two servomotors, move when distance is less than 20
    int targetAngle1 = (distance < 20) ? 90 : 0;  // Target angle 1
    int targetAngle2 = (distance < 20) ? 45 : 30;  // Target angle 2

    // Only update if the target angle is different from the current angle
    if (abs(currentAngle1