esp-hal/examples/src/bin/adc.rs
Dániel Buga fbc57542a8
Remove pins from Io (#2508)
* Split pins off of Io

* Remove the GPIO peripheral

* p.GPIO
2024-11-12 10:36:25 +00:00

54 lines
1.5 KiB
Rust

//! Connect a potentiometer to an IO pin and see the read values change when
//! rotating the shaft.
//!
//! Alternatively, you could also connect the IO pin to GND or 3V3 to see the
//! maximum and minimum raw values read.
//!
//! The following wiring is assumed for ESP32:
//! - Analog pin => GPIO32
//! The following wiring is assumed for ESP32S2/S3:
//! - Analog pin => GPIO3
//! The following wiring is assumed for others:
//! - Analog pin => GPIO2
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_hal::{
analog::adc::{Adc, AdcConfig, Attenuation},
delay::Delay,
prelude::*,
};
use esp_println::println;
#[entry]
fn main() -> ! {
let peripherals = esp_hal::init(esp_hal::Config::default());
cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
let analog_pin = peripherals.GPIO32;
} else if #[cfg(any(feature = "esp32s2", feature = "esp32s3"))] {
let analog_pin = peripherals.GPIO3;
} else {
let analog_pin = peripherals.GPIO2;
}
}
// Create ADC instances
let mut adc1_config = AdcConfig::new();
let mut adc1_pin = adc1_config.enable_pin(analog_pin, Attenuation::Attenuation11dB);
let mut adc1 = Adc::new(peripherals.ADC1, adc1_config);
let delay = Delay::new();
loop {
let pin_value: u16 = nb::block!(adc1.read_oneshot(&mut adc1_pin)).unwrap();
println!("ADC reading = {}", pin_value);
delay.delay_millis(1500);
}
}