DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ads1115.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file ads1115.h
6 * @brief TI ADS1115 16-bit ADC codec (DETWS_ENABLE_ADS1115).
7 *
8 * The ADS1115 is a 4-channel 16-bit analog-to-digital converter on the I2C bus with a
9 * programmable-gain amplifier - far more resolution and range control than the ESP32's own ADC.
10 * A reading is a 16-bit config-register write (start, channel, gain, mode, data rate) followed
11 * by a 16-bit read of the conversion register; the signed result scales to a voltage by the
12 * selected gain's full-scale range.
13 *
14 * This codec is pure and host-tested: ::ads1115_config_single builds the config word for a
15 * single-shot single-ended reading, and ::ads1115_raw_to_uv converts the signed sample to
16 * microvolts. On an ESP32 the binding writes the config, waits for the conversion, and reads it
17 * back over I2C (Wire); only that touches hardware.
18 *
19 * A cheap solder-and-bench-test breakout: measure a battery, a potentiometer, or an analog
20 * sensor and bridge the reading onto the network.
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef DETERMINISTICESPASYNCWEBSERVER_ADS1115_H
27#define DETERMINISTICESPASYNCWEBSERVER_ADS1115_H
28
29#include <stdint.h>
30
31#define ADS1115_REG_CONVERSION 0x00 ///< conversion result register
32#define ADS1115_REG_CONFIG 0x01 ///< configuration register
33
34/** @brief Programmable-gain settings (PGA register codes; full-scale +/- range). Config field values
35 * shifted into the config word, so integer constants in a namespacing struct - cast-free. */
37{
38 static constexpr uint8_t ADS1115_GAIN_TWOTHIRDS = 0; ///< +/- 6.144 V
39 static constexpr uint8_t ADS1115_GAIN_1 = 1; ///< +/- 4.096 V
40 static constexpr uint8_t ADS1115_GAIN_2 = 2; ///< +/- 2.048 V (default)
41 static constexpr uint8_t ADS1115_GAIN_4 = 3; ///< +/- 1.024 V
42 static constexpr uint8_t ADS1115_GAIN_8 = 4; ///< +/- 0.512 V
43 static constexpr uint8_t ADS1115_GAIN_16 = 5; ///< +/- 0.256 V
44};
45
46/** @brief Data-rate settings (DR register codes; samples per second). */
48{
49 static constexpr uint8_t ADS1115_DR_8 = 0; ///< 8 SPS
50 static constexpr uint8_t ADS1115_DR_16 = 1; ///< 16 SPS
51 static constexpr uint8_t ADS1115_DR_32 = 2; ///< 32 SPS
52 static constexpr uint8_t ADS1115_DR_64 = 3; ///< 64 SPS
53 static constexpr uint8_t ADS1115_DR_128 = 4; ///< 128 SPS (default)
54 static constexpr uint8_t ADS1115_DR_250 = 5; ///< 250 SPS
55 static constexpr uint8_t ADS1115_DR_475 = 6; ///< 475 SPS
56 static constexpr uint8_t ADS1115_DR_860 = 7; ///< 860 SPS
57};
58
59/**
60 * @brief Build the 16-bit config word for a single-shot, single-ended reading of @p channel
61 * (0..3) at gain @p gain and data rate @p dr (comparator disabled). Out-of-range fields fall
62 * back to channel 0 / gain +/-2.048 V / 128 SPS.
63 */
64uint16_t ads1115_config_single(uint8_t channel, uint8_t gain, uint8_t dr);
65
66/** @brief Convert a signed 16-bit sample to microvolts for @p gain's full-scale range. */
67int32_t ads1115_raw_to_uv(int16_t raw, uint8_t gain);
68
69// --- ESP32 binding (I2C via Wire; no-ops on a host build) ------------------------------------
70
71/** @brief Initialize the I2C bus for the ADS1115 at @p addr. @return true on ESP32. */
72bool ads1115_begin(uint8_t addr);
73
74/** @brief Single-shot read of @p channel (0..3) at @p gain into @p raw. @return false on error. */
75bool ads1115_read_raw(uint8_t channel, uint8_t gain, int16_t *raw);
76
77/** @brief Single-shot read of @p channel at @p gain, converted to microvolts in @p microvolts. */
78bool ads1115_read_uv(uint8_t channel, uint8_t gain, int32_t *microvolts);
79
80#endif // DETERMINISTICESPASYNCWEBSERVER_ADS1115_H
uint16_t ads1115_config_single(uint8_t channel, uint8_t gain, uint8_t dr)
Build the 16-bit config word for a single-shot, single-ended reading of channel (0....
bool ads1115_begin(uint8_t addr)
Initialize the I2C bus for the ADS1115 at addr.
int32_t ads1115_raw_to_uv(int16_t raw, uint8_t gain)
Convert a signed 16-bit sample to microvolts for gain's full-scale range.
bool ads1115_read_raw(uint8_t channel, uint8_t gain, int16_t *raw)
Single-shot read of channel (0..3) at gain into raw.
bool ads1115_read_uv(uint8_t channel, uint8_t gain, int32_t *microvolts)
Single-shot read of channel at gain, converted to microvolts in microvolts.
Data-rate settings (DR register codes; samples per second).
Definition ads1115.h:48
static constexpr uint8_t ADS1115_DR_475
475 SPS
Definition ads1115.h:55
static constexpr uint8_t ADS1115_DR_128
128 SPS (default)
Definition ads1115.h:53
static constexpr uint8_t ADS1115_DR_16
16 SPS
Definition ads1115.h:50
static constexpr uint8_t ADS1115_DR_250
250 SPS
Definition ads1115.h:54
static constexpr uint8_t ADS1115_DR_64
64 SPS
Definition ads1115.h:52
static constexpr uint8_t ADS1115_DR_860
860 SPS
Definition ads1115.h:56
static constexpr uint8_t ADS1115_DR_8
8 SPS
Definition ads1115.h:49
static constexpr uint8_t ADS1115_DR_32
32 SPS
Definition ads1115.h:51
Programmable-gain settings (PGA register codes; full-scale +/- range). Config field values shifted in...
Definition ads1115.h:37
static constexpr uint8_t ADS1115_GAIN_2
+/- 2.048 V (default)
Definition ads1115.h:40
static constexpr uint8_t ADS1115_GAIN_4
+/- 1.024 V
Definition ads1115.h:41
static constexpr uint8_t ADS1115_GAIN_8
+/- 0.512 V
Definition ads1115.h:42
static constexpr uint8_t ADS1115_GAIN_1
+/- 4.096 V
Definition ads1115.h:39
static constexpr uint8_t ADS1115_GAIN_16
+/- 0.256 V
Definition ads1115.h:43
static constexpr uint8_t ADS1115_GAIN_TWOTHIRDS
+/- 6.144 V
Definition ads1115.h:38