Example Code for Arduino-Reading Data via I2C

Reading Data via I2C

Hardware Preparation

Software Preparation

Wiring Diagram

 Wiring Diagram

Other Preparation Work

  • Step 1: Connect the sensor to the ESP32-E controller according to the wiring diagram above, and switch the communication mode selection switch on the sensor to the I2C side.
  • Step 2: Open Arduino IDE, copy the following code and upload it to the ESP32-E.
  • Step 3: Open the serial monitor in Arduino IDE, set the baud rate to 115200, and observe the serial print results.

Sample Code

#include "DFRobot_GestureFaceDetection.h"
#define DEVICE_ID  0x72 

DFRobot_GestureFaceDetection_I2C gfd(DEVICE_ID);
char str[100];

void setup(){
    gfd.begin(&Wire);
    Serial.begin(115200);
    gfd.setFaceDetectThres(60);
    gfd.setGestureDetectThres(60);        
    gfd.setDetectThres(100);
}

void loop(){
    if(gfd.getFaceNumber() > 0){
        uint16_t faceScore = gfd.getFaceScore();
        uint16_t faceX = gfd.getFaceLocationX();
        uint16_t faceY = gfd.getFaceLocationY();
        sprintf(str, "detect face at (x = %d, y = %d, score = %d)\n", faceX, faceY, faceScore);
        Serial.print(str); 
        uint16_t gestureType = gfd.getGestureType();
        uint16_t gestureScore = gfd.getGestureScore();
        sprintf(str, "detect gesture %d, score = %d\n", gestureType, gestureScore);
        Serial.print(str);
    }
    delay(1500);
}

Result

Output content of the serial monitor:

Result

Additional Information

Interpretation:

  1. First line: Face detection data
    • x = 291, y = 330: Indicates the position coordinates of the detected face (area above the shoulders) in the camera's field of view (range 0-640; the larger the value, the closer to the right/bottom of the field of view);
    • score = 74: Face recognition score (0-100), with a higher score indicating more accurate recognition (a score ≥60 is considered valid).
  2. Second line: Gesture detection data
    • gesture 2: Indicates that the recognized gesture type is "OK" (corresponding to program value 2, refer to the gesture comparison table in "Detailed Explanation of Core Functions");
    • score = 91: Gesture recognition score (0-100), with a higher score indicating a more standard gesture (a score ≥60 is considered valid). At this time, the gesture indicator light will synchronously light up green (corresponding to the "OK" gesture).

Note:

The above two lines of information will only be output when the sensor detects both a face (area above the shoulders) and a gesture simultaneously; if no face is detected, there will be no output from the serial port.

Was this article helpful?

TOP