esp-drv8833

Crates.ioesp-drv8833
lib.rsesp-drv8833
version0.1.6
created_at2025-06-06 14:09:45.875405+00
updated_at2025-06-23 16:07:38.609694+00
descriptionA crate that provides control over the DRV8833 Dual H-Bridge Motor Driver
homepage
repositoryhttps://github.com/nguterresn/esp-drv8833
max_upload_size
id1703059
size55,286
Nuno Guterres Nogueira (nguterresn)

documentation

README

About

This crate provides control over the DRV8833 Dual H-Bridge Motor Driver.

The crate requires no external or standard library and only depends on the esp-hal crate. The code uses the espressif LEDC peripheral to control the DRV8833.

The crate uses the slow clock as default:

  • It is better suited for motor control, since the frenquency is quite low, e.g. < 20kHz.
  • It is more power efficient.
  • It can still work under sleep modes.

Drive forward with 100% duty cycle

The followig example shows how to use the crate to drive a brushed motor forward with max duty cycle (100%), using the GPIO1 and GPIO2:

let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);

let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);

let motor_conf = MotorTimerConfig::new(
    &ledc,
    timer::Number::Timer0,
    timer::config::Duty::Duty12Bit,
    Rate::from_khz(1),
)?;

let motor: MotorFastDecay = Motor::new(
    &ledc,
    &motor_conf,
    MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
    MotorLink::new(channel::Number::Channel1, peripherals.GPIO2)
)?;

motor.forward(100)?;

Drive backwards with 50% duty cycle

motor.backward(50)?;

Brake motor

motor.brake()?;

Setup a slow decay motor

let motor: MotorSlowDecay = Motor::new(
    &ledc,
    &motor_conf,
    MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
    MotorLink::new(channel::Number::Channel1, peripherals.GPIO2)
)?;

Setup two motors

// A channel number from 0-7;
let motor_right: MotorFastDecay = Motor::new(
    &ledc,
    &motor_timer_conf,
    MotorLink::new(channel::Number::Channel0, peripherals.GPIO1),
    MotorLink::new(channel::Number::Channel1, peripherals.GPIO2),
)?;
let motor_left: MotorFastDecay = Motor::new(
    &ledc,
    &motor_timer_conf,
    MotorLink::new(channel::Number::Channel2, peripherals.GPIO3),
    MotorLink::new(channel::Number::Channel3, peripherals.GPIO4),
)?;

Setup a stepper motor

let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);

let mut stepper = Stepper::new(
    peripherals.GPIO0,
    peripherals.GPIO1,
    peripherals.GPIO10,
    peripherals.GPIO9,
    Rate::from_hz(200), // 200hz, i.e. 5ms
    200, // steps per rev
);

let delay = Delay::new();
loop {
    stepper.angle(30.0, &delay);
}

Build

cargo build --example forward --features esp32c6
Commit count: 15

cargo fmt