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

Gesture and Sound Controlled Relay Switching with Arduino Nano

Image 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

  • Arduino Nano: A compact microcontroller board based on the ATmega328P, featuring digital and analog I/O pins, communication interfaces, and a USB connection for programming and power supply.
  • APDS-9960 RGB and Gesture Sensor: A sensor that offers ambient light and color measuring, proximity detection, and gesture sensing.
  • Terminal PCB 2 Pin: A simple two-pin terminal block for making secure power connections.
  • KY-038 Sound Sensor Module: A microphone with a digital output that detects sound levels.
  • 4 Channel Relay Module: A module with four relays that can control high power devices, each with normally open (N.O.) and normally closed (N.C.) contacts.
  • Power Transformer: A component that transforms electrical energy from one circuit to another through inductively coupled conductors.
  • 40mm Fan 12V: A 12V DC fan used for cooling purposes.
  • Bulb: A simple electric light bulb.

Wiring Details

Arduino Nano

  • GND: Connected to the GND pins of the KY-038, APDS-9960 sensor, and one terminal of the 2-pin terminal PCB.
  • D2: Connected to the D0 pin of the KY-038.
  • D4, D5, D6, D7: Connected to the IN 1, IN 2, IN 3, IN 4 pins of the 4-channel relay module respectively.
  • VIN: Connected to one terminal of a 2-pin terminal PCB.
  • 5V: Connected to the + pin of the KY-038.
  • A4 (SDA), A5 (SCL): Connected to the SDA and SCL pins of the APDS-9960 sensor respectively.
  • 3V3: Connected to the VCC pin of the APDS-9960 sensor.

APDS-9960 RGB and Gesture Sensor

  • GND: Connected to the GND pin of the Arduino Nano.
  • VCC: Connected to the 3V3 pin of the Arduino Nano.
  • SDA: Connected to the A4 pin of the Arduino Nano.
  • SCL: Connected to the A5 pin of the Arduino Nano.

KY-038 Sound Sensor Module

  • G: Connected to the GND pin of the Arduino Nano.
  • +: Connected to the 5V pin of the Arduino Nano.
  • D0: Connected to the D2 pin of the Arduino Nano.

4 Channel Relay Module

  • IN 1, IN 2, IN 3, IN 4: Connected to the D4, D5, D6, D7 pins of the Arduino Nano respectively.
  • COM 1, COM 2, COM 3, COM 4: All connected to one terminal of a 2-pin terminal PCB.
  • VCC+: Connected to the same terminal of the 2-pin terminal PCB as COM pins.
  • VCC- (GND): Connected to the other terminal of the 2-pin terminal PCB, which is also connected to the negative terminals of the fans and bulbs.
  • N.O. 1, N.O. 2, N.O. 3, N.O. 4: Connected to the positive terminals of the two fans and two bulbs respectively.

Power Transformer

  • 3 - Secondary: Connected to one terminal of a 2-pin terminal PCB.
  • 4 - Secondary: Connected to the other terminal of the same 2-pin terminal PCB.

40mm Fan 12V (Two Fans)

  • +12V: Each connected to the N.O. 1 and N.O. 2 of the 4-channel relay module respectively.
  • -12V: Both connected to the negative terminal of a 2-pin terminal PCB.

Bulb (Two Bulbs)

  • +: Each connected to the N.O. 3 and N.O. 4 of the 4-channel relay module respectively.
  • -: Both connected to the negative terminal of a 2-pin terminal PCB.

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).