Example Code for Arduino - Custom Posture Recognition
Last revision 2026/03/05
Drive posture sensor via I2C, output posture info including ID and confidence over serial. It's like connecting a smart camera to your computer to recognize and label different body positions. Use the Gravity: AI Posture & Gesture Sensor with FireBeetle 2 ESP32-E, connect VCC to 5V, GND to GND, C to 22/SCL, D to 21/SDA. Initialize the sensor, set to human posture detection mode, and read results in a loop.
Hardware Preparation
- Gravity: AI Posture & Gesture Sensor (SKU: SEN0670) ×1
- FireBeetle 2 ESP32‑E (SKU: DFR0654)
- USB Type‑C cable ×1
- PC ×1
Software Preparation
- Programming environment: Click to download Arduino IDE
- Download and install the sensor library: DFRobot_HumanPose
- Please click here to view: How to install a library?
Wiring Diagram
I²C Connection (The following examples all use I²C communication)

Connection Description:
| Sensor Pin: VCC | Connect to | Main Controller Pin: 5V |
|---|---|---|
| Sensor Pin: GND | Connect to | Main Controller Pin: GND |
| Sensor Pin: C | Connect to | Main Controller Pin: 22/SCL |
| Sensor Pin: D | Connect to | Main Controller Pin: 21/SDA |
UART Connection

Connection Description:
| Sensor Pin: VCC | Connect to | Main Controller Pin: 5V |
|---|---|---|
| Sensor Pin: GND | Connect to | Main Controller Pin: GND |
| Sensor Pin: R | Connect to | Main Controller Pin: 26/D3 |
| Sensor Pin: T | Connect to | Main Controller Pin: 25/D2 |
Note:
- The default communication method of the sensor is I²C. If you need to switch to UART, use the on‑board DIP switch to change the communication method.
- After switching, the sensor must be powered off and on again for the change to take effect.
Sample Code
Before starting this example, make sure the sensor has already learned custom postures using the host computer software. If no postures have been learned beforehand, the sensor will recognise them as unknown postures.
Function: Drive posture sensor via I2C, output posture info including ID and confidence over serial.
#include <DFRobot_HumanPose.h>
const uint8_t I2C_ADDR = 0x3A; // I2C communication by default
DFRobot_HumanPose_I2C humanPose(&Wire, I2C_ADDR);
void setup()
{
Serial.begin(115200);
// 1. Initialise the sensor
while (!humanPose.begin()) {
Serial.println("Sensor initialisation failed, please check the wiring!");
delay(1000);
}
Serial.println("Sensor initialisation successful!");
// 2. Set to "human posture detection" mode
humanPose.setModelType(DFRobot_HumanPose::ePose);
}
void loop()
{
// 3. Get the detection result
if (humanPose.getResult() == DFRobot_HumanPose::eOK) {
// Iterate through all detected targets
while (humanPose.availableResult()) {
Result *result = humanPose.popResult();
if (result) {
// Print prefix uniformly
if (result->id == 0) {
Serial.print("[Unknown posture/person]");
} else {
Serial.print("[Learned posture]");
}
// Print ID and name uniformly
Serial.print(" ID: ");
Serial.print(result->id);
Serial.print(" | Name: ");
if (result->id == 0) {
Serial.println("unknown"); // Show "unknown" for unlearned
} else {
Serial.println(result->name); // Show actual name for learned
}
// Print common information: confidence
Serial.print("Confidence: ");
Serial.println(result->score);
Serial.println("------------------------");
}
}
}
delay(100); // 100ms delay to avoid spamming the serial monitor
}
Result
The sensor automatically recognises postures and can distinguish between learned and unlearned postures.

Was this article helpful?
