Controlling a stepper motor with an ESP32 PLC

Introduction

In this post, You can see how to control a stepper motor using an ESP32 PLC. We will cover the basics of stepper motors, their features, and how they can be used in combination with a driver, being controlled by the PWM outputs of an ESP32 PLC. The motor and driver used in this blog are the 17HS16-2004S and the TB6600, respectively.

Stepper Motor Basics

Stepper motors are electromechanical devices that convert electrical signals into precise mechanical movements. Unlike conventional motors that rotate continuously, stepper motors move in discrete steps. This unique characteristic makes them ideal for applications that require accurate control over position, speed, and torque.

The discrete steps used by stepper motors correspond to a fixed angular displacement, known as the step angle. The step angle is determined by the motor's design and is typically measured in degrees. Stepper motors can have different step angles, such as 1.8° (200 steps per revolution) or 0.9° (400 steps per revolution). In this case, the 17HS16-2004S uses 200 steps per revolution.

Specific signals are used to control a stepper motor. The most common signals are the pulse, direction signals, and enable. The pulse signal determines the frequency, while the direction signal determines the rotation direction (clockwise or counterclockwise). The enable signal is used to activate or deactivate the motor. By controlling the timing and sequence of these signals, precise positioning and rotation of the stepper motor can be achieved.?

However, these signals cannot be sent to the motor directly, a driver is needed. The driver receives control signals from a controller, such as a microcontroller or PLC, and provides the necessary power and current to the motor. It converts the control signals into the appropriate current levels and waveforms required to drive the motor coils. Stepper motor drivers may offer features such as different control modes (e.g., full step, half step, micro-stepping), current regulation, and protection mechanisms. The TB6600 used in this example allows up to 32 micro-steps and is able of delivering up to 4A of current.

17HS16-2004S Motor

The used 17HS16-2004S NEMA17 motor has the following specifications:

Electrical Specification

Bipolar/Unipolar -- Bipolar

Holding Torque -- 0.45

NmInductance -- 2.6 mH

Phase resistance -- 1.1

OhmRated current -- 2 A

Step Angle -- 1.8o


TB6600 Microstep Driver

The TB6600 supports multiple modes, depending on the microsteps and current:

  • Microsteps: 1, 2, 4, 8, 16, 32
  • Current (A): 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5

To select the micro-steps and the current, the driver has 6 switches. A table in the driver case indicates how to set each switch (on/off) for each mode.

Connections

In order to control the motor, the following connections are required.

Stepper Motor -Driver connections

  • Blue???-->?A-
  • Red????--> A+
  • Black?--> B-
  • Green --> B+

ESP32 PLC - Driver connections

  • Q0.0 --> PUL+?
  • Q0.1 --> DIR+
  • Q0.2 --> ENA+
  • GND --> PUL-, DIR -, ENA -

The ESP32 outputs need to be set to 5V. To properly set them they need?so QVdc needs to be connected to 5V and COM(-) to GND. Also, don't forget to?power the ESP32PLC and the TB6600 Microstep Driver. The same 12-24V power supply can be used for both.

Connection diagram

No alt text provided for this image


Software

Let's see how to program the ESP32 PLC to control the stepper motor. Remember that you need to have the Industrial Shields ESP32 package installed in your Arduino IDE. We will also be using the?Adafruit PWM Servo Driver Library?to output the PWMs.?

Keep in mind that the PWM outputs of the ESP32 PLC support frequencies between 24Hz and 1526Hz, which limits the available speeds of the motor, depending on the steps. To know more, visit this blog about the?ESP32 PWM outputs.

Simple movement

This version of the program makes the motor perform movements at a constant speed.


Firstly, we need to define our physical setup: pins used, the motor steps, etc. In this case, we are using the Q0.0, Q0.1, and Q0.2 pins for the step, direction, and enable signals, respectively. However, any of the PWM outputs of the ESP32 PLC can be used. The STEPS_REV macro calculates the number of steps of a rev depending on the real steps of the motor and the micro-step configuration of the driver, which is useful to calculate the motor speed.


Then we define some functions to set up the motor and its speed. The target_speed variable is used to define the speed, as can be seen in the loop of the program. In order to move the motor we defined the move_to() function, which takes a distance (in revolutions) to move that can be positive or negative depending on the direction. This function calculates the necessary time the motor needs to be moving, depending on the speed, to move the specified distance.

ce.

Acceleration movement

This program moves the motor by applying acceleration and deceleration at the start and end of the movement.?

Instead of calculating the necessary time to perform a movement, this program works by calculating at fixed intervals the traveled distance. Every 30ms in this case, the function?calculate_speed()?is used to calculate the speed the motor should be rotating at in a certain moment in time. This speed depends on the acceleration/deceleration profiles, calculated at the start of the program by the?calculate_accel()?function.?This way the speed of the motor is adjusted every 30ms, accelerating and decelerating when needed. Depending on the acceleration, the movement may not be very fluid, as the speed can vary a lot in 30ms. Due to the ESP32 PLC internal expansion chips, the PWM output speed cannot be updated much faster.

Inside the same loop, every 30ms, the traveled distance is calculated as a function of the speed. This way we keep track of the motor's actual position and can perform fixed-distance moves.

Requirements (To practice the example)

ESP32 PLC

Power Supply

Stepper Motor

Microstep Driver


要查看或添加评论,请登录

Albert Prieto Open Source的更多文章

社区洞察

其他会员也浏览了