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

Arduino UNO Sound-Activated Relay for AC Bulb Control

View and Edit Project In Cirkit DesignerImage of Arduino UNO Sound-Activated Relay for AC Bulb Control

Circuit Documentation

Summary

The circuit is designed to detect specific frequencies of sound using a KY-038 sound sensor and control an AC bulb through a relay module based on the detected frequency. The Arduino UNO serves as the central processing unit, reading the analog signals from the sound sensor and the photoresistor sensor, and controlling the relay module accordingly. The relay module switches the AC bulb on or off, and the photoresistor sensor provides additional input that could be used for light-based control logic. A resistor is included for potential voltage division or current limiting purposes.

Component List

Arduino UNO

Resistor (10k Ohms)

Photoresistor (LDR) Sensor (Wokwi Compatible)

SparkFun Beefcake Relay Control Kit

AC Bulb

Power 220V

KY-038 Sound Sensor Module

Wiring Details

Arduino UNO

Resistor (10k Ohms)

Photoresistor (LDR) Sensor (Wokwi Compatible)

SparkFun Beefcake Relay Control Kit

AC Bulb

Power 220V

KY-038 Sound Sensor Module

Documented Code

#include <ArduinoFFT.h>

#define SOUND_SENSOR_PIN A0  // Analog pin connected to the sound sensor
#define RELAY_PIN 8          // Digital pin connected to the relay module

const uint16_t samples = 128; // Must be a power of 2
const double samplingFrequency = 1000; // Hz, must be less than 10000 due to ADC

unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[samples];     // Real part of the signal
double vImag[samples];     // Imaginary part of the signal

ArduinoFFT FFT = ArduinoFFT(); // Create FFT object

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 baud rate

  pinMode(RELAY_PIN, OUTPUT); // Set the relay pin as an output
  digitalWrite(RELAY_PIN, LOW); // Ensure the relay is off initially

  sampling_period_us = round(1000000 * (1.0 / samplingFrequency)); // Calculate the sampling period
}

void loop() {
  /* Sample the signal */
  for (int i = 0; i < samples; i++) {
    microseconds = micros();
    
    vReal[i] = analogRead(SOUND_SENSOR_PIN); // Read the analog input and store it in the vReal array
    vImag[i] = 0; // Initialize the vImag array with zeros

    while (micros() - microseconds < sampling_period_us) {
      // Do nothing to wait for the next sample time
    }
  }

  /* Compute FFT */
  FFT.Compute(vReal, vImag, samples, FFT_FORWARD); // Compute the FFT
  FFT.ComplexToMagnitude(vReal, vImag, samples); // Convert complex numbers to magnitudes

  /* Find the peak frequency */
  double peakFrequency = FFT.MajorPeak(vReal, samples, samplingFrequency);

  /* Print the peak frequency to the Serial Monitor */
  Serial.print("Peak Frequency: ");
  Serial.println(peakFrequency);

  /* Check if the peak frequency is within the clap frequency range */
  double lowerThreshold = 1000.0; // Lower frequency threshold (in Hz)
  double upperThreshold = 2000.0; // Upper frequency threshold (in Hz)

  if (peakFrequency >= lowerThreshold && peakFrequency <= upperThreshold) {
    // Frequency is within range, turn on the relay
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("Relay ON");
  } else {
    // Frequency is outside range, turn off the relay
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Relay OFF");
  }

  // Wait a short while before sampling again
  delay(100);
}

This code is designed to run on the Arduino UNO. It uses the ArduinoFFT library to perform a Fast Fourier Transform (FFT) on the signal from the sound sensor. The code samples the sound sensor at a specified frequency, computes the FFT, and then finds the peak frequency. If the peak frequency is within a specified range, it turns on a relay, which could be used to control an AC bulb or other device. The relay is turned off if the frequency is outside the specified range.