Advertisement
hrfrazier

pumpPWM.h

Jun 11th, 2025
1,341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include "esphome.h"
  2. #include "pwmWrite.h"
  3. using namespace esphome;
  4.  
  5. #define PUMP_PWM_A 32
  6. #define PUMP_PWM_B 33
  7.  
  8. Pwm pwm = Pwm();
  9. const char TAG[]="PUMP_PWM";
  10. class AeroGardenPumpOutput : public Component, public BinaryOutput {
  11.     public:
  12.         bool runState=false;
  13.         void setup() override {
  14.             ESP_LOGI(TAG,"Started PWM Control.");
  15.             //https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
  16.             pwm.pause();
  17.  
  18.             //The following is done in the write_state function.
  19.             //For the H-Bridge to truly turn off, both outputs need
  20.             // to be 0 or both 1.
  21.             //The inverted pin needs to be detached and reattached as non-inverted
  22.             //for its output to go to 0 on pause or else it will go to 1.
  23.             //So, I just made it detach and reattach both pins on both
  24.             // on/off branches of write_state for simplicity and so that it's clear.
  25.  
  26.             //No longer need below
  27.             //Attach to pwm channel sharing timer 2
  28.             // pwm.attach(PUMP_PWM_A, 4, false);   //pin 32, channel 4, invert=false
  29.             // pwm.attach(PUMP_PWM_B, 5, true);    //pin 33, channel 5, invert=true
  30.             // pwm.write(PUMP_PWM_A, 128, 60);     //Set pin 32 to 50% duty, 60hz
  31.             // pwm.write(PUMP_PWM_B, 128, 60);
  32.         }
  33.         bool state(){return runState;}
  34.         void loop() override {
  35.         }
  36.  
  37.         void write_state(bool state) override {
  38.             if(!runState && state){
  39.                 ESP_LOGI(TAG,"Turning PWM ON.");
  40.                  //resume pins 32 & 33 to 50% duty, 60hz
  41.                 pwm.detach(PUMP_PWM_A);    //pin 32, channel 4, invert=false
  42.                 pwm.detach(PUMP_PWM_B);    //pin 33, channel 5, invert=true
  43.                 pwm.attach(PUMP_PWM_A, 4, false);   //pin 32, channel 4, invert=false
  44.                 pwm.attach(PUMP_PWM_B, 5, true);    //pin 33, channel 5, invert=true
  45.                 pwm.write(PUMP_PWM_A, 128, 60);
  46.                 pwm.write(PUMP_PWM_B, 128, 60);
  47.                 pwm.resume(4); //Resume channel 4
  48.                 pwm.resume(5); //Resume channel 5
  49.             } else if (runState) {
  50.                 ESP_LOGI(TAG,"Turning PWM OFF.");
  51.  
  52.                 //Roundabout way - detach and reattach without inverted pin
  53.                 pwm.detach(PUMP_PWM_A);
  54.                 pwm.detach(PUMP_PWM_B);
  55.                 pwm.attach(PUMP_PWM_A, 4, false);
  56.                 pwm.attach(PUMP_PWM_B, 5, false);
  57.                 pwm.write(PUMP_PWM_A, 0, 60);
  58.                 pwm.write(PUMP_PWM_B, 0, 60);
  59.                 pwm.pause(4); //Pause channel 4
  60.                 pwm.pause(5); //Pause channel 5
  61.             }
  62.             runState = state;
  63.         }
  64. };
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement