How to control this Flapper with Esphome + H-bridge?

my new toy: Flapper (Flap Actuator – Flexar).
it can only be controlled via H Bridge. how can i use Esphome to control this Flapper?
i used the code here and got error instantly upon compiling H-bridge Fan — ESPHome. please help
image

on the hardware side, this is how i already wired it.

no idea what the code will be like since i got errors when compiled

platform: gpio, not platform: ...

i fixed as you suggested:
image

hitting another error now:

Apologies - that output needs to be a float - not a binary. My mistake for not reading your whole post.

If you are using an ESP32 try platform: ledc

For ESP826 try platform: esp8266_pwm

im happy to say i got no errors now with this code, thanks to your help!

output:
  - platform: esp8266_pwm
    id: motor_forward_pin
    pin: GPIO5
  - platform: esp8266_pwm
    id: motor_reverse_pin
    pin: GPIO4

fan:
  - platform: hbridge
    id: my_fan
    name: "Living Room Fan"
    pin_a: motor_forward_pin
    pin_b: motor_reverse_pin
    # enable_pin: motor_enable
    decay_mode: slow

in HA, i only see this though. when flipped to ON, the H-Bridge outputs 12v DC. when flipped Off, i get 0 volt.
how do i get the Speed and Direction adjustment as seen in the example code at H-bridge Fan — ESPHome
image

wiring has been adjusted as well because the first diagram i posted is very wrong.

PWM sounds about right as the inventor also prefers PWM mode as seen here https://www.youtube.com/watch?v=oe-3H8u-2eM

Hmm - no idea there. My only suggestion would to be to remove the device from the ESPHome integration (NOT the ESPHome add-on) and re-add it.

OK. i see now. after clicking on entity, i get speed and direction control. 40% speed is about 5v dc output. but how to get esphome to do PWM?
flat line of 5v dc is no good for this flapper as seen in this video from the inventor https://youtu.be/oe-3H8u-2eM?si=sMzVaa2z_ye_FfWx&t=130

What are you measuring the output with? An oscilloscope is the only thing that will give you an accurate picture of what is happening on the output. A multimeter will just average the voltage of the PWM signal on the output.

Have a read: L298N Motor Driver - Arduino Interface, How It Works, Codes, Schematics

Also you will need to set the PWM frequency to the recommended value that your device requires.

here is the specs of the Flapper: https://cdn.shopify.com/s/files/1/0766/9855/0605/files/Flexar_Flap_Actuator_Datasheet_Rev1.0.pdf?v=1689834771
it’s too bad i dont have an oscilloscope, only a multimeter…
after reading your link on how L298N, i see voltage output on the H driver is done by regulating the freq. so if the freq is very high, then Vout is almost constant of 12v DC, right? if freq is low, then it will average out around 3v… BUT the max instantaneous peak is still 12v, correct? would this max 12v damage the Flapper?
i see in the specs, the voltage should be under 6v for the Flapper, but cant tell if that’s average or max peak

There are several things i can see:
the default PWM is 1000hz, which is way too high (for flaps), you need in the 0.5-10Hz range (max is 25Hz).
but you also want to control the PWM to limit power, which will be high frequency

so you will need 2 PWM’s, one set for the 0-10hz (adjustable) and one at the default 1000hz

for the 0-10hz PWM controlling the flap frequency you set a fixed 50% duty cycle and from the docs in esp8266_pwm you will need to implement this to change the frequency,

on_...:
  - output.esp8266_pwm.set_frequency:
      id: pwm_output
      frequency: XXXHz

and then adjusting the PWM duty cycle of the other PWM at 1000hz will be changing how much force it has. this PWM would be fed into the EN pin on the board you have. and this is where you take into account the formula to limit heat.

But you also need to drive the flap in both directions, so for every forward direction pulse you also need to do a reverse direction pulse to move the flap back down
I’m not sure the best way to do that, probably something like on_change with the low frequency pwm and have it invert the GPIO to the other input.

could you please show me what the code should be instead of this? i dont understand where you want me to insert your codes.
image

and can you show me exactly how to rewire instead of what i currently have?

as of now, the Flapper does not work as you know. The Flapper stays up… “floating” in the air and making this high pitch noise. not super loud but noticeable.

I can give you ideas and help you figure out what’s happening, i just don’t have time to sit and figure out the code to actually make it work. it takes practice reading the docs and figuring out how to put it all together to do what you want. ESP home is big and complex and there is normally more than one way to do things.

to drive your device, you will need
PWM fed to the ENA pin on the board, this will control the amount of power going to the flapper, not how fast it flaps
you will need to toggle IN1 and IN2 at whatever frequency you need, this is not PWM, its a fixed 50% duty cycle, for example
IN1 off IN2 on
wait 500ms
IN2 off IN1 on
wait 500ms
IN1 off IN2 on
wait 500ms
IN2 off IN1 on
wait 500ms
the delay between is what changes the flap speed. 100ms is the lowest you can go and will produce the 10Hz limit. the bigger the number the slower the flaps, 500ms will give 1 flap per second

what you are seeing now is the flap pushed away and humming with the 1000hz PWM you are feeding it.

i wonder if Light mode will work a lot better than Fan as seen here H-bridge Light — ESPHome

try this, i don’t have a way to test it.
you will need 1 additional wire from GPIO14 / D5 to the ENB pin (if you are using IN3 and IN4) - you will need to remove the jumper

output:
  - platform: esp8266_pwm
    id: motor_en_pin
    pin: GPIO14
    max_power: 0.45
switch:
  - platform: gpio
    internal: true
    id: motor_forward_pin
    pin: GPIO5
    on_turn_on:
      then:
        - switch.turn_off: motor_reverse_pin
        - delay: !lambda "return (500 / id(flap_speed).state);"
        - switch.turn_on: motor_reverse_pin
  - platform: gpio
    id: motor_reverse_pin
    pin: GPIO4
    internal: true
    on_turn_on:
      then:
        - switch.turn_off: motor_reverse_pin
        - delay: !lambda "return (500 / id(flap_speed).state);"
        - switch.turn_on: motor_reverse_pin

number:
  - platform: template
    id: flap_speed
    name: "Flap Speed Hz"
    step: 0.1
    min_value: 0.5
    max_value: 10
    optimistic: true
    initial_value: 1

light:
  - platform: monochromatic
    name: "Flap Power"
    output: motor_en_pin

i will try your code when i have some time this weekend.
now, i wonder if i should be using the ESP32 board instead. The ESP32 LED PWM controller has 16 independent channels that can be configured to generate PWM signals with different properties. All pins that can act as outputs can be used as PWM pins (GPIOs 34 to 39 can’t generate PWM). To set a PWM signal, you need to define these parameters in the code: Signal’s frequency; Duty cycle; PWM channel; GPIO where you want to output the signal.
then i can skip using the H bridge altogether, correct???

no, you still need the h-bridge, because the ESP can only handle a few milliamps. the H-Bridge is required to deliver the power required to make the flap move.

the H bridge is a really cool device, there is a few logic gates that prevent the FET’s on the same side of the H from being turned on at the same time. so IN1 controls say the left side, when its low then the bottom FET will be on and the top one off, when you set IN1 high it will turn the bottom FET off and the top one on. IN2 does the same but for the right hand side. the EN pin is used to essentially turn all the FET’s off.
this allows power to be applied in the opposite direction, so your flap will push away from the magnet then pull towards it
you really only need 1 actual PWM, that limits the maximum power. the other PWM set at a square wave at the flap speed is really slow (for a computer) and its pulse width doesn’t change so its not really PWM. in the code above i did away with it and just turn the switches on and off.

i prefer the ESP8266, I’ve had way less issues with them. they seem to be less finicky with the power supply.

I’ve been watching Carl Bugeja for a while and find his projects really interesting and a great use of flexible PCB’s and PCB coils. I’ve often thought about how i can use them round the house, but so far cant think of anything.

I was considering making a bladeless fan, like a tube with PCB coils and magnets that operates in a wave to pump the air along the tube.

i will be using the Flapper for some art projects. myself, im more of an artist than an engineer. it is a problem because i cant code to safe my life. can only do copy n paste from good youtube tutorials. some people on YT dont focus on the coding because they assume others are just as smart, very wrong assumption. this is why i really appreciate your help and Daryl here :smiley:

is this how i should wire it?
the Enable jumper short 2 pins. does it matter where my D5 on the Mini goes to which Enable pins?

that looks right

let us know how it goes