Stepper motor speed control

Hi,

I’m trying to use a stepper motor to create a twinkling effect on some fiber optic star ceiling.
I would like to set a fixed speed for the stepper motor, but the “stepper.set_speed” action does not seem to work properly.

I followed the example from the stepper documentation, and the stepper motor works fine with the “stepper.set_target” action.

My problem is that when i switch to “stepper.set_speed”, nothing happens. No action using a scope either.

My code is as follows:

ESPHOME config:

api:
  services:
    - service: control_stepper
      variables:
        speed: float
      then:
        - logger.log: "Speed updated"
        - stepper.set_speed:
            id: my_stepper
            speed: !lambda 'return speed;'

# Stepper motor
stepper:
  - platform: a4988
    id: my_stepper
    step_pin: D5
    dir_pin: D6
    sleep_pin: D7
    max_speed: 250 steps/s
    acceleration: inf
    deceleration: inf

Home Automation config:

# Add a slider control to Home Assistant to set an integer value
input_number:
  stepper_control:
    name: Stepper Control
    initial: 0
    min: -100
    max: 100
    step: 1
    mode: slider

# Do something when the slider changes
automation:
  - alias: Write Stepper Value to ESP
    trigger:
      platform: state
      entity_id: input_number.stepper_control
    action:
      # Replace livingroom with the name you gave the ESP
      - service: esphome.stars_control_stepper
        data_template:
          speed: '{{ trigger.to_state.state | float }}'

Anyone got any idea of why the set_speed action does not work?
ps: I also wonder how i could print the value of the variable passed to the api in the logger.log function.

Hi Jan-Erik,

DId you solved this?
I’m actually doing the same thing with the star optics :smiley:

Hi!
I finally got it working, and i would like to share my solution to the problem.

My setup is as follows:
WT32-ETHO1 with temperature sensors (1-wire / Dallas), one stepper motor driver (A4988) and RGBW leds controlled by PWM.

EspHome config

Starlight.yaml

esphome:
  name: stars
  platform: ESP32
  board: esp-wrover-kit

ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO0_IN
  phy_addr: 1
  power_pin: GPIO16

  # Optional manual IP
  manual_ip:
    static_ip: 10.0.3.002
    gateway: 10.0.0.1
    subnet: 255.255.252.0

# Enable logging
logger:
  level: VERBOSE

# Web Server
web_server:
  port: 80

# Globals
globals:
  - id: stepper_speed
    type: float
    restore_value: no
    initial_value: '0'
    
# Enable Home Assistant API
api:
  password: <top-secret>
  services:
    - service: control_stepper
      variables:
        speed: float
      then:
        - globals.set:
            id: stepper_speed
            value: !lambda 'return speed;'
        - logger.log:
            format: "New speed= %.1f"
            args: ['id(stepper_speed)']
        - stepper.set_speed:
            id: my_stepper
            speed: !lambda 'return speed;'
        - stepper.set_target:
            id: my_stepper
            target: !lambda |-
              if (id(stepper_speed) > 0) {
                return 10000000.0;
              }
              else if (id(stepper_speed) < 0) {
                return -10000000.0;
              }
              else {return 0.0;}
ota:
  password: <top-secret>

# Example configuration entry
dallas:
  - pin: GPIO5

# Individual sensors
sensor:
  - platform: dallas
    address: 0x36021833C94CFF28
    name: "Temperatur LED"
  - platform: dallas
    address: 0xDE031850B7B1FF28
    name: "Temperatur DC-DC converter"
  - platform: dallas
    address: 0xBA021833CBABFF28
    name: "Temperatur StepperDriver"

# Stepper motor
stepper:
  - platform: a4988
    id: my_stepper
    step_pin: GPIO4
    dir_pin: GPIO32
    max_speed: 500 steps/s

    # Optional:
    sleep_pin: GPIO2
    acceleration: inf
    deceleration: inf
    
# Example configuration entry
light:
  - platform: rgbw
    name: "Starlight"
    effects:
      - flicker:
      - flicker:
          name: Flicker Effect With Custom Values
          alpha: 95%
          intensity: 1.5%
      - random:
      - random:
          name: Random Effect With Custom Values
          transition_length: 5s
          update_interval: 7s
    red: output_red
    green: output_green
    blue: output_blue
    white: output_white

# Example output entry
output:
  - platform: ledc
    id: output_red
    pin:
      number: GPIO15
    max_power: 100%
  - platform: ledc
    id: output_green
    pin: 
      number: GPIO12
    max_power: 100%
  - platform: ledc
    id: output_blue
    pin: 
      number: GPIO33
    max_power: 100%
  - platform: ledc
    id: output_white
    pin: 
      number: GPIO14
    max_power: 100%

Home Automation

Configuration.yaml

input_number:
  stepper_control:
    name: Star Sparkle
    initial: 0.0
    min: 0.0
    max: 500.0
    step: 10.0
    mode: slider

Automation.yaml

- alias: Write Stepper Value to WT32
  trigger:
    platform: state
    entity_id: input_number.stepper_control
  action:
  - service: esphome.stars_control_stepper
    data_template:
      speed: '{{ trigger.to_state.state | float}}'

On your dashboard you can now create a new Entity (slider) and connect it to the “input_number.stepper_control”. When this slider is changing value, it will trigger a service that sends the speed setpoint to the ESPhome controller.

I hope this may be helpfull to someone!

1 Like

Hi,
I have not had time to look into this project until now, but I found a solution as stated bellow. I hope this helps :beers: