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

Arduino Nano Controlled Smart Home System with Gesture and Sound Activation

Image of Arduino Nano Controlled Smart Home System with Gesture and Sound Activation

Circuit Documentation

Summary

This circuit is designed to interface an Arduino Nano with an APDS-9960 RGB and Gesture Sensor, a KY-038 sound sensor module, a 4-channel relay module, and a power transformer through various terminal PCBs. The Arduino Nano serves as the central processing unit, reading gestures from the APDS-9960 sensor and sound signals from the KY-038 to control the state of the relays. The relays are used to switch loads on and off, and the power transformer is included to step down or step up voltage as required for the circuit.

Component List

Arduino Nano

  • Microcontroller board based on the ATmega328P
  • Pins: D1/TX, D0/RX, RESET, GND, D2 to D13, VIN, 5V, A0 to A7, AREF, 3V3
  • Used for controlling the circuit logic and interfacing with sensors and relay module.

APDS-9960 RGB and Gesture Sensor

  • A sensor that detects gesture, color, ambient light, and proximity.
  • Pins: VL, GND, VCC, SDA, SCL, INT
  • Connected to the Arduino Nano for gesture input.

KY-038 Sound Sensor Module

  • A module that detects sound levels with both analog and digital outputs.
  • Pins: +, G, A0, D0
  • Connected to the Arduino Nano for sound detection.

4 Channel Relay Module

  • A module with 4 relays to control high power loads.
  • Pins: N.O. 1-4, COM 1-4, N.C. 1-4, IN 1-4, VCC+, VCC- (GND)
  • Controlled by the Arduino Nano to switch loads.

Power Transformer

  • A component that transforms electrical voltage from one level to another.
  • Pins: 1 - Primary, 2 - Primary, 3 - Secondary, 4 - Secondary
  • Used to step down or step up voltage.

Terminal PCB 2 Pin (x3)

  • A simple two-pin terminal block for making electrical connections.
  • Pins: Pin A, Pin B
  • Used for connecting power and relay module inputs.

Wiring Details

Arduino Nano

  • GND connected to APDS-9960 GND, KY-038 G, and Terminal PCB 2 Pin B (VIN connection).
  • D2 connected to KY-038 D0.
  • D4, D5, D6, D7 connected to 4 channel relay module IN 1, IN 2, IN 3, IN 4 respectively.
  • VIN connected to Terminal PCB 2 Pin A (power input).
  • 5V connected to KY-038 +.
  • A5 connected to APDS-9960 SCL.
  • A4 connected to APDS-9960 SDA.
  • 3V3 connected to APDS-9960 VCC.

APDS-9960 RGB and Gesture Sensor

  • GND, SCL, SDA, VCC connected to corresponding pins on the Arduino Nano.

KY-038 Sound Sensor Module

  • +, G, D0 connected to corresponding pins on the Arduino Nano.

4 Channel Relay Module

  • IN 1, IN 2, IN 3, IN 4 connected to corresponding pins on the Arduino Nano.
  • VCC+ connected to Terminal PCB 2 Pin A (power input).
  • VCC- (GND) connected to Terminal PCB 2 Pin B (ground).

Power Transformer

  • 3 - Secondary, 4 - Secondary connected to corresponding pins on Terminal PCB 2 Pin A and B.

Terminal PCB 2 Pin

  • Used for power connections and relay module inputs.

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 controls the circuit based on gesture inputs from the APDS-9960 sensor and sound detection from the KY-038 module. It toggles the state of the relays based on these inputs, with specific gestures and clap patterns mapped to different relay channels.