Hbridge motor - not understanding platform for pin a/b

I’m new to ESP32’s and ESPHome but, goodness me, it has been superb in getting started. I’m having a bit of a problem understanding how to use an hbridge (L298N) with my board to get a PWM controlled motor though, and hoping someone might be able to give some advice.

This is my config as it is:

output:
- platform: ledc
  pin: GPIO12
  id: motor_speed_pin
- platform:
  id: motor_forward_pin
  pin: GPIO5
- platform:
  id: motor_reverse_pin
  pin: GPIO4

fan:
- platform: hbridge
  id: my_fan
  name: "Speed"
  pin_a: motor_forward_pin
  pin_b: motor_reverse_pin
  enable_pin: motor_speed_pin
  decay_mode: slow   # slow decay mode (braking) or fast decay (coasting).

Using the h-bridge component for ESPHome.

The docs there not that pin_a and pin_b need to point to a float. I tried using platform: gpio but get:

ID ‘motor_forward_pin’ of type gpio::GPIOBinaryOutput doesn’t inherit from output::FloatOutput. Please double check your ID is pointing to the correct value.

Fair enough. So two questions:

  1. I thought those pins on the h bridge were simply high or low and the “enable” pin can be used for PWM speed control. I don’t understand why the should be floats?
  2. What do I use for the platform? I can’t figure it out from the docs.

Many thanks,
Allan

1 Like

For platform use an output component that supports float ESPHome — ESPHome

Your motor forward and motor reverse pins should be platform “gpio”.

1 Like

I have this same issue and I do not believe this statement is correct. hbridge is expecting output:float and gpio can only be binary or switch.

I believe you are supposed to use a PWM. According to the ESPHome manual, you have three choices (not counting Slow PWM):

I tested only the first. The ESPHome documentation for the fan module is not optimal. Below is what worked for me:

output:
  # Leads (+) and (-) for motor
  - platform: esp8266_pwm
    id: mot_p
    pin: GPIO5
  - platform: esp8266_pwm
    id: mot_n
    pin: GPIO4

fan:
  # Motor controller
  - platform: hbridge
    id: mot
    name: "Motor"
    pin_a: mot_p
    pin_b: mot_n

Hope that helps.

Cheers, AndBu

1 Like

Seeing your reply just now, sorry!
Just had a look in the docs and it seems you are correct.
That, however, doesn’t make any sense to me at all. The h-bridge is expecting a binary state on pin a and pin b. It would work with pwm 0% and 100% as well, but what’s the benefit?
Very curious now as well.

PWM is considered “analog” in the microcontroller output context, because a 50% duty cycle pwm is half the voltage for an inductive load, and half as bright for an LED (as a simplification).

I agree that PWM is wrong here.
Yes we need a PWN for the motor speed.
But a binary for the INx / PIN_x. since they only expect low/high. ( pin_a = forward, pin_b = backwards spin)

Did you find any solution to this?

PWM is not wrong. It is just not properly documented.

Did you try my solution posted above? Before you can use PWM, you have to assign the respective pins under the output section - and you obviously have to have an H-bridge wired to these pins in your physical circuit to drive the motor. You could for example use the cheap DRV8833 or the L293N integrated motor drivers with appropriate external passive components connected.

Cheers, AndBu