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

Arduino Nano Controlled Clap-Activated Sound and Light System

View and Edit Project In Cirkit DesignerImage of Arduino Nano Controlled Clap-Activated Sound and Light System

Circuit Documentation

Summary

This circuit is designed to integrate an Arduino Nano with various components including an Adafruit Audio FX Mini Sound Board, a SparkFun Electret Microphone Breakout, a TIP120 Hi-Current Darlington Transistor, a 12V power supply, a loudspeaker, a Power LED, and a 1 kilohm resistor. The Arduino Nano serves as the central processing unit, controlling the LED and sound playback based on input from the microphone. The Adafruit Audio FX Mini Sound Board is used for audio output, which is played through the loudspeaker. The TIP120 transistor is used to control the high-current LED. The circuit is powered by a 12V power supply, which also powers the LED.

Component List

Wiring Details

Arduino Nano

Adafruit Audio FX Mini Sound Board

12V Power Supply

Loudspeaker

TIP120 Hi-Current Darlington Transistor

Power LED 12V 10W 0.8-0.9A

1 kilohm Resistor

SparkFun Electret Microphone Breakout

Documented Code

// Pin definitions
const int soundSensorPin = A0; // analog output connected to A0
const int ledPin = 6;          // LED bulb connected to D6
const int fxTriggerPin = 2;    // FX board trigger pin connected to D2

// Variables
bool ledState = false;         // To keep track of LED state
int clapCount = 0;             // To count the number of claps
unsigned long lastClapTime = 0; // To store the time of the last clap
int trackIndex = 0;            // To keep track of the current track for pin5

void setup() {
  pinMode(soundSensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(fxTriggerPin, OUTPUT);
  Serial.begin(9600);          // Initialize serial communication for debugging
}

void loop() {
  int sensorValue = analogRead(soundSensorPin);
  
  // Detect clap (assuming a threshold value for clap detection)
  if (sensorValue > 600) { // Adjust threshold as needed
    delay(50); // Debounce delay
    if (analogRead(soundSensorPin) > 600) {
      unsigned long currentTime = millis();
      unsigned long timeBetweenClaps = (currentTime - lastClapTime) / 1000; // Convert to seconds
      lastClapTime = currentTime;
      
      clapCount++;
      Serial.println("Clap detected!");
      
      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState ? HIGH : LOW);
      
      // Determine which track to play based on time between claps and clap count
      if (timeBetweenClaps >= 30 && timeBetweenClaps <= 45) {
        if (clapCount % 2 == 1) {
          playRandomTrack(1); // Odd clap, play from pin1
        } else {
          playRandomTrack(2); // Even clap, play from pin2
        }
      } else if (timeBetweenClaps >= 15 && timeBetweenClaps < 30) {
        if (clapCount % 2 == 1) {
          playRandomTrack(3); // Odd clap, play from pin3
        } else {
          playRandomTrack(4); // Even clap, play from pin4
        }
      } else if (timeBetweenClaps < 15) {
        playSequentialTrack(5); // Play sequentially from pin5
      } else {
        // Default action if time between claps is outside specified ranges
        playRandomTrack(0); // Play a random track from GPIO0_0 to GPIO0_4
      }
      
      // Wait for a short period to avoid multiple detections of the same clap
      delay(500);
    }
  }
}

void playRandomTrack(int pin) {
  // Generate a random number between 0 and 3 (for 4 tracks)
  int trackNumber = random(0, 4);
  
  // Send the track number to the FX board
  // Assuming the FX board is set up to play tracks on GPIO0_0 to GPIO0_4
  digitalWrite(fxTriggerPin, LOW); // Reset the trigger pin
  delay(10);
  digitalWrite(fxTriggerPin, HIGH); // Trigger the pin
  delay(10);
  digitalWrite(fxTriggerPin, LOW); // Reset the trigger pin
  
  Serial.print("Playing track from pin");
  Serial.print(pin);
  Serial.print(": ");
  Serial.println(trackNumber);
}

void playSequentialTrack(int pin) {
  // Play the next track in order from pin5
  int trackNumber = trackIndex % 8; // There are 8 tracks
  trackIndex++;
  
  // Send the track number to the FX board
  digitalWrite(fxTriggerPin, LOW); // Reset the trigger pin
  delay(10);
  digitalWrite(fxTriggerPin, HIGH); // Trigger the pin
  delay(10);
  digitalWrite(fxTriggerPin, LOW); // Reset the trigger pin
  
  Serial.print("Playing sequential track from pin");
  Serial.print(pin);
  Serial.print(": ");
  Serial.println(trackNumber);
}

This code is designed to run on the Arduino Nano and controls the behavior of the circuit based on audio input from the microphone. It detects claps and toggles the state of the LED, as well as plays audio tracks from the Adafruit Audio FX Mini Sound Board. The code includes functions for playing random and sequential tracks based on the clap pattern detected.