Skip to content

Commit

Permalink
Add example for sigma-delta modulation
Browse files Browse the repository at this point in the history
  • Loading branch information
katyo committed Oct 20, 2024
1 parent 0ce7ff1 commit 194a98f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/src/bin/sdm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Connect an oscilloscope to an IO pin and see the modulation sine wave.
//!
//! Also you may connect low-pass filter to SDM output.
//!
//! The following wiring is assumed for ESP32:
//! - SDM pin => GPIO32
//! The following wiring is assumed for ESP32S2/S3:
//! - SDM pin => GPIO3
//! The following wiring is assumed for others:
//! - SDM pin => GPIO2
//% CHIPS: esp32 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::{delay::Delay, gpio::Io, prelude::*, sdm::Sdm};
use esp_println::println;

#[entry]
fn main() -> ! {
let peripherals = esp_hal::init(esp_hal::Config::default());

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
let modulation_pin = io.pins.gpio32;
} else if #[cfg(any(feature = "esp32s2", feature = "esp32s3"))] {
let modulation_pin = io.pins.gpio3;
} else {
let modulation_pin = io.pins.gpio2;
}
}

let mut sdm = Sdm::new(peripherals.GPIO_SD);
let mut sdm_ch = sdm.enable_pin(modulation_pin, 100.kHz()).unwrap();

let delay = Delay::new();

println!("Sigma-delta modulation is on");

loop {
sdm_ch.set_pulse_density(0);
delay.delay_millis(1500);
}
}

0 comments on commit 194a98f

Please sign in to comment.