Stepper Motor Board with NuttX

In this fourth installment, in the stepper motor series, sample code is provided to control a stepper motor using NuttX RTOS. If you haven’t, you might want to read the stepper motor fundamentals described in the blog article Stepper Motor, Getting Started, before reading this article.

Hardware

  • Stepper Motor Board

  • ZKit-ARM-1769 Board

  • Stepper Motor

Stepper-ZKit-ARM-1769

Stepper motor board has a 14-pin header capable of mating with a DIO header on the motherboard. But the ZKit-ARM-1769 doesn’t have a 14-pin DIO header. And hence we connect ZKit-ARM-1769 to the Stepper Motor Board through the Proto Header.

  • Connect the Stepper Motor Board to the ZKit-ARM-1769 board’s Proto Header pins, as indicated in [connectivity].

  • Connect the Stepper Motor to the Stepper Motor Board.

  • Connect the 12V supply to the Stepper Motor Board.

Software

#include <nuttx/config.h>
#include <chip/lpc17_gpio.h>

#include <stdio.h>
#include <unistd.h>

#define A1 (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT0 | GPIO_PIN4)
#define B1 (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT0 | GPIO_PIN5)
#define A2 (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT4 | GPIO_PIN28)
#define B2 (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT4 | GPIO_PIN29)

#define MAX_PIN 4

int pins[] = {A1, A2, B1, B2};

void stepper_init(void)
{
        int i;
        for (i = 0; i < MAX_PIN; i++) {
                lpc17_configgpio(pins[i]);
                lpc17_gpiowrite(pins[i], false);
        }
}

int app_main(int argc, char *argv[])
{
        int i;
        int j;

        stepper_init();
        i = 0;
        j = 1;

        while (1) {
                lpc17_gpiowrite(pins[i], true);
                lpc17_gpiowrite(pins[j], true);

                usleep(500 * 1000);  /* 500 ms delay */

                lpc17_gpiowrite(pins[i], false);
                lpc17_gpiowrite(pins[j], false);

                i = (i + 1) % MAX_PIN;
                j = (j + 1) % MAX_PIN;
        }
}

ZKit-ARM-1769 GPIO pins mapping for each of the stepper motor signals is shown in the following diagram.

GPIO pins Phase

GPIO0_4

A1

GPIO0_5

B1

GPIO4_28

A2

GPIO4_29

B2

The pin mapping is stored in array pins, so that it can be easily iterated over.

In stepper_init(), the pins are configured for GPIO functionality, GPIOs direction is set to output.

In the infinite while loop in main(), the stepper motor is driven in full stepping sequence. The sequence table is given below. The phases indicated with a *, are driven 12V, while the other phases are connected to ground.

Pin No. Phase Step 1 Step 2 Step 3 Step 4

0

A1

*

*

1

B1

*

*

2

A2

*

*

3

B2

*

*

Initially pins 0 and 1 are selected. Within the while loop,

  1. The selected pair of pins is turned ON.

  2. A short delay, determines the speed of rotation.

  3. The selected pair of pins is turned OFF, to prepare for the next step.

  4. The next pair of pins is selected based on the sequence table.

Concluding Notes

Hope this article has helped to understand how to use Stepper Motor Board with the NuttX RTOS.