当前位置:网站首页>[homeassistant shakes hands with 28byj-48 stepping motor]

[homeassistant shakes hands with 28byj-48 stepping motor]

2022-06-10 08:13:00 2345VOR

1. Preface

This assembly allows you to connect the stepper motor to ESPHome Use it together . Currently only supported A4988 Stepping driver ( Data sheet ) and ULN2003( Data sheet ).stepper

note
This component does not automatically appear in Home Assistant front end , because Home Assistant I won't support it stepper. see also Home assistant configuration .

2. Basic step configuration

All stepper configuration modes inherit these options .

2.1 Configuration variables :

  • max_speed( It's necessary , float ): Drive step steps/s The maximum speed of the actuator ( In steps per second ) . Please note that , Most steppers cannot operate at a speed higher than 250 Step / Second speed correct step .
  • acceleration( Optional , float ):steps/s^2 The acceleration used to start the movement ( They count / Second squared ). The default is inf Infinite acceleration , therefore The stepper will immediately attempt to drive at full speed . If the first movement of the motor is too rapid for what it is moving , This value is very helpful . If you set this number very small , It takes a while for the motor to accelerate .
  • deceleration ( Optional , float): Same as acceleration, But it is used when the motor decelerates shortly before reaching the set position . The default is inf( Slow down immediately ).

2.2 A4988 Components

 Insert picture description here

Put this code in the... Of this device ESPHome In the configuration file on .

 Insert picture description here

stepper:
  - platform: a4988
    id: my_stepper
    step_pin: D0
    dir_pin: D1
    max_speed: 250 steps/s

    # Optional:
    sleep_pin: D2
    acceleration: inf
    deceleration: inf

2.3 Configuration variables :

  • id ( It's necessary , ID ): Specifies the... Of the stepper ID, So that you can control it .
  • step_pin ( It's necessary , Pin Schema ): STEPA4988 Step driver pins .
  • dir_pin ( It's necessary , Pin Schema ): DIRECTIONA4988 Step driver pins .
  • sleep_pin ( Optional , Pin Schema ): You can also choose to use SLEEPA4988 Step driver pins . If specified , Once the stepper reaches the target number of steps , The driver will enter sleep mode .
  • All others come from Base Stepper Configuration.

note :
If the stepper travels in the wrong direction , You can reverse dir_pin:

stepper:
  - platform: a4988
    # ...
    dir_pin:
      number: D1
      inverted: true

2.4 ULN2003 Components

Put this code in the... Of this device ESPHome In the configuration file on .
 Insert picture description here

# Example configuration entry
stepper:
  - platform: uln2003
    id: my_stepper
    pin_a: D0
    pin_b: D1
    pin_c: D2
    pin_d: D3
    max_speed: 250 steps/s

    # Optional:
    acceleration: inf
    deceleration: inf

2.5 Configuration variables :

  • id ( It's necessary , ID ): Specifies the... Of the stepper ID, So that you can control it .
  • pin_a ( It's necessary , Pin Schema ): The pins of the step control board a .
  • pin_b ( It's necessary , Pin Schema ): The pins of the step control board b .
  • pin_c ( It's necessary , Pin Schema ): The pins of the step control board c .
  • pin_d ( It's necessary , Pin Schema ): The pins of the step control board d .
  • sleep_when_done ( Optional , boolean): Whether to turn off all enablers when the stepper reaches the target position .
  • step_mode ( Optional , string): Step mode of motor operation . One of :
  • FULL_STEP( Default )
  • HALF_STEP
  • WAVE_DRIVE
  • All others come from Base Stepper Configuration.

3. Stepping motor status monitoring

3.1 stepper.set_target action

To use your stepper motor in automation or templates , You can use this action to set the target location ( Step by step ). The stepper will always run to the target position , And stop after reaching the target .target Is a signed integer .

on_...:
  then:
  - stepper.set_target:
      id: my_stepper
      target: 250

  # Templated
  - stepper.set_target:
      id: my_stepper
      target: !lambda |-
        if (id(my_binary_sensor).state) {
    
          return 1000;
        } else {
    
          return -1000;
        }

3.1.1 configuration option :

  • id ( It's necessary , ID ): Stepper ID .
  • target ( Required , int, templatable ): Target location , In steps .

3.1.2 Warning

This will turn the stepper to the absolute position ! To move the steering gear relative to the current position , First reset the current position , Then set the target to a relative value .

on_...:
  then:
    # Move 150 steps forward
    - stepper.report_position:
        id: my_stepper
        position: 0
    - stepper.set_target:
        id: my_stepper
        target: 150

3.2 stepper.report_position action

All steppers start with the target and current position at startup 0. however , for example , If you want to return the stepper motor , It is useful to report the current position of the stepper motor .

Through this operation , You can set the internal position counter of the stepper to a specific value ( In steps ). Please note that , The reported position may cause the stepper to move unexpectedly . for example , If the target and current position of the stepper are 1000 Step , And you “ The report ” The position is 0, The stepper will move forward 1000 Step to match the target again .

on_...:
  then:
  - stepper.report_position:
      id: my_stepper
      position: 250
  # It's best to call set_target directly after report_position, so that the stepper doesn't move
  - stepper.set_target:
      id: my_stepper
      target: 250

  # Templated
  - stepper.report_position:
      id: my_stepper
      position: !lambda |-
        if (id(my_binary_sensor).state) {
    
          return 0;
        } else {
    
          return -1000;
        }

Configuration variables :

  • id ( It's necessary , ID ): Stepper ID .
  • position ( Required , int, templatable ): Location of step-by-step reports .

3.3 stepper.set_speed action

This allows you to set the speed of the stepper at run time .

on_...:
  - stepper.set_speed:
      id: my_stepper
      speed: 250 steps/s

Configuration variables :

  • id ( It's necessary , ID ): Stepper ID .
  • speed ( Required , templatable , float): Drive step steps/s The speed of the actuator ( Steps per second ).

3.4 stepper.set_acceleration action

This operation allows you to set the acceleration of the stepper at run time .

on_...:
  - stepper.set_acceleration:
      id: my_stepper
      acceleration: 250 steps/s^2

Configuration variables :

  • id ( It's necessary , ID ): Stepper ID .
  • acceleration( It's necessary , Templates , float ):steps/s^2 The acceleration used to start the movement ( They count / Second squared ).

3.5 stepper.set_deceleration action

This operation allows you to set the deceleration of the stepper at runtime .

on_...:
  - stepper.set_deceleration:
      id: my_stepper
      deceleration: 250 steps/s^2

Configuration variables :

  • id ( It's necessary , ID ): Stepper ID .

  • deceleration ( Required , templatable , float): And identical acceleration, But it is used when the motor decelerates shortly before reaching the set position .

4. Home assistant configuration

This component does not automatically appear in Home Assistant front end (Overview) in , because Home Assistant Itself does not support stepper.

You can add the following stepper component code to your Home Assistant To configure ( configuration.yaml) in , So that the stepper can be controlled from the front end .

# Add a slider control to Home Assistant to set an integer value
input_number:
  stepper_control:
    name: Stepper Control
    initial: 0
    min: -1000
    max: 1000
    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.livingroom_control_stepper
        data_template:
          target: '{
    { trigger.to_state.state | int }}'

In the code above ,“ stepper_control ” Is a numeric input field ID. It has to be the only , And used as a reference name in the automation section . The display name of this field is in stepper _control Of name In the key .

If you want your user interface to give you more control over the stepper controller , For example, set the acceleration 、 Deceleration, etc , Then you can go to after But before adding more input fields . They can be simple numeric input fields ( Pattern : box ) Or a slider like this . Each of these additional input fields needs to be in ESPHome The equipment API Associated input parameters defined on the service .stepper_controlautomation

The automation section tells Home Assistant What to do when the slider changes . It needs a trigger ( The state of the slider ) And an action . In the trigger section , Reference must be made to the configuration that triggers Automation ID. For us , This is the project In the field . This is why the value is .stepper_controlentity_idstepper_controlinput_numberinput_number.stepper_control

In the operation section , The service name is essential for proper use : It connects the front end of home automation to ESPHome Adhesive for equipment configuration . Although you might expect the syntax to be esphome.<your_device>.<api_service>, But the correct syntax is to underline the device ID Connect to API service ID, for example “ A living room ” yes ESPHome Equipment in China ,“control_ stepper ” This is the of the device API service .esphome.livingroom_control_stepper

The template string is used to extract from Home Assistant Front end components target Fields on ( Define... In the objectives section ) obtain “ state ” value .input_number Then pass the value to ESPHome Defined in the device configuration API service . The data_template The section lists a value for each input parameter on the automatically invoked service . In our case ,ESPHome The device has a single parameter “target” Of API service . If you call this “my_target”, So the last line above should be . It is very important to establish this connection correctly .my_target: ‘{ { trigger.to_state.state | int }}’

The following code needs to enter the ESPHome The configuration file . above , We often talk about “API service ”. This code is where it is defined . You may have added it ( Or something like that ). Please note that , The input variable of the service is called . This matches the automation configuration above . Attention, please. , Variables are defined as integers . This means that it must be an integer , Instead of strings .

# ESPHome configuration
api:
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - stepper.set_target:
            id: my_stepper
            target: !lambda 'return target;'

stepper:
  - platform: ...
    # [...] stepper config
    id: my_stepper

5. lambda call

from lambdas in , You can call several methods on the stepper motor to perform some advanced operations ( For more information , Please refer to the complete API Reference resources ).

  • set_target: Set the target position of the motor to an integer .
// Argument is integer (signed int)
// Set the (absolute) target position to 250 steps
id(my_stepper).set_target(250);
  • report_position: Reports that the current position is an integer .
// Report the (absolute) current position as 250 steps
id(my_stepper).report_position(250);
  • current_position: Get the current position of the stepper as an integer .
int pos = id(my_stepper).current_position;
  • target_position : Obtain the set target position of the stepper as an integer .
int pos = id(my_stepper).target_position;

6. experiment

use ESP-12E Motor expansion board Participate in the experiment
 Insert picture description here

Arduino course :Arduino And 28BYJ-48 Stepping motor handshake
use esp8266 , The steering gear is connected to D0,uln2003 The stepper of is connected to D5,6,7,8.

esphome:
  name: nodemcu3-motor

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
# stepper:https://github.com/esphome/issues/issues/3136
api:
  services:
    - service: control_stepper
      variables:
        target: int
      then:
        - stepper.report_position:
            id : stepper_vmc
            position: 0
        - stepper.set_target:
            id: stepper_vmc
            target: !lambda 'return target;'
        - logger.log:
            format: "The target value is %d"
            args : ['target']
            
ota:
  password: "97e8755852590800ea1ae239e5169c92"

wifi:
  ssid: "J09 502"
  password: "qwertyuiop111" 

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu3-Motor Fallback Hotspot"
    password: "rrLzvQaTiYn8"



captive_portal:

number:
  - platform: template
    name: Servo Control
    min_value: -100
    max_value: 100
    step: 1
    set_action:
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return x / 100.0;'

# Example configuration entry
servo:
  - id: my_servo
    output: pwm_output

# Example output platform
# On ESP32, use ledc output
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D0
    frequency: 50 Hz




#Etat de la connection
binary_sensor:
  - platform: status
    name: "espvmc_Status"
    

    
stepper:
 - platform: uln2003
    id: stepper_vmc
    pin_a: D5
    pin_b: D6
    pin_c: D7
    pin_d: D8
    max_speed: 150 steps/s
    sleep_when_done: true
    acceleration: inf
    deceleration: inf
    
switch:
 - platform: gpio
    name: "switch"
    pin: D2
    
 - platform: template
    name: "Stepper position -250"
    turn_on_action:
      - stepper.set_target:
            id: stepper_vmc
            target: -250
 - platform: template
    name: "Stepper position 250"
    turn_on_action:
      - stepper.set_target:
            id: stepper_vmc
            target: 250

The effect is as follows :

  • Move Servo Control The sliding block actuator also reaches the corresponding position .
  • Click on "Stepper position -250" The button can be reversed to -250, Click on "Stepper position 250" The button can go directly to 250.

 Insert picture description here

See also
API Reference resources

stay GitHub Edit this page on
【Homeassistant Drive the steering gear servo】
Stepper Component

原网站

版权声明
本文为[2345VOR]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100810399559.html