Main Content

Sense Stepper Motor Angle Using Hall Effect Encoder

Since R2025a

This example shows how to detect the angle of a stepper motor using a Hall-Effect Rotary Encoder block.

The Hall-Effect Rotary Encoder block models a 360° rotary position sensor using four Hall elements spaced equally under a rotating magnet. Each Hall element varies its output voltage based on the strength of the applied magnetic field. The Hall elements align with the poles of the rotating magnet and experience maximal magnetic flux density at different orientations because the elements are spaced 90° apart. The elements therefore generate four sinusoidal waveforms corresponding to their output Hall voltages, 90° out of phase with one another. These four waveforms uniquely encode the 360° rotary position.

This example uses two models of a stepper motor driven up from 0° to 360°, then down from 360° to 0°:

  • In the HallEffectRotaryEncoderVoltage model, the Hall-Effect Rotary Encoder block has conserving ports in the electrical domain. These ports output the raw Hall element voltages. To enable these electrical conserving ports, set the Output interface parameter to Electrical connections.

  • In the HallEffectRotaryEncoderAngle model, the Hall-Effect Rotary Encoder block has an physical signal output port, Angle. To enable this port, set the Output interface parameter to Decoded Angular Position.

Open Hall-Effect Rotary Encoder with Voltage Output Model

Open the HallEffectRotaryEncoderVoltage model.

open_system("HallEffectRotaryEncoderVoltage.slx")
sim("HallEffectRotaryEncoderVoltage.slx")

Plot Simulation Results from Simscape Logging

The HallEffectRotaryEncoderVoltage model illustrates how the Hall-Effect sensor detects distinct components of the rotational position signal through changes in magnetic flux density. This figure shows a schematic of the encoder. The hall elements, yp, yn, xp, and xn, remain in a fixed position. The north and south poles of the magnet rotate at the motor shaft angle θ.

Simulate the model and plot the results.

HallEffectPlotVoltagesAndAngle

Figure HallEffectRotaryEncoderVoltage contains 2 axes objects. Axes object 1 with title Hall Voltages vs Angle, ylabel Voltage (V) contains 2 objects of type line. These objects represent v_y, v_x. Axes object 2 with xlabel Time (s), ylabel Angle (deg) contains an object of type line. This object represents \theta.

The voltages follow a sinusoidal path with respect to the angle when the motor steps up and steps down. The maxima and minima occur when the poles of the magnet align with two of the sensors. At the start of the simulation, the magnetic poles align with the xn and xp sensors. This alignment creates a maximum voltage across the xn and xp ports, Vx, and a zero voltage across the yn and yp ports, Vy. When the shaft starts to rotate, Vx decreases and Vy increases as the north pole moves towards yp and the south pole moves towards yn.

Open Hall-Effect Rotary Encoder with Decoded Angle Output Model

Open the HallEffectRotaryEncoderAngle model.

open_system("HallEffectRotaryEncoderAngle.slx")
sim("HallEffectRotaryEncoderAngle.slx")

Plot Simulation Results from Simscape Logging

The HallEffectRotaryEncoderAngle model shows how to calculate the angle of the stepper motor using the Angle port of the Hall-Effect Rotary Encoder block. Simulate the model and plot the results.

HallEffectPlotAngle

Figure HallEffectRotaryEncoderAngle contains an axes object. The axes object with title Decoded Angle, xlabel Time (s), ylabel Angle (deg) contains an object of type line.

The results match those of the HallEffectRotaryEncoderVoltage model.

See Also