Arduino库
如果您不使用Arduino, 可以看看开发章节
安装
PlatformIO
在PlatformIO中使用本库,在platformio.ini中添加如下内容或者通过UI搜索BULLM_SensorModule添加
ini
[env:myenv]
...
lib_deps =
bullm/BULLM_SensorModule
...Arduino IDE
在Arduino IDE中,直接在库管理器中搜索BULLM_SensorModule并安装即可。
简单示例
c
#include <Arduino.h>
#include <Wire.h>
#include "BULLM_SensorAds1110.h"
BULLM_SensorAds1110 sensor(0x48);
void setup() {
Wire.begin(); // 初始化I2C
Serial.begin(9600);
if (!sensor.begin()) {
Serial.println("BULLM_SensorAds1110 not found");
return;
}
Serial.println("BULLM_SensorAds1110 found");
// 设置配置寄存器
// SC (转换模式) : 0 = 连续转换, 1 = 单次转换
// DR (数据速率) : 0 = 240SPS, 1 = 60SPS, 2 = 30SPS, 3 = 15SPS
// PGA (增益) : 0 = 1x, 1 = 2x, 2 = 4x, 3 = 8x
sensor.setConfig(0, 3, 0);
// 设置是否短接跳线, 默认不短接 (短接后电压范围为0~50V,默认0~26V)
sensor.setMode(false);
}
void loop() {
Serial.print("Voltage -> ");
Serial.println(sensor.getVoltage(), 3);
delay(100);
}实例方法
| 方法 | 用途 | 参数 | 返回值 |
|---|---|---|---|
begin() | 初始化传感器 | 无 | bool: 是否成功 |
setConfig(uint8_t sc, uint8_t dr, uint8_t pga) | 设置配置参数 | sc: 转换模式 (0连续, 1单次)dr: 数据速率 (0=240SPS, 1=60SPS, 2=30SPS, 3=15SPS)pga: 增益 (0=1x, 1=2x, 2=4x, 3=8x) | bool: 是否成功 |
setConfigRaw(uint8_t config) | 写入原始配置寄存器值 | config: 8位配置寄存器数据 | bool: 是否成功 |
setMode(bool isJumper) | 设置电压测量量程(是否短接跳线) | isJumper: true 短接(0~50V), false 不短接(0~26V默认) | 无 |
getVoltage() | 获取当前读取并转换后的电压值 | 无 | float: 电压值(V) |
getVoltageRaw() | 获取芯片读取的原始电压值 | 无 | uint16_t: 原始电压值 |