SensorHub
A core communication and data processing hub for I2C-based sensors. It provides essential I2C read and write operations, making it easier to integrate various sensor classes for data acquisition and processing.
Install / Use
/learn @styropyr0/SensorHubREADME
SensorHub Library
Overview
SensorHub is a core communication and data processing hub for I2C-based sensors. It provides essential I2C read and write operations, making it easier to integrate various sensor classes for data acquisition and processing. The library supports multi-byte transactions, configurable communication modes, and device connectivity verification. Additionally, it includes I2S support for ADC-based sensors on platforms that support it.
Features
-
I2C Communication Support
- Read and write operations for single and multi-byte transactions.
- Support for big-endian and little-endian data formats.
- Configurable I2C communication mode.
- Device connectivity verification.
-
I2S Communication Support (Only for SOCs that support ADC over I2S)
- Configurable word select (WS), bit clock (BCLK), and data pins.
- Supports stereo, left, and right channel selection.
- Customizable sampling rate.
Compatibility
The SensorHub library is compatible with:
- ESP32 (Supports both I2C and I2S communication)
- Arduino Due (Supports only I2C communication)
- Other boards with I2C support
Installation
- Download the repository or clone it:
git clone https://github.com/yourusername/SensorHub.git - Copy the
SensorHubfolder into your Arduino libraries directory:Documents/Arduino/libraries/ - Restart the Arduino IDE.
- Include the library in your code:
#include <SensorHub.h>
Usage
1. Initialize an I2C Sensor
To use an I2C-based sensor, initialize the SensorHub object with the sensor's I2C address:
#include <SensorHub.h>
SensorHub sensor(0x40); // Example I2C address
void setup() {
Serial.begin(115200);
if (sensor.is_sensor_connected()) {
Serial.println("Sensor connected!");
} else {
Serial.println("Sensor not found.");
}
}
void loop() {
// Add sensor reading logic here
}
2. Writing Data to an I2C Register
sensor.i2c_execute(0x01, 0xFF); // Write 0xFF to register 0x01
3. Reading Data from an I2C Register
uint8_t data;
sensor.i2c_readByte(0x02, &data, 1);
Serial.print("Register Value: ");
Serial.println(data, HEX);
4. Reading Multi-Byte Data (Little-Endian)
uint16_t value;
sensor.i2c_read_Xbit_LE(0x03, &value, 16);
Serial.println(value);
5. Using I2S-Based Sensors (ESP32 Only)
To initialize an I2S-based sensor:
#ifdef ARDUINO_ARCH_ESP32
SensorHub i2sSensor(25, 26, 27, 16000, I2S_CHANNEL_LEFT);
#endif
API Reference
Constructor
I2C Sensor Initialization
SensorHub(uint8_t addr);
addr: I2C address of the sensor.
I2S Sensor Initialization (ESP32 Only)
SensorHub(uint8_t ws, uint8_t bclock, uint8_t data, uint32_t samplingRate, uint8_t channel);
ws: Word select pin.bclock: Bit clock pin.data: Data pin.samplingRate: Desired sampling rate.channel:I2S_CHANNEL_LEFT,I2S_CHANNEL_RIGHT, orI2S_CHANNEL_STEREO.
I2C Methods
Write Operations
void i2c_execute(uint8_t reg, uint8_t data);
void i2c_execute_16bit(uint8_t reg, uint16_t data);
- Writes data to the specified register.
Read Operations
bool i2c_readByte(uint8_t reg, uint8_t *data, uint8_t length);
bool i2c_readByte(uint8_t reg, int8_t *data, uint8_t length);
- Reads one or more bytes from the given register.
Multi-Byte Read (Little-Endian & Big-Endian)
template <typename T>
bool i2c_read_Xbit_LE(uint8_t reg, T *data, uint8_t length);
template <typename T>
bool i2c_read_Xbit(uint8_t reg, T *data, uint8_t length);
- Reads a multi-bit value from the register.
I2C Configuration
void set_i2c_mode(uint8_t mode);
- Sets the I2C communication mode.
Sensor Connectivity Check
bool is_sensor_connected();
- Returns
trueif the sensor is detected over I2C.
License
This library is open-source and available under the MIT License.
Author
Developed by Saurav Sajeev.
Related Skills
node-connect
350.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
