Run Stepper indefinitely

I am using a stepper motor, create a twinkle effect for my fiber optic star ceiling, I need to set the stepper to run indefinitely until I stop it. Can you help please.

My Current config:

esphome:
  name: ceilinglights

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - stepper.set_target:
            id: twinkle
            target: !lambda 'return target;'

ota:
  password: "6fb9fbfdfcbfcbc5cc9542cea7ecb924"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "ssid"
    password: "password"

captive_portal:

stepper:
  - platform: uln2003
    id: twinkle
    pin_a: GPIO15
    pin_b: GPIO16
    pin_c: GPIO17
    pin_d: GPIO18
    max_speed: 50 steps/s
    sleep_when_done: true
    
light:
  - platform: rgbw
    name: "Star Lights"
    red: red_output_1
    green: green_output_1
    blue: blue_output_1
    white: white_output_1
  - platform: rgb
    name: "Accent RGB"
    red: red_output_2
    green: green_output_2
    blue: blue_output_2
    
output:
  - platform: ledc
    id: red_output_1
    pin: GPIO12
    max_power: 50%
    frequency: 1000HZ    
  - platform: ledc
    id: green_output_1
    pin: GPIO14
    max_power: 50%
    frequency: 1000HZ    
  - platform: ledc
    id: blue_output_1
    pin: GPIO27
    max_power: 50%
    frequency: 1000HZ    
  - platform: ledc    
    id: white_output_1
    pin: GPIO26
    max_power: 50%
    frequency: 1000HZ    
  - platform: ledc    
    id: red_output_2
    pin: GPIO33
    max_power: 75%
    frequency: 1000HZ    
  - platform: ledc    
    id: green_output_2
    pin: GPIO32
    max_power: 75%
    frequency: 1000HZ
  - platform: ledc    
    id: blue_output_2
    pin: GPIO25
    max_power: 75%
    frequency: 1000HZ

Seems you’re looking for a loop. You can use a binary template sensor.

binary_sensor:
  - platform: template
    name: "Loop template"
    id: loop_template
    lambda: |-
      if (id(enable_loop)) {
        return true;
      } else {
        return false;
      }
    on_state:
      - while:
          condition:
            binary_sensor.is_on: loop_template
          then:
            - script.execute: twinkle_script 
            - delay: !lambda 'return id(loop_duration);'

enable loop is a input_boolean in HA hat enables the loop. loop_duration is an input_number in HA that adjusts the length of the loop. To bring the values in from HA.

binary_sensor:
## loop on/off    
  - platform: homeassistant
    id: loop_bool
    internal: true
    entity_id: input_boolean.enable_loop
    on_state:
      - globals.set:
          id: enable_loop
          value: !lambda 'return id(loop_bool).state;'
sensor:
## loop time          
  - platform: homeassistant
    id: loop_interval
    internal: true
    accuracy_decimals: 2
    entity_id: input_number.loop_interval
    on_value:
      - globals.set:
          id: loop_duration
          value: !lambda 'return id(loop_interval).state * 60000;'

Then store values in globals to hold setting in case the node reboots or HA goes down.

globals:

## loop time    
  - id: loop_duration
    type: int
    restore_value: yes
    initial_value: '300000'    
## loop on/off    
  - id: enable_loop
    type: bool
    restore_value: yes
    initial_value: "true"

Thanks for the help, but I don’t really know what I am suppose to do. Should I just copy/past this to my own configuration on the ESP?

You’ll need to define the steps the motor takes in an automation in order to get the effect you want. Steppers don’t have an infinite movement as far as I can see. You define x amount of steps then it stops.

You’ll need a way to restart the action when it finishes. That’s where a loop could restart the script. So possibly

binary_sensor:
  - platform: template
    name: "Loop template"
    id: loop_template
    lambda: |-
      if (id(enable_loop)) {
        return true;
      } else {
        return false;
      }
    on_state:
      - while:
          condition:
            binary_sensor.is_on: loop_template
          then:
            - script.execute: twinkle_script 
            - script.wait: twinkle_script
### I am not sure that this will wait for the motor to complete it's steps
### Might need to use a delay instead.

Then create an input_boolean in HA named enable loop[ and then added to esphome

binary_sensor:
## loop on/off    
  - platform: homeassistant
    id: loop_bool
    internal: true
    entity_id: input_boolean.enable_loop

stepper

I work on a BBQ skewer,
would someone help me with the code so that the bbq skewer runs in the loop?
…what I still need for automation is a switch to stop.

captive_portal:

cover:
  - platform: template
    name: "BBQ MASTER"
    id: my_blind
    device_class: blind
    open_action:
      - stepper.set_target:
          id: bbq_stepper
          target: 10000
      - sensor.template.publish:
          id: position
          state: !lambda return id(bbq_stepper).target_position;
    close_action:
      - stepper.set_target:
          id: bbq_stepper
          target: 0
      - sensor.template.publish:
            id: position
            state: !lambda return id(bbq_stepper).target_position;
    stop_action:
      - stepper.set_target:
          id: bbq_stepper
          target: !lambda return id(bbq_stepper).current_position;
      - sensor.template.publish:
            id: position
            state: !lambda return id(bbq_stepper).current_position;
    #optimistic: True
    assumed_state: True

sensor:
  - platform: template
    name: "stepper Position"
    id: position

stepper:
  - platform: a4988
    id: bbq_stepper
    step_pin: D0
    dir_pin: D1
    max_speed: 50 steps/s

I fount that it’s much easier to work with a 3d printer stepper driver ie. A4988, but any step/direction controlled stepper driver should, use the led output as the step command for the stepper, the frequency should set the speed off the motor, set the enable and direction pins as switches .

output:
  - platform: ledc
    pin: GPIO16
    id: stepper_step
    max_power: 50%
    frequency: 300HZ

switch:
  - platform: gpio
    pin: 
      number: GPIO15
      inverted: true
    id: stepper_en 
    on_turn_on:
      then:
      - output.turn_on: stepper_step
    on_turn_off:
      then:
      - output.turn_off: stepper_step  
  - platform: gpio
    pin: 
      number: GPIO17
      inverted: true
    id: stepper_dir
2 Likes