⟳
MPMC Lab · Experiment 11 · 8051 · Motor control

Stepper Motor Interfacing

Clockwise and anticlockwise rotation, direction by switch, and rotation by N steps.

☰ All experiments

Aim

To interface a stepper motor with the 8051 and write programs to rotate it clockwise and anticlockwise, and to rotate it by a specified number N of steps.

Learning objectives

1
Principle
Explain how energising coils in sequence turns the rotor in discrete steps.
2
Sequences
Generate wave, full-step and half-step sequences from look-up tables.
3
Direction
Reverse the direction by walking the sequence backwards.
4
Angle & speed
Calculate the angle for N steps and the speed in rpm from the step delay.
5
Driver
Explain the role of the ULN2003 Darlington driver.
6
Limits
Recognise missed steps when stepping faster than the motor can follow.

What this experiment demonstrates

  • Open-loop position control: no sensor is needed to turn exactly N × 1.8°.
  • The torque/speed trade-off of the step delay.
  • Look-up-table driven output sequences.

Sub-tasks

  • 11.1 A Clockwise then anticlockwise
  • 11.1 B Direction selected by a switch
  • 11.2 Rotate by N steps

Excitation sequences

Excitation sequences (D C B A = P2.3…P2.0) Wave (1-phase):01H 02H 04H 08Hlow torque, 1.8°/step Full (2-phase):03H 06H 0CH 09Hhigh torque, 1.8°/step Half-step:01 03 02 06 04 0C 08 09smoother, 0.9°/step Clockwise = walk the table forwards. Anticlockwise = walk it backwards. Skipping or reversing two entries makes the rotor jitter. Steps per revolution = 360° / step angle = 360 / 1.8 = 200. Angle = N × 1.8°. Speed (rpm) = 60 / (steps per rev × time per step). 10 ms/step → 60 / (200 × 0.01) = 30 rpm. Too short a delay (faster than the pull-in rate) → the rotor cannot follow and steps are lost. ULN2003: Darlington sinks up to 500 mA per coil and has built-in flyback diodes (COM pin → +V).
Fig 3. Stepper motor drive sequences and formulas.

Software delay in C

Machine cycle T = 12 / f_osc = 12 / 11.0592 MHz = 1.085 µs (classic 12T core) Inner loop for(j=0; j<115; j++); ≈ 8 machine cycles per pass 115 × 8 × 1.085 µs ≈ 1.0 ms delay_ms(500) ≈ 0.5 s

The exact count depends on the compiler's optimisation level, so calibrate it with Keil's stopwatch (t1 in the status bar) or this simulator's clock. The often-quoted value 1275 was calibrated for a 1-clock DS89C4x0 chip. On a classic 12-clock 8051 it gives about 11 ms per count.

Formulas

Steps per revolution = 360° / step angle = 360 / 1.8 = 200 Angle for N steps = N × 1.8° (half-step: N × 0.9°) Speed (rpm) = 60 / (steps/rev × t_step) t_step = 10 ms → 60 / (200 × 0.010) = 30 rpm

Board wiring (this lab)

CoilPin
AP2.0
BP2.1
CP2.2
DP2.3
Direction switchP3.0 (OFF = CW)

The ULN2003 inverts: a pin at 1 turns its Darlington on and energises the coil. At reset P2 = FFH energises all coils at once, so programs write 0x00 first.

Apparatus & environment

ItemSpecificationQty
PC with Keil µVisionC51/A51 tool chain (evaluation version is fine)1
8051 trainer boardNuvoton W78E052D, 11.0592 MHz crystal1
Nuvoton ISP-ICP utilityAPROM programming over USB-UART (CH340)—
USB cableUSB-A to the board's serial/ISP port1
Power supply+5 V regulated (board adapter)1
Stepper motorUnipolar, 1.8°/step, 5–12 V1
DriverULN2003 module1
SupplyMotor supply (separate from logic)1

Safety rules & good practice

  • ⚡ Switch the board off before connecting or removing modules, ribbon cables or jumpers.
  • 🔌 Check the supply: +5 V only. Reverse polarity or more than 6 V destroys the controller.
  • 🧲 Handle ICs by the edges and touch a grounded surface first (ESD).
  • 🔁 Do not drive a port pin connected to a switch with 0 while the switch is pressed to VCC. Read inputs only after writing 1s.
  • 🔥 If a component gets hot or smells, switch off immediately and inform the instructor.
  • 🌀 Keep fingers and loose clothing away from the shaft.
  • 🔌 Use a separate motor supply. Never power the motor from the 5 V logic regulator.
  • 🌡 Coils get hot when held energised. Release them (P2 = 0) when idle.

Procedure (hardware / real tools)

  1. Connect the ULN2003 inputs to P2.0–P2.3, the motor to the driver, and the motor supply (power off).
  2. Build and program 11.1 A. Mark the shaft and check one revolution clockwise, then one anticlockwise.
  3. Build 11.1 B and toggle the direction switch.
  4. Build 11.2 with N = 50 and measure the angle (should be 90°). Repeat with other values of N.
  5. Reduce the step delay until steps are missed and note the limit.
Click a step to tick it off. Your progress is saved in this browser.

Doing it in this virtual lab

  1. Choose a sub-task chip. The program opens in the µVision window.
  2. Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
  3. Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
  4. Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
  5. Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.

Pre-Lab Test 6 questions · diagnostic

Check your prerequisite knowledge. Each answer gets instant feedback. Score at least 4/6 before using the simulator.

Observation table

Rows are added by ● Record in the simulator: inputs, the expected (theoretical) value and the simulator result. Type your own hand calculation in "Your value" to check it. Add rows by hand for hardware-bench readings. The table is saved in this browser.

#Sub-taskInputsExpectedSimulatorYour valueCheckNote

Edge cases to test

  1. Set the step delay to 1 ms: missed steps (the rotor cannot follow).
  2. Use the sequence 01H, 04H, 02H, 08H: invalid jumps make the rotor jitter.
  3. Remove MOTOR = 0x00;: all coils are on at reset.
  4. Use half-step with N = 50: what angle results?
  5. Change 200 to 100 in 11.1 A: how far does it turn?
  6. 11.1 B with the DIR latch written to 0: the direction switch no longer works.

Analysis & conclusion

Saved automatically in this browser.

Post-Lab Test 9 questions · instant hints

A wrong answer shows a hint so you can try again. Only first attempts count towards the score.

9. Numerical: how many full steps of 1.8° rotate the shaft 270°?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

Finish the post-lab test to see your personalised summary.