Example Code for Arduino-Face-Tracking Fan
This project creates a smart, face-tracking fan using the Gravity AI Sensor. It turns on/off with hand gestures ("OK" to start, "Open Palm" to stop) and automatically follows your face's horizontal position in real-time, keeping you cool without any physical contact.
Hardware Preparation
- DFR0216 DFRduino UNO R3 ×1
- DFR0265 Gravity: IO Sensor Expansion Board V7.1 ×1
- DFR0411 Gravity: 130 DC Motor Fan ×1
- SER0056 2Kg 300° Clutch Servo ×1
- SEN0626 AI Visual Gesture/Face Tracking Sensor ×1
Software Preparation
- Arduino IDE Click to download Arduino IDE
- Download and install DFRobot_GestureFaceDetection
- Download and install DFRobot_RTU
- How to install libraries
Wiring Diagram

Other Preparation Work
- Step 1: Connect the sensor, DFRduino UNO R3 controller, IO sensor expansion board, fan, and servo according to the wiring diagram, and switch the sensor's communication mode switch to the I2C side.
- Step 2: Open Arduino IDE, copy the following code and upload it to the DFRduino UNO R3.
Sample Code
#include <Wire.h>
#include <Servo.h>
#include "DFRobot_GestureFaceDetection.h"
#define DEVICE_ID 0x72
#define FAN_PIN 3
#define SERVO_PIN 5
Servo myservo;
DFRobot_GestureFaceDetection_I2C gfd(DEVICE_ID);
void setup(){
myservo.attach(SERVO_PIN);
myservo.write(0);
gfd.begin(&Wire);
Serial.begin(115200);
gfd.setFaceDetectThres(60);
gfd.setGestureDetectThres(60);
gfd.setDetectThres(100);
pinMode(FAN_PIN, OUTPUT);
}
void loop(){
//Serial.println(gfd.getGestureType());
if(gfd.getFaceNumber() > 0){
uint16_t gestureType = gfd.getGestureType();
if(gestureType == 2){
analogWrite(FAN_PIN, 255);
}
else if(gestureType == 3){
analogWrite(FAN_PIN, 0);
}
uint16_t x = gfd.getFaceLocationX();
Serial.println(x);
myservo.write(map(x,0,620,10,170));
Serial.println(gfd.getGestureType());
}
}
Result
When the sensor detects a face (area above the shoulders), the system activates the following linked effects:
- Gesture-Controlled Fan
- Making the "OK" gesture (corresponding to gesture type 2): The fan turns on immediately at maximum power without the need to touch a switch;
- Making the "Number 5" gesture (corresponding to gesture type 3): The fan automatically turns off, achieving contactless start-stop control.
- Real-Time Face Position Tracking: The servo motor will drive the fan, adjusting the angle in real time according to the X-coordinate of the face in the camera's field of view (range 0-620):
- When the face moves left (X value decreases), the servo angle decreases synchronously (range 10°-170°), and the fan turns left;
- When the face moves right (X value increases), the servo angle increases synchronously, and the fan turns right; the entire process is smooth and continuous, achieving the precise following effect of "wherever the person goes, the fan follows".
- Debugging Feedback: The serial monitor outputs the X-coordinate of the face and the currently recognized gesture type in real time (e.g., "300", "2"), facilitating observation of the device's operating status and debugging optimization.
Note: Please check the tracking fan structure file in more downloads.
Was this article helpful?
