Lolin HW-648 Motor Shield Component for ESPHome

Has anyone ported the arduino library for the Lolin/Wemos D1 Motor shield to ESPHome? they are marked with HW-648 V1.0.0

I have purchased a few of these boards without first checking that the library was available!

If anyone is interested in using one of these to drive a cover I’ve come up with this really quick and dirty code just for testing, till the library gets ported over.

this is my setup for a current cover using an inexpensive INA219 board to current sense the power into the HW-648 Motor shield, running a N20 gear motor. peak current at full speed is about 160mA

i2c:
  sda: D2
  scl: D1
  scan: true
  id: bus_a

sensor:
  - platform: ina219 //this is the current sensor
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      internal: true
      id: current_sensor
    max_voltage: 7.2V
    max_current: 3.2A
    update_interval: 0.2s

cover:
  - platform: current_based
    name: "Blinds Cover"
    malfunction_detection: false
    open_sensor: current_sensor
    open_moving_current_threshold: 0.002
    open_obstacle_current_threshold: 0.15
    open_duration: 30s
    open_action:
      - lambda: |-
          Wire.beginTransmission(0x30);  //this is the address of the motor shield board
          Wire.write(0x00 | (byte)0x10);  //this is the channel 0x00 for A, 0x01 for B
          Wire.write(0x01);  // Direction 0=Brake, 1=CCW, 2=CW, 3=Stop, 4=Standby
          uint16_t _pwm_val=uint16_t(10000);  // speed setting, 1-10000
          if(_pwm_val>10000)
            _pwm_val=10000;
          Wire.write((byte)(_pwm_val >> 8));
          Wire.write((byte)_pwm_val);
          Wire.endTransmission();
    close_sensor: current_sensor
    close_moving_current_threshold: 0.002
    close_obstacle_current_threshold: 0.15
    close_duration: 30s
    close_action:
      - lambda: |-
          Wire.beginTransmission(0x30);
          Wire.write(0x00 | (byte)0x10);
          Wire.write(0x02);
          uint16_t _pwm_val=uint16_t(10000);
          if(_pwm_val>10000)
            _pwm_val=10000;
          Wire.write((byte)(_pwm_val >> 8));
          Wire.write((byte)_pwm_val);
          Wire.endTransmission();
    stop_action:
      - lambda: |-
          Wire.beginTransmission(0x30);
          Wire.write(0x00 | (byte)0x10);
          Wire.write(0x03);
          uint16_t _pwm_val=uint16_t(10000);
          if(_pwm_val>10000)
            _pwm_val=10000;
          Wire.write((byte)(_pwm_val >> 8));
          Wire.write((byte)_pwm_val);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(0x30);  // dirty hack, put the PWM freq in the stop action
          uint32_t freq= 10000;  //pwm frequency
          Wire.write(((byte)(freq >> 16)) & (byte)0x0f);
          Wire.write((byte)(freq >> 16));
          Wire.write((byte)(freq >> 8));
          Wire.write((byte)freq);
          Wire.endTransmission();     // stop transmitting
          delay(100);

    obstacle_rollback: 0%
    start_sensing_delay: 1s
1 Like

Thank you for sharing your code. I just used it with success.

Doesn’t work for me :frowning:

esphome:
  name: test_motor
  platform: ESP8266
  board: d1_mini

i2c:
  sda: D2
  scl: D1
  scan: true

button:
  - platform: template
    name: "Test Button"
    on_press:
      then:
        - lambda: |-            
            Wire.beginTransmission(0x30);
            uint32_t freq = 5000;
            Wire.write(((byte)(freq >> 16)) & (byte)0x0f);
            Wire.write((byte)(freq >> 16));
            Wire.write((byte)(freq >> 8));
            Wire.write((byte)freq);
            Wire.endTransmission();

            Wire.beginTransmission(0x30);
            Wire.write(0x00 | (byte)0x10);
            Wire.write(0x01);
            uint16_t _pwm_val = uint16_t(5000);
            Wire.write((byte)(_pwm_val >> 8));
            Wire.write((byte)_pwm_val);
            Wire.endTransmission();

The following code (from button lambda section) successfully starts motor in Arduino IDE, but doesn’t work in ESPHome. Any ideas?