Stepper Motor HAT (B)

From Waveshare Wiki
Jump to: navigation, search
Stepper Motor HAT (B)

Stepper Motor HAT for Raspberry Pi





Provides RPi demo and Jetson Nano demo


  • Raspberry Pi connectivity, compatible with Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+
  • Onboard dual DRV8825 motor controller IC with built-in microstepping indexer,easy to drive two stepper motors
  • 6 available microstepping modes, configured with the DIP switches: full-step, half-step, 1/4-step, 1/8-step, 1/16-step, and 1/32-step
  • Adjustable motor drive current via potentiometer, maximum 2.5A current output
  • Integrates 5V regulator, allows providing power to Raspberry Pi
  • Onboard multi connector options for stepper motors in different specifications


  • Motor controller: HR8825
  • Motor drive voltage: 8.2V~28V
  • Motor drive current: 2.5A
  • Dimension: 65mm × 56mm
  • Mounting hole size: 3.0mm

Onboard Resource

Pin Description
VCC 8.2~28V Power Input
5V 5V Power
GND Ground
A1 Output 1 of bipolar stepper motor M1 winding A
A2 Output 2 of bipolar stepper motor M1 winding A
B1 Output 1 of bipolar stepper motor M1 winding B
B2 Output 2 of bipolar stepper motor M1 winding B
A3 Output 3 of bipolar stepper motor M2 winding A
A4 Output 4 of bipolar stepper motor M2 winding A
B3 Output 3 of bipolar stepper motor M2 winding B
B4 Output 4 of bipolar stepper motor M2 winding B

Onboard resource.png
In which: A1, A2, B1, B2: Control pins of stepper motor M1; A3, A4, B3, B4: Control pins of stepper motor M2;
Power switch: Control power supply for Raspberry Pi; Switch D0-D5: Control subdivision format;
D0-D2: Control stepper motor 1; D3-D5: Control stepper motor 2; Potentiometers: Control output current


Stepper Motor HAT2.png
MP1584 regular supports 4.5V to 28V input, and up to 2A current output. Even though MP1584 supports the lowest input of 4.5V. However, VM also supplies power for the motor controller, which requires at least 8.2V. So, the recommended input voltage is 8.2~28v.
The specification of the power DC head is a powerhead with an outer diameter of 5.5 and an inner diameter of 2.1. There are many DC heads on the market with this specification. You can use a 9V2A or 12V2A power supply for power supply.

Motor Driver

Current regulation2.png

HR8825 is a dual H-bridge motor driver chip that supports 32 subdivisions. It also integrates short-circuit, overheating, under-voltage and cross-conduction protection circuits, which can detect fault conditions and quickly cut off the H-bridge, thus providing protection for the motor and driver chip.
VM is the input voltage, the input range is: 8.2V to 45V, the input range of the integrated voltage regulator chip is 8.2V to 28V.
nSLEEP needs to be kept high, otherwise the chip will directly enter sleep mode, the H-bridge of the device is disabled, the charge pump circuit stops working, the V3P3 output is disabled, and all internal clocks stop working, and all logic inputs are ignored.
The nENBL pin is an enable pin, inputting a low level, the H bridge output is enabled, and the rising edge on STEP is recognized. When the input is high, the H-bridge is disabled, the output is in a high-impedance state, and the STEP input is ignored. It must be disabled when it is not working, otherwise the chip will remain in the enabled state, and the chip and the motor will always be in a high temperature state!
STEP is the stepping clock input, DIR is the direction control input, and MODE0, MODE1, and MODE2 are the subdivision inputs.
Pins 12 and 13 are used to adjust the output current, R13 and R16 are sampling resistors of 0.2 ohms, according to the formula on the data sheet (data sheet: page11):
Ichop = V(xREF)/(5*R(ISENSE))
Substitute R(ISENSE) = 0.2 into the formula
Then the output current is proportional to the voltage on the potentiometer:
I = Vref
If the torque of the motor is not enough, the output current can be increased by adjusting the on-board potentiometer.
Motor Driver2.png

Control Protocol

HR8825 is a dual H-bridge motor driver chip. The H-bridge is a classic control motor circuit, named after its shape like the letter "H". It is composed of four triodes or mosfets, and the middle one is connected to the motor horizontally. To make the motor rotate, a group of diagonal lines needs to be turned on.
When Q1 and Q4 is accessed, the current flow from positive pole->Q1 to Q4->negative pole, then the motor moves forward.
When Q2 and Q4 is accessed, the current flow from the positive pole->Q2 to Q4->negative pole, then the motor moves backward.
If it is two H-bridge, there will be two set of output lines, for example, stepper motor has four wires which is two H-bridge
Control Protocol.png

Protocol of Motor

According to Ampère's circuital law, when the current of coil A flow from left to right, the stator generates a magnetic field, internal side is the North pole which will adopt the rotator of the motor. When the currents of four coils are based on certain rules, they will generate a rotating magnetic field and drive the rotator rotates. If every motor has four rotators, it has four statuses:
Status 1: coil A left in right out(current), coil C right in left out, the motor rotates 0 degree;
Status 2: coil B top in bottom out, coil D bottom in top out, the motor rotates 90 degrees against status 1.
Status 3: coil A right in left out, coil C left in right out, the motor rotates 90 degrees against status 2;
Status 4: coil B bottom in top out, coil D top in left out, the motor rotates 90 degrees against status 3;
Motor turn from the previous status to the next status, we call it to step. The motor rotates in a circle every four steps, and its step angle is 90 degrees.
Most motors have more than four stators. For example, 42 motors, and 57 motors all have 50 stators with a step angle of 1.8 degrees.
28BYJ-48 is four-phase and eight steps, Speed Variation Ratio is 1/64 and its step angle is 5.625/64 degrees.


The rotation of the motor is due to the regular energization of the coil, which will synthesize a uniformly distributed circular rotating magnetic field, thereby attracting the rotor to rotate.
According to this principle, controlling the currents in the windings of each phase can make them rise or fall according to a certain law, that is, to form multiple stable intermediate current states between zero current and maximum current, the direction of the corresponding synthetic magnetic field vector is also There will be multiple stable intermediate states, and only the direction of this vector will be changed, so that the motor can rotate at a smaller angle and rotate more smoothly.
Taking the motor with the above four stators as an example, if it is subdivided into half, then it takes two steps to complete the transition from the first state to the second state, because the state after the first state, the A-coil 50% current, 50% of the current of the B coil will generate a vector magnetic field with an included angle of 45 degrees, thereby attracting the motor rotor to rotate 45 degrees. When running 8 steps, the motor completes one revolution.


HR8825 only needs to give the number of pulses to realize the rotation of the stepper motor. According to the previous analysis, and without subdivision:
How many pulses are needed to drive 42 motors to make one revolution:
360 / 1.8 = 200
Through experiments, it was found that the 42 motors did rotate for a week
How many pulses are needed to drive the 28BYJ-48 motor to make one revolution:
360 / 5.625 * 64 = 4096
So are you sure that's the case? Through experiments, it was found that 28BYJ-48 turned for two weeks.
28BYJ-48 is a four-phase motor. It supports three working modes: single-four-beat, double-four-beat and eight-beat. The step angle of the eight-beat working mode is half of that of single-four-beat and double-four-beat. , is the single four-beat working method used, so it takes 2048 pulses to drive the 28BYJ-48 to make one revolution.


The module supports up to 32 microstepping and can be controlled using software or hardware.

  • Software Microstepping Control:

First, you need to solder the 6 resistors on the back
Modify the corresponding command on the program to enable the corresponding software system
python can be selected by 'softward'
c language can be selected by SOFTWARD
And it is necessary to turn all the DIP switches to 1 for the software control to take effect.

  • Hardware Microstepping Control:

In the c program can be selected by HARDWARD
In python can be selected by 'hardward'
D0, D1, D2 correspond to MODE0, MODE1, MODE2 that control the M1 driver, and D3, D4, and D5 correspond to MODE0, MODE1, and MODE2 that control the M2 driver. The specific correspondence is as follows
Note: The sample program needs to dial all D0-D5 to 0, corresponding to the full step.
Note: The default subdivision is all in the position of 1, so the subdivision of the hardware is very slow, and the experimental effect may not be seen. It is best to adjust it to 0.

Current Setting

The maximum output current of DRV8825 is 2.5A. The size of the current can be adjusted by adjusting the potentiometer in the figure below.
Onboard Risense = 200mR, then we can simplify the formula: Ichop = Vxref.
So Ichop is proportional to Vxref, Vxref is the voltage of the potentiometer, and Ichop is the output current.
When the potentiometer rotates clockwise, the voltage decreases, and when the potentiometer rotates counterclockwise the voltage increases. If you need to measure, you can plug in the power supply to the module, and use a multimeter, the red test lead touches the bottom, and the black test lead touches the upper rectangular pad.
The factory-set potentiometer is in the middle, which can drive most motors.
If the minimum phase current of the motor is larger than this, it may not reach the ideal state, and working for a long time may cause the chip to burn.
Do not turn it counterclockwise to the end. Working for a long time may cause the chip to burn.
Stepper Motor HAT (B).png
Stepper Motor HAT (B)2.png

Raspberry Pi


Stepper Motor HAT Raspberry Pi (BCM)
A1A2B1B2 dir 13
A1A2B1B2 step 19
A1A2B1B2 enable 12
A1A2B1B2 mode 16 17 20
A3A4B3B4 dir 24
A3A4B3B4 step 18
A3A4B3B4 Dir 4
A3A4B3B4 mode 21 22 27

Library Installation


#Enable the Raspberry Pi terminal and run the following command
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
# For more information, please refer to the official website:


#Open the Raspberry Pi terminal and run the following command
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (earlier than before, you may not need to execute), you may need to upgrade:
sudo dpkg -i wiringpi-latest.deb
gpio -v
# Run gpio -v and version 2.52 will appear. If it does not appear, the installation is wrong

#Bullseye branch system use the following command:
git clone
cd WiringPi
gpio -v
# Run gpio -v and version 2.60 will appear. If it does not appear, it means that there is an installation error


sudo apt-get update
sudo pip install RPi.GPIO

Download Sample Program

sudo apt-get install p7zip-full
7z x Stepper_Motor_HAT_Code_B.7z -r -o./Stepper_Motor_HAT_Code_B
sudo chmod 777 -R Stepper_Motor_HAT_Code_B
cd Stepper_Motor_HAT_Code_B/Raspberry\ PI/

Running Sample Program

Provide three programs, BCM2835, wiringPi, and python.
Do the following steps before running the program:
1. Raspberry Pi uses an independent power supply.
2. The DC port of the module is connected to an 8.2V-28V power supply, and the switch is turned ON.
3. Correctly connect the stepper motor.

  • BCM2835 Demo
cd bcm2835/
make clean
sudo ./motor
  • wiringPi Demo
cd wiringpi/
make clean
sudo ./motor
  • Python Demo
cd python/
sudo python

Jetson Nano

Library Installation

sudo apt-get install python-pip
sudo pip install Jetson.GPIO
sudo groupadd -f -r gpio
sudo usermod -a -G gpio your_user_name
sudo udevadm control --reload-rules && sudo udevadm trigger
sudo apt-get install python3-pip
sudo pip3 install Jetson.GPIO
sudo groupadd -f -r gpio
sudo usermod -a -G gpio your_user_name
sudo udevadm control --reload-rules && sudo udevadm trigger

Download Sample Program

sudo apt-get install p7zip-full
7z x Stepper_Motor_HAT_Code_B.7z -r -o./Stepper_Motor_HAT_Code_B
sudo chmod 777 -R Stepper_Motor_HAT_Code_B
cd Stepper_Motor_HAT_Code_B/Jetson\ nano/

Running Sample Program

Provides three demos: C, Python2, Python3

  • C
cd C/
make clean
sudo ./main
  • Python 2
cd python2/
sudo python
  • Python 3
cd python3/
sudo python3






1. The chip is replaced by HR8825 from DRV8825; the function remains unchanged

2. Modify the layout, increase the heat dissipation area, and make the chip fan heat better
3. All interface positions remain unchanged, fully compatible with Stepper Motor HAT
4. Program compatibility



1. Confirm that all the pins of the Raspberry Pi are good and not burned by the pins;

2. Confirm whether superimposing other modules will cause the misuse of pins;
3. Confirm which model your motor is, the default demo is to drive 28BJY-48 (channel 2) for one channel, and one to drive 1.8-degree stepper motors (channel 1) such as nema23 and nema14, if both are 1.8 The stepper motor needs to change the delay of channel 2 to the delay of channel 1. Because the GPIO high and low-level switching is used by default to simulate PWM, different motors have different frequencies for PWM.
4. Replace the new system and test it.



1. Check config.txt to confirm that no other pins are occupied

2. Do not insert other modules, just insert a Stepper Motor HAT
3. Make sure that the driver pins of the Raspberry Pi are all good, and no pins are burned
4. Only forward rotation but not reverse rotation indicates that there is a pin that controls the direction not working



No, because it is driven by GPIO, the two Stepper Motor HATs use the same pins for stacking, and the actions of the two boards are the same even if they are stacked.


It needs to be confirmed that other expansion boards do not occupy GPIO pins. For detailed pins, please refer to the schematic diagram. The driving motor requires 12 pins.


Poke the switch here with a pen to the other side, the default setting is 32 subdivisions, and the program only runs 200 pulses, and the rotation angle is only 360/32 degrees, the phenomenon is difficult to see

Stepper motor hatb2.png



Use the pen to poke the switch here to the other side, the default setting is 32 subdivisions

Stepper motor hatb2.png



Stepper motors generally do not talk about power, because when the control speed of the motor becomes faster or slower, the power consumed by the motor changes, which cannot be expressed in detail. The state we often use is to rotate in a single step. There will be multiple groups or groups of coils that will be energized. Because the winding methods of different stepping motors are different, and the internal resistance of the coils is different, the resulting impedance will be different. If it must be calculated, it can be calculated by torque. To calculate P.png, P is the power, the unit is the watt, n is the revolution per minute, M is the torque, and the unit is Newton·m


1. There is a high probability that the motor cannot be driven. It is recommended that the phase current of the motor should not exceed 2.5A. If it exceeds the risk of heat dissipation without a fan, the driver chip may be burned.

2. The default current is small, it is to try to change the resistance of the potentiometer
3. The power adapter with larger output can be used. The output power of the power adapter provided by us is relatively small, and the motor with a large load may not be able to drive.



Open-loop control, the fundamental difference between open-loop and closed-loop is whether the current running status signal will be fed back. Open-loop has no feedback signal, so it only executes in sequence, while closed-loop feedback signal, the stepper motor driver will take corresponding actions according to the signal. Measures or signals are fed back to the controller for open-loop control.



If you require technical support, please go to the Support page and open a ticket.