SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analog_reader.h
Go to the documentation of this file.
1#ifndef _analog_reader_H_
2#define _analog_reader_H_
3
4#include "Arduino.h"
5#include "sensesp.h"
6#if defined(ESP32)
7#include "esp_adc_cal.h"
8#endif
9
10namespace sensesp {
11
16 private:
17 int output_scale_;
18
19 public:
20 virtual bool configure() = 0;
21 virtual float read() = 0;
22};
23
25 private:
26 int pin_;
27 adc_atten_t attenuation_ = ADC_ATTEN_DB_11;
28 // This should work with ESP32 and newer variants, ADCs are different
30 // maximum voltage readout for 3.3V VDDA when attenuation_ is set to 11 dB
31 const float kVmax_ = 3300;
32 int8_t adc_channel_;
33 esp_adc_cal_characteristics_t adc_characteristics_;
34 const int kVref_ = 1100; // voltage reference, in mV
35
36 public:
37 ESP32AnalogReader(int pin) : pin_{pin} {
38 if (!(32 <= pin && pin <= 39)) {
39 debugE("Only ADC1 is supported at the moment");
40 adc_channel_ = -1;
41 return;
42 }
43 adc_channel_ = digitalPinToAnalogChannel(pin);
44 }
45
46 bool configure() {
47 if (adc_channel_ == -1) {
48 return false;
49 }
50 adc1_config_width(bit_width_);
51 adc1_config_channel_atten((adc1_channel_t)adc_channel_, attenuation_);
52 esp_adc_cal_characterize(ADC_UNIT_1, attenuation_, bit_width_, kVref_,
53 &adc_characteristics_);
54 return true;
55 }
56
57 float read() {
59 esp_adc_cal_get_voltage((adc_channel_t)adc_channel_, &adc_characteristics_,
60 &voltage);
61 return voltage / kVmax_;
62 }
63};
65
66} // namespace sensesp
67
68#endif
Used by AnalogInput as a hardware abstraction layer.
virtual float read()=0
virtual bool configure()=0
Construct a new transform based on a single function.
#define debugE(fmt,...)
Definition local_debug.h:50
ESP32AnalogReader AnalogReader