Smart Fan

Smart fan with adaptive speed control and intelligent environment sensing

Smart Fan
2024-09-02
View on GitHub
Computer VisionLabVIEWRaspberry Pi
Smart Fan System

This project introduces a Smart Fan that intelligently adapts its rotation and speed based on real-time environmental data and user presence. The system leverages face detection and distance measurement to optimize airflow, enhancing user comfort while conserving energy.

My Contribution

I was the project lead in this measurements and instrumentation project. I developed the custom sensor application to adjust fan speed and rotation angle integrating face detection for distance estimation with servo motor control. This involved capturing video, detecting faces, calculating distances, and controlling the servo motor based on the detected face positions.

Project Objectives

The main objectives of this project are:

  • Build a smart system using actuators and sensors (one custom) and integrate them using LabVIEW and NI-DAQmx.
  • Regulate power using variable DC power supply and control with Rigol Ultra Sigma.

Features

Real-Time Distance Estimation

The system uses a Mediapipe model for face detection, and OpenCV for processing the video feed from the webcamera connected to the Raspberry Pi.

  • Face Detection: The algorithm detects faces in real-time, drawing bounding boxes around them.
  • Distance Measurement: The area of the bounding box is used to estimate the distance to the face and angles.

Here is a brief snippet of the core logic for face detection and servo control:

face_centers = obj_data(frame)
  # Get face positions
  face_centers = obj_data(frame)

  if face_centers:
      if only one face detected:
          face_x = x-coordinate of face
          angle = map face_x to servo angle [-80, 80]

          farthest_face = face with smallest depth
          distance_m = pixels_to_meters(farthest_face.depth)

          # Set fan speed based on distance
          if distance_m < 1:      set_pwm_distance(3)
          elif distance_m < 2:    set_pwm_distance(15)
          else:                   set_pwm_distance(26)

          display distance text on frame

          # Set servo angle range
          if angle < 30:      setAngle(30)
          elif angle < 60:    setAngle(60)
          else:               setAngle(80)

      else:
          # Multiple faces: draw lines between each pair
          for each pair (i, j) in face_centers:
              draw line between face i and face j

          leftmost  = face with min x
          rightmost = face with max x
          horizontal_distance = distance(leftmost, rightmost)

          farthest_face = face with smallest depth
          distance_m = pixels_to_meters(farthest_face.depth)

          # Set fan speed based on distance
          if distance_m < 1:      set_pwm_distance(3)
          elif distance_m < 2:    set_pwm_distance(15)
          else:                   set_pwm_distance(26)

          display distance and left-right distance on frame

          if horizontal_distance > 0:
              face_range = rightmost.x - leftmost.x
              angle = map face_range to servo angle

              if angle < 30:      setAngle(30)
              elif angle < 60:    setAngle(60)
              else:               setAngle(80)

              display angle text on frame
  else:
      # No faces detected
      print "No face detected"
      stop fan and center servo

Servo Motor Control

Based on the detected faces' positions, the system adjusts the angle of a servo motor using RPi.GPIO.

  • Angle Calculation: The horizontal position of the faces is used to calculate the appropriate angle for the servo.
  • PWM Control: The Raspberry Pi uses PWM at 50Hz to control the servo motor, aligning it with the detected faces.

System Workflow

The block diagram below illustrates how the components of the Smart Table Fan system interact:

Overall workflow of the Smart Fan system

Overall workflow of the Smart Fan system.

Overall setup of the Smart Fan system

Overall setup of the Smart Fan system.

  • Raspberry Pi: Processes video input from the webcam for face detection using OpenCV and MediaPipe, calculates distance, and controls the fan's rotation angle.
  • Webcam: Captures real-time video to detect user presence and measure distance.
  • LabVIEW: Integrates data from sensors and controls the system, managing fan speed and rotation.
  • Servo Motor MG-995: Adjusts the fan's direction based on the user's position from the Raspberry Pi.
  • DC Fan: Modulates fan speed according to PWM signals sent from the Raspberry Pi to power supply controlled by LabVIEW.
  • NI DAQ mx: Acquires data from various sensors and actuators, providing real-time feedback to the LabVIEW interface.
  • Rigol Variable DC Power Supply: Modifies fan speed based on control signals from LabVIEW.
  • LM35 Temperature Sensor: Measures ambient temperature.
  • AMT1001 Humidity Sensor: Monitors humidity levels in the environment.

Calibration Curves

To ensure the system responds accurately to environmental changes, calibration curves were developed for distance, temperature, and humidity.

Distance Calibration Curve

Calibration curve for distance measurement, showing the relationship between detected face size and distance.

Temperature Calibration Curve

Temperature calibration curve used to adjust fan speed based on ambient temperature readings.

Humidity Calibration Curve

Humidity calibration curve for optimizing fan operation under varying humidity conditions.

How It Works

  1. Face Detection: The system captures frames from the video feed and processes them to detect faces. It can consider up to four faces, limiting the processing load and focusing on the most relevant data.
  2. Distance Calculation: For each detected face, the system calculates the area of the bounding box. The area is then converted into a distance measurement, estimating how far the face is from the camera.
  3. Servo Angle Adjustment: Based on the horizontal positions of the detected faces, the system calculates an appropriate angle for the servo motor. The servo motor adjusts to this angle, potentially aligning with the direction of the detected faces.
  4. PWM Output: The system also adjusts the PWM signal output based on the distance to the farthest face. This output could be used for further control or feedback mechanisms.

Conclusion

The project demonstrates a practical application of computer vision with physical hardware, effectively detecting faces and controlling a servo motor.

For more details, you can explore the GitHub repository.