mirror of
https://github.com/esphome/esphome.git
synced 2025-09-03 11:52:20 +01:00
add ADC support
This commit is contained in:
@@ -193,4 +193,15 @@ def validate_adc_pin(value):
|
|||||||
{CONF_ANALOG: True, CONF_INPUT: True}, internal=True
|
{CONF_ANALOG: True, CONF_INPUT: True}, internal=True
|
||||||
)(value)
|
)(value)
|
||||||
|
|
||||||
|
if CORE.is_nrf52:
|
||||||
|
conf = pins.gpio_pin_schema(
|
||||||
|
{CONF_ANALOG: True, CONF_INPUT: True}, internal=True
|
||||||
|
)(value)
|
||||||
|
number = conf[CONF_NUMBER]
|
||||||
|
if number not in (2, 3, 4, 5, 28, 29, 30, 31):
|
||||||
|
raise cv.Invalid(
|
||||||
|
"nRF52840: Only pins 2, 3, 4, 5, 28, 29, 30 and 31 support ADC"
|
||||||
|
)
|
||||||
|
return conf
|
||||||
|
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@@ -294,6 +294,23 @@ float ADCSensor::sample() {
|
|||||||
}
|
}
|
||||||
#endif // USE_LIBRETINY
|
#endif // USE_LIBRETINY
|
||||||
|
|
||||||
|
#ifdef USE_NRF52
|
||||||
|
float ADCSensor::sample() {
|
||||||
|
// https://infocenter.nordicsemi.com/pdf/nRF52840_PS_v1.1.pdf
|
||||||
|
// 6.23.2 Reference voltage and gain settings
|
||||||
|
// Default settings are internal reference with 1/6 gain (GND..3.6V ADC range)
|
||||||
|
// With internal reference, single-ended input (grounded negative input) and a gain of 1/6, the input range will be:
|
||||||
|
// Input range = (0.6 V)/(1/6) = 3.6 V
|
||||||
|
|
||||||
|
uint32_t raw = analogRead(this->pin_->get_pin()); // NOLINT
|
||||||
|
if(output_raw_) {
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
// default 10-bit resolution
|
||||||
|
return raw * 3.6f / 1024.0f;
|
||||||
|
}
|
||||||
|
#endif // USE_NRF52
|
||||||
|
|
||||||
#ifdef USE_ESP8266
|
#ifdef USE_ESP8266
|
||||||
std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
|
std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
|
||||||
#endif
|
#endif
|
||||||
|
@@ -7,6 +7,7 @@ from esphome.const import (
|
|||||||
CONF_MODE,
|
CONF_MODE,
|
||||||
CONF_INVERTED,
|
CONF_INVERTED,
|
||||||
CONF_NUMBER,
|
CONF_NUMBER,
|
||||||
|
CONF_ANALOG,
|
||||||
)
|
)
|
||||||
|
|
||||||
nrf52_ns = cg.esphome_ns.namespace("nrf52")
|
nrf52_ns = cg.esphome_ns.namespace("nrf52")
|
||||||
@@ -44,6 +45,7 @@ NRF52_PIN_SCHEMA = cv.All(
|
|||||||
pins.gpio_base_schema(
|
pins.gpio_base_schema(
|
||||||
NRF52GPIOPin,
|
NRF52GPIOPin,
|
||||||
validate_gpio_pin,
|
validate_gpio_pin,
|
||||||
|
modes=pins.GPIO_STANDARD_MODES + (CONF_ANALOG,),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -34,7 +34,12 @@ sensor:
|
|||||||
pin: P0.10
|
pin: P0.10
|
||||||
id: gpio_10
|
id: gpio_10
|
||||||
update_interval: 3s
|
update_interval: 3s
|
||||||
|
- platform: adc
|
||||||
|
pin: P0.29
|
||||||
|
id: adc4
|
||||||
|
update_interval: 1s
|
||||||
|
|
||||||
dfu:
|
dfu:
|
||||||
|
|
||||||
beacon:
|
beacon:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user