Manually control stepper with momentary button

Hello guys,

First post here, hope you’ll be able to help me :slight_smile: . I think it’s a simple problem, but I’m king of new in programming.

I’m building a simple linear rail with a Nema17 and a A4988 and I want to be able to move the carriage with 2 buttons. It should be momentary buttons, meaning the carriage should move until you release the button (or when it reached the limit of determined steps).

I have a limit switch to reset the position to 0 if that’s pertinent.

My goal is to use this device standalone sometimes (without HA), so I want something embedded in ESPHome, would it be possible ?

Right now everything is working fine, but I can’t figure how to properly set those momentary button

Thank you :slight_smile:

Welcome King of New (just joking, couldn’t resist)

Should be possible.

Should just be a matter of the right on-press: and on_release actions.

What have you got for configuration so far?

Oops, small typo lol !

stepper:

  - platform: a4988

    id: test_stepper

    step_pin: D6

    dir_pin:

     number: D5

     inverted: false

    max_speed: 500 steps/s

    sleep_pin:

      number: D7

      inverted: True

    acceleration: inf

    deceleration: inf

binary_sensor:

  - platform: gpio

    pin:

      number: D2

      mode:

        input: true

        pullup: true

    name: "Home_Z"

    filters:

     - invert:

    on_press:

      then:

        - stepper.report_position:

            id: test_stepper

            position: 0

        - stepper.set_target:

            id: test_stepper

            target: 0

        - stepper.set_target:

            id: test_stepper

            target: 25

  - platform: gpio

    pin:

      number: D1

      mode:

        input: true

        pullup: true

    name: "Jog Up"

    filters:

     - invert:

    on_press:

      then:

So here is the code for the motor and the homing switch. Now I want D1 to jog the motor UP. It’s more the process of ‘‘calculating’’ the steps when manually moving the motor than the actual switch.

I’m really bad with Lambda (king of bad !!)

Thank you :slight_smile:

I’m not so familiar with stepper motor control, but I think one way to do it would be like this pseudo code. Can’t help much with the details of stepper control.

  1. on_press of jog_up
  2. while id jog_up is on
  3. Run the following script:
  • Move the stepper motor a small amount
  • delay a small amount
  • wait for the script to finish
    *Loop again.

I don’t know if there is a smarter way to do that. Maybe something to do with checking the number of steps left before issuing a new small increment?

Hi,

I have the same idea, but first I don’t want to use script as I want to be able to use the devide standalone without HA. Also, I’m certainely not able to manually code these steps to make it work.

I’ve been searching the whole internet for few days and I didn’t find anythind, either on Arduino or HA… I can’t belive as this is basically a 3D printer… Thanks !

Everything I mentioned would be in ESPHome.

ESPHome has scripts.

Finally solved the problem by simply buying a rotary encoder switch and adding this short section of code :

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D1
    pin_b: D2
    on_clockwise:
      - logger.log: "Turned Clockwise"
      - stepper.set_target:
            id: test_stepper
            target: !lambda 'return (float(id(test_stepper).current_position) +25);'
    on_anticlockwise:
      - logger.log: "Turned Clockwise"
      - stepper.set_target:
            id: test_stepper
            target: !lambda 'return (float(id(test_stepper).current_position) -25);'

Thanks !