Coil Sequencing

Adding the Stepper Extension

Create a new MakeCode project and add the stepper extension:

Stepper extension in MakeCode

The Stepper Program

Enter this program:

Stepper program blocks

You can also import this program from https://github.com/League-Microbit/stepper.

After you download the program, you should see the shaft of the stepper motor moving back and forth.

What Is It Doing?

You will notice that the LEDs on the driver board seem to be blinking, but they are blinking too fast to see the pattern. Let’s slow it down.

In the block “set delay between steps to”, change the value from 0 ms to 500 ms. The “ms” means “milliseconds” or 1/1000 of a second. So 500/1000 seconds is half a second.

After making the change, download the program again and observe the LEDs. Now that we’ve increased the delay, we can see the patterns of the LEDs, which indicate which magnetic coils are being activated to move the motor in one direction or the other.

Tip
If your motor vibrates but does not turn, your delay might be too short or your wires may be in the wrong order. Try increasing the delay or double-checking your IN1-IN4 connections.

Understanding the Coil Pattern

Let’s figure out what these lights mean by looking at the wiring diagram for the motor:

28BYJ-48 Stepper Motor Coils

Image from Random Nerd Tutorials

The squiggly parts of the diagram are magnetic coils — they are just electromagnets. The common connection for all of the coils is pin 5. Each of the LEDs indicates that power is being applied to one of the pins, and when that happens, it turns on one of the electromagnets.

The LED pattern is:

  • 1 & 2
  • 2 & 3
  • 3 & 4
  • 4 & 1

Just like an electromagnet picks up paperclips or nails, the electromagnetic coils in the stepper attract the rotor, which turns a bit. By energizing these coils in order, we can make the rotating rotor “chase” the electromagnets, causing it to rotate.

Tip
This stepping pattern is called full stepping. There are also half-stepping and micro-stepping modes that provide smoother motion and finer positioning, but full stepping is the simplest to understand.

Inside a Stepper Motor

If you want to understand how this works in more detail, this video will show you what is inside the stepper motor:

Learn More

Here is a video about how stepper motors work:

Challenges

  1. Speed test: What is the fastest delay (smallest number of milliseconds) that still makes your motor turn smoothly? Record the value.
  2. Precision challenge: Can you program the motor to turn exactly one full rotation (2,000 steps) and then stop?
  3. Direction control: Modify the program so the motor turns in only one direction. What did you change?