ADC to PWM integration
Combine the existing Rust ADC and PWM modules in a single BeagleBone Black application.
Practical Checklist
As in the earlier C ADC-to-PWM practical, this chapter integrates the adc.rs and pwm.rs modules and builds on examples/adc_read.rs and examples/pwm_test.rs.
Mapping adc to pwm
-
To map we need to get the relationship between adc and the period to get the duty cycle, as seen below:
\[ \text{duty_cycle_ns}\ =\ \frac{\text{adc_value} \cdot \text{period_ns}}{4095} \]
-
This has been implemented in the
pwm.rsin the last chapter, for reference here is code snippet for the function:pub fn map_adc_to_duty_cycle(adc_value: u16, period_ns: u32) -> BbbResult<u32> { let adc_value = adc_value as u32; if adc_value > MAX_ADC { return Err(BbbError::InvalidValue(format!( "ADC value out of range: {adc_value}" ))); } Ok((adc_value * period_ns) / MAX_ADC) } -
We will create an example that reuses the functionality developed for
~/examples/adc.rsandexamples/pwm_test.rs. -
Open the file called
~/bbb-io/examples/adc2pwm.rsand reproduce the following code:use std::thread; use std::time::Duration; use bbb_io::adc::{self, AdcPin}; use bbb_io::pwm::{self, Pwm}; fn main() -> bbb_io::BbbResult<()> { let pwm = Pwm::init("P9_14")?; let period_ns = 1_000_000; // 1 ms = 1 kHz pwm.set_period(period_ns)?; pwm.set_duty_cycle(0)?; pwm.enable()?; println!("Phy: {}", pwm.physical_pin); println!("Channel: {}", pwm.channel); println!("Chip: {}", pwm.chip); println!("Period path: {}", pwm.period_path.display()); println!("Duty Cycle path: {}", pwm.duty_cycle_path.display()); println!("Enable path: {}", pwm.enable_path.display()); for _ in 0..30 { let raw = adc::read_raw(AdcPin::A0)?; let mut volts = 0.0; adc::to_voltage(raw, &mut volts)?; let duty_cycle = pwm::map_adc_to_duty_cycle(raw as u16, period_ns)?; println!( "ADC Reading: {raw:4}, Voltage: {volts:.3} V, Duty Cycle: {duty_cycle} ns" ); pwm.set_duty_cycle(duty_cycle)?; thread::sleep(Duration::from_secs(1)); } pwm.disable()?; pwm.cleanup()?; Ok(()) } -
Build and run the program:
rustc examples/adc2pwm.rs --edition=2021 -L target/debug --extern bbb_io=target/debug/libbbb_io.rlib -o adc2pwm -
Setup the following circuit, which is an amalgamation of ADC and PWM circuits

-
When running
./adc2pwmyou may see this terminal output and you have connected up your circuit correctly, the LED should change it's brightness based on the resistance from the potentiometer:If you see:
echo 0 | sudo tee /sys/bus/iio/devices/iio\:device0/buffer/enableand then re-run the program.
or:
Phy: P9_14 Channel: 0 Chip: 0 Period path: /sys/class/pwm/pwmchip0/pwm0/period Duty Cycle path: /sys/class/pwm/pwmchip0/pwm0/duty_cycle Enable path: /sys/class/pwm/pwmchip0/pwm0/enable ADC Reading: 1536, Voltage: 0.675 V, Duty Cycle: 375091 ns ADC Reading: 1537, Voltage: 0.675 V, Duty Cycle: 375335 ns ADC Reading: 1536, Voltage: 0.675 V, Duty Cycle: 375091 ns ADC Reading: 626, Voltage: 0.275 V, Duty Cycle: 152869 ns ADC Reading: 258, Voltage: 0.113 V, Duty Cycle: 63003 ns ADC Reading: 0, Voltage: 0.000 V, Duty Cycle: 0 ns ADC Reading: 0, Voltage: 0.000 V, Duty Cycle: 0 ns ADC Reading: 308, Voltage: 0.135 V, Duty Cycle: 75213 ns ADC Reading: 635, Voltage: 0.279 V, Duty Cycle: 155067 ns ADC Reading: 1078, Voltage: 0.474 V, Duty Cycle: 263247 ns ADC Reading: 1456, Voltage: 0.640 V, Duty Cycle: 355555 ns ADC Reading: 2093, Voltage: 0.920 V, Duty Cycle: 511111 ns ADC Reading: 2565, Voltage: 1.127 V, Duty Cycle: 626373 ns ADC Reading: 3390, Voltage: 1.490 V, Duty Cycle: 827838 ns ADC Reading: 4023, Voltage: 1.768 V, Duty Cycle: 982417 ns ADC Reading: 4094, Voltage: 1.799 V, Duty Cycle: 999755 ns ADC Reading: 4094, Voltage: 1.799 V, Duty Cycle: 999755 ns ... -
Once you have done that, experiment with the code, can you get a faster response, or a longer run time of the program?