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

Gesture and Sound Controlled Relay Switching with Arduino Nano

View and Edit Project In Cirkit DesignerImage of Gesture and Sound Controlled Relay Switching with Arduino Nano

Circuit Documentation

Summary

This circuit is designed to interface an Arduino Nano with various components including an APDS-9960 RGB and Gesture Sensor, a KY-038 sound sensor module, a 4-channel relay module, a power transformer, two 40mm 12V fans, and two bulbs. The Arduino Nano serves as the central processing unit, reading gestures and sound signals to control the relays, which in turn power the fans and bulbs. The APDS-9960 sensor allows for gesture input, and the KY-038 detects sound (claps) as a form of input. The power transformer is used to step down or step up the voltage for the circuit, and the terminal PCBs are used for making power connections.

Component List

Wiring Details

Arduino Nano

APDS-9960 RGB and Gesture Sensor

KY-038 Sound Sensor Module

4 Channel Relay Module

Power Transformer

40mm Fan 12V (Two Fans)

Bulb (Two Bulbs)

Documented Code

#include <Wire.h>
#include <SparkFun_APDS9960.h> // Include the SparkFun Gesture Sensor library

SparkFun_APDS9960 apds = SparkFun_APDS9960();

const int soundSensorPin = 2;  // Sound sensor digital pin
const int relayPins[4] = {4, 5, 6, 7}; // Relay pins for controlling the 4 loads

bool loadStates[4] = {false, false, false, false}; // Track each load's state (on/off)
unsigned long lastClapTime = 0;
int clapCount = 0;
const int clapTimeout = 800;  // Timeout between claps (in milliseconds)

void setup() {
  Serial.begin(9600);

  // Initialize the gesture sensor
  if (apds.init()) {
    Serial.println("APDS-9960 initialized successfully");
  } else {
    Serial.println("Error initializing APDS-9960");
  }

  // Enable gesture mode
  if (apds.enableGestureSensor(true)) {
    Serial.println("Gesture sensor enabled");
  } else {
    Serial.println("Gesture sensor failed to enable");
  }

  // Set up relay pins
  for (int i = 0; i < 4; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], HIGH); // Relays are off by default (HIGH = off)
  }

  // Set up sound sensor pin
  pinMode(soundSensorPin, INPUT);

  Serial.println("System Initialized: Waiting for gestures or claps.");
}

void loop() {
  // Handle gesture control
  if (apds.isGestureAvailable()) {
    int gesture = apds.readGesture();
    handleGesture(gesture);
  }

  // Handle clap detection
  if (digitalRead(soundSensorPin) == HIGH) {
    unsigned long currentTime = millis();
    if (currentTime - lastClapTime > clapTimeout) {
      lastClapTime = currentTime;
      clapCount++;
      Serial.print("Clap detected! Clap count: ");
      Serial.println(clapCount);
    }
  }

  // Evaluate clap counts after timeout
  if (millis() - lastClapTime > clapTimeout && clapCount > 0) {
    handleClap(clapCount);
    clapCount = 0;
  }
}

// Function to handle gestures
void handleGesture(int gesture) {
  switch (gesture) {
    case DIR_UP:
      Serial.println("Gesture UP - Toggle Load 1");
      toggleRelay(0); // Toggle Load 1
      break;
    case DIR_DOWN:
      Serial.println("Gesture DOWN - Toggle Load 1");
      toggleRelay(0); // Toggle Load 1
      break;
    case DIR_LEFT:
      Serial.println("Gesture LEFT - Toggle Load 2");
      toggleRelay(1); // Toggle Load 2
      break;
    case DIR_RIGHT:
      Serial.println("Gesture RIGHT - Toggle Load 2");
      toggleRelay(1); // Toggle Load 2
      break;
    default:
      Serial.println("Unknown Gesture");
  }
}

// Function to handle claps
void handleClap(int count) {
  if (count == 1) {
    Serial.println("Single Clap - Toggle Load 3");
    toggleRelay(2); // Toggle Load 3
  } else if (count == 2) {
    Serial.println("Double Clap - Toggle Load 4");
    toggleRelay(3); // Toggle Load 4
  } else {
    Serial.println("Multiple Claps Detected");
  }
}

// Function to toggle relays
void toggleRelay(int relay) {
  loadStates[relay] = !loadStates[relay]; // Toggle the state
  digitalWrite(relayPins[relay], loadStates[relay] ? LOW : HIGH); // LOW = ON, HIGH = OFF
  Serial.print("Load ");
  Serial.print(relay + 1);
  Serial.println(loadStates[relay] ? " ON" : " OFF");
}

This code is designed to run on the Arduino Nano and utilizes the SparkFun APDS9960 library to handle gesture sensing. It sets up the necessary pins for the sound sensor and relay module, and contains functions to handle gestures and claps. The relays are toggled based on the input from the gesture sensor and sound sensor, controlling the connected loads (fans and bulbs).