Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Nano Controlled Clap-Activated Sound and Light System

Image 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

  • Arduino Nano: A compact microcontroller board based on the ATmega328P, featuring digital and analog I/O pins.
  • Adafruit Audio FX Mini Sound Board: A sound playback module capable of storing and playing WAV and OGG files.
  • 12V Power Supply: Provides the necessary power to the circuit components.
  • Loudspeaker: An electroacoustic transducer used to produce sound in response to an electrical audio signal input.
  • TIP120 Hi-Current Darlington Transistor: A transistor used for controlling high-current loads.
  • Power LED 12V 10W 0.8-0.9A: A high-power light-emitting diode for illumination.
  • 1 kilohm Resistor: A resistor with a resistance of 1 kilohm, used for current limiting or voltage division.
  • SparkFun Electret Microphone Breakout: A small and sensitive microphone with an onboard amplifier for audio signal capture.

Wiring Details

Arduino Nano

  • 3V3 connected to SparkFun Electret Microphone Breakout VCC and Adafruit Audio FX Mini Sound Board VIN
  • A0 connected to SparkFun Electret Microphone Breakout AUD
  • D6 connected to TIP120 Transistor BASE (via 1 kilohm resistor)
  • GND connected to common ground net
  • D0/RX connected to Adafruit Audio FX Mini Sound Board TX
  • VIN connected to 12V power supply + and Power LED +
  • D1/TX connected to Adafruit Audio FX Mini Sound Board RX_5V

Adafruit Audio FX Mini Sound Board

  • RST, GPIO0_0 to GPIO0_7, ACT, L_AC, CS, UG, VBUS not connected
  • VIN connected to Arduino Nano 3V3
  • GND connected to common ground net
  • TX connected to Arduino Nano D0/RX
  • RX_5V connected to Arduino Nano D1/TX
  • R_AC connected to Loudspeaker pin2

12V Power Supply

  • + connected to Arduino Nano VIN and Power LED +
  • - connected to common ground net

Loudspeaker

  • pin1 connected to Adafruit Audio FX Mini Sound Board GND
  • pin2 connected to Adafruit Audio FX Mini Sound Board R_AC

TIP120 Hi-Current Darlington Transistor

  • BASE connected to Arduino Nano D6 (via 1 kilohm resistor)
  • COLLECTOR connected to Power LED -
  • EMITTER connected to common ground net

Power LED 12V 10W 0.8-0.9A

  • + connected to 12V power supply + and Arduino Nano VIN
  • - connected to TIP120 Transistor COLLECTOR

1 kilohm Resistor

  • pin1 connected to TIP120 Transistor BASE
  • pin2 connected to Arduino Nano D6

SparkFun Electret Microphone Breakout

  • VCC connected to Arduino Nano 3V3
  • GND connected to common ground net
  • AUD connected to Arduino Nano A0

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.