Shahzeb
Published © GPL3+

Gesture Control smart fan

When both sensor are high fan will off, If one of them will high fan will rotate to that direction till it max angle.

IntermediateFull instructions provided3 hours99
Gesture Control smart fan

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
RGB LED Module
DIYables RGB LED Module
×1
DC Motor, Miniature
DC Motor, Miniature
You can use motor depend on the size of your fan and weight
×1

Story

Read more

Code

Code for project

Arduino
Make you connection should be proper, and just paste it .
#include <Arduino.h>
#include <Servo.h>
#include <LowPower.h>

#define LEFT_IR A1
#define RIGHT_IR A0
#define SERVO_PIN 9
#define FAN_PIN 11
#define RED_LED 5
#define BLUE_LED 7

Servo fanServo;

// === State ===
bool isLocked = false;
bool systemActive = false;
unsigned long lastDetectTime = 0;
unsigned long lockTimer = 0;
const unsigned long lockDelay = 5000;
int servoAngle = 90;

void setup() {
  pinMode(FAN_PIN, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);

  analogWrite(FAN_PIN, 0);
  digitalWrite(RED_LED, LOW);
  digitalWrite(BLUE_LED, LOW);

  fanServo.attach(SERVO_PIN);
  fanServo.write(servoAngle);

  Serial.begin(9600);
}

void loop() {
  int leftVal = analogRead(LEFT_IR);
  int rightVal = analogRead(RIGHT_IR);
  int avgVal = (leftVal + rightVal) / 2;//smothing

  // Thresholds
  const int detectThresh = 200;  // IR active when > 200
  const int moveThresh = 50;     // To detect hand direction
  const int steadyThresh = 40;   // For stability detection

  bool handNearLeft = leftVal > detectThresh;
  bool handNearRight = rightVal > detectThresh;
  bool handDetected = handNearLeft || handNearRight;

  // Sleep if nothing detected and not locked
  if (!handDetected && !isLocked) {
    systemActive = false;
    digitalWrite(RED_LED, LOW);
    digitalWrite(FAN_PIN, LOW);
    digitalWrite(BLUE_LED, LOW);
    LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
    return;
  }

  // === Active Mode ===
  analogWrite(FAN_PIN,168);
  systemActive = true;

  if (!isLocked) {
    digitalWrite(RED_LED, HIGH);
    digitalWrite(BLUE_LED, LOW);

    // Servo movement
    int diff = leftVal - rightVal;
    if (abs(diff) > moveThresh) {
      servoAngle += (diff > 0) ? -2 : 2;
      servoAngle = constrain(servoAngle, 60, 120);
      fanServo.write(servoAngle);
    }

    // Check for lock condition
    if ((handNearLeft && !handNearRight && abs(leftVal - avgVal) < steadyThresh) ||
        (handNearRight && !handNearLeft && abs(rightVal - avgVal) < steadyThresh)) {
      if (millis() - lockTimer > lockDelay) {
        isLocked = true;
        lockTimer = millis();  // Reset for unlock timer
      }
    } else {
      lockTimer = millis();
    }
  } else {
    // === LOCKED STATE ===
    digitalWrite(RED_LED, HIGH);
    fanServo.write(servoAngle);  // Keep angle fixed

    // Show blue if hand moved away
    if (!handDetected) {
      digitalWrite(BLUE_LED, HIGH);
    }

    // Unlock if hand steady on both IRs
    if (handNearLeft && handNearRight && abs(leftVal - rightVal) < steadyThresh) {
      if (millis() - lockTimer > lockDelay) {
        isLocked = false;
        digitalWrite(BLUE_LED, LOW);
      }
    } else {
      lockTimer = millis();
    }
  }

  delay(100); // Small debounce
}

Credits

Shahzeb
1 project • 1 follower
Pursuing B.Tech in Electronics and Communication, I am passionate about embedded systems. I have also completed several projects.

Comments