Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: in adc example, use DB_11 for ESP-IDF v4 and DB_12 for ESP-IDF v5
  • Loading branch information
j-g00da committed Mar 4, 2025
commit bab39a42b5eccdc09631d6854c9cd77f52c65e7a
11 changes: 8 additions & 3 deletions examples/adc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! ADC example, reading a value form a pin and printing it on the terminal
//!

use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_sys::{self as _, adc_atten_t}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported

use std::thread;
use std::time::Duration;
Expand All @@ -20,14 +20,19 @@ fn main() -> anyhow::Result<()> {
#[cfg(esp32)]
let mut adc = AdcDriver::new(peripherals.adc2, &Config::new().calibration(true))?;

#[cfg(esp_idf_version_major = "4")]
const ATTENUATION: adc_atten_t = attenuation::DB_11;
#[cfg(not(esp_idf_version_major = "4"))]
const ATTENUATION: adc_atten_t = attenuation::DB_12;

// configuring pin to analog read, you can regulate the adc input voltage range depending on your need
// for this example we use the attenuation of 11db which sets the input voltage range to around 0-3.6V
#[cfg(not(esp32))]
let mut adc_pin: esp_idf_hal::adc::AdcChannelDriver<{ attenuation::DB_12 }, _> =
let mut adc_pin: esp_idf_hal::adc::AdcChannelDriver<{ ATTENUATION }, _> =
AdcChannelDriver::new(peripherals.pins.gpio4)?;

#[cfg(esp32)]
let mut adc_pin: esp_idf_hal::adc::AdcChannelDriver<{ attenuation::DB_12 }, _> =
let mut adc_pin: esp_idf_hal::adc::AdcChannelDriver<{ ATTENUATION }, _> =
AdcChannelDriver::new(peripherals.pins.gpio12)?;

loop {
Expand Down