Source: https://mokxi.com/libraries/adafruit-sensor
Updated: 2026-11-17

Libraries / Sensors

Arduino library · Sensors

# Adafruit Unified Sensor: the event every Adafruit driver fills

Yes. You can use the Adafruit Unified Sensor library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "MPU6050 servo stabilizer", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <Adafruit_Sensor.h> and it builds, with nothing to install. Free, and no account needed.

## What Adafruit Unified Sensor does

Adafruit Unified Sensor drives no chip of its own. It is a shared vocabulary that many Adafruit libraries build on: a struct called sensors_event_t that holds one reading, a struct called sensor_t that describes a sensor, and a base class, Adafruit_Sensor, with getEvent() and getSensor(). An accelerometer, a thermometer and a humidity sensor all hand back the same kind of event, so code that logs or prints readings does not have to care which chip made them.

The event is a union. acceleration, gyro, temperature, relative_humidity, pressure and light share the same few bytes, and the field to read is the one that matches event.type. The units come from the convention, not the chip: meters per second squared for acceleration, degrees Celsius, percent, hectopascals. Constants such as SENSORS_GRAVITY_EARTH (9.80665) help convert to other units.

Plenty of tutorials put #include <Adafruit_Sensor.h> above DHT.h or Adafruit_MPU6050.h out of habit. On a real board it has to be installed, because those libraries list it as a dependency. In Mokxi it is built in, and the drivers that use it include it themselves. The example below is the servo stabilizer, which reads an MPU6050 into three events per pass, one each for acceleration, rotation and temperature.

Library Manager name | Adafruit Unified Sensor

Author | Adafruit

Include | #include <Adafruit_Sensor.h>

Boards | every board Mokxi compiles for

In the simulator | Simulated: it runs against the part the way it does on the bench.

## Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <Adafruit_Sensor.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit Unified Sensor" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit Unified Sensor to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.15, so a sketch written here builds there too.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### sensors_event_t a, g, temp

Declares the reading structs. getEvent() clears each one, stamps its type, sensor ID and millis(), then writes the value into the field for that kind of sensor.

### mpu.getEvent(&a, &g, &temp)

A driver with several readings fills several events in one call. Single-reading drivers take one pointer, such as accel.getEvent(&event), and return false when the chip did not answer.

### a.acceleration.x

The x axis of an accelerometer event in m/s^2, with .y and .z beside it. A gyroscope event uses gyro.x, gyro.y and gyro.z instead.

### temp.temperature

Degrees Celsius from a temperature event. relative_humidity, pressure and light are the other single-value fields; read only the one that matches the sensor that filled the event.

### accel.getSensor(&sensor)

Fills a sensor_t with the chip’s name, its minimum and maximum values, its resolution and the ID given to the constructor.

### accel.printSensorDetails()

Prints that same description to Serial as a block of lines, a quick check in setup() that the expected driver is the one talking.

### event.type == SENSOR_TYPE_ACCELEROMETER

Tells which kind of reading an event carries, one of the SENSOR_TYPE_ numbers, useful in code that receives events from more than one driver.

## Example: MPU6050 servo stabilizer

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · MPU6050 servo stabilizer: the project page

## Common errors and how to fix them

### fatal error: Adafruit_Sensor.h: No such file or directory

On hardware the header comes from its own package. Open the Library Manager, search for Adafruit Unified Sensor and install it next to the driver. Mokxi ships it already, so the include resolves there with nothing to add.

### A temperature prints as 9.81, or some other number that belongs to a different sensor

Because every field shares the same memory, reading temperature from an accelerometer event returns the bytes of acceleration.x. Keep one event variable per reading and read the field that matches it.

### error: 'sensors_event_t' does not name a type

The struct is used above the line that brings it in. Put the driver’s include, or Adafruit_Sensor.h itself, at the top of the sketch before any event is declared.

### cannot declare variable 'sensor' to be of abstract type 'Adafruit_Sensor'

Adafruit_Sensor is only the base class and cannot be made on its own. Create the driver object, for example Adafruit_ADXL345_Unified, and use Adafruit_Sensor pointers only for what calls such as getTemperatureSensor() return.

## Questions and answers

### Does a DHT11 sketch need Adafruit_Sensor.h?

In Mokxi, DHT.h uses nothing from it, so the include is harmless and optional. For a real board install it anyway, since the DHT library depends on it and its unified example uses the events.

### Why does acceleration come in m/s^2 rather than g?

The unified convention fixes SI units for every driver. Divide by SENSORS_GRAVITY_EARTH to get g, as the stabilizer does before working out its angles.

## Projects that use Adafruit Unified Sensor

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Tilt level An ADXL345 accelerometer read with the Adafruit library: the three axes in m/s^2 and a spirit level that says which way to tip. HMC5883L compass heading An HMC5883L on a GY-271 board reads the Earth’s field, and the serial monitor prints the heading and its point of the compass twice a second. ESP32 async web server: BME280, slider and relay ESPAsyncWebServer serves a page with BME280 temperature, humidity and pressure, a slider that dims an LED and a button that switches a relay. Hand gesture controlled car Tilt an MPU6050 on your hand and a 2WD car follows over nRF24L01 radios: forward, reverse, left, right, and it stops when the signal is lost.

## Keep going

### Learn

- Arduino MPU6050: Accelerometer and Tilt
- Arduino ADXL345: Library, Example Code and Tilt

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280
- arduino-sht
- Sensirion I2C SHT3x
- SHT4x
- forcedBMX280
- BlueDot BME280 Library
- Grove - Barometer Sensor BME280

Every built-in library · Open the editor

## Try Adafruit Unified Sensor without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
