Nucleo-F030R8: 28BYJ-48 stepper with a servo gauge
Half-step a 28BYJ-48 through a ULN2003 with BSRR writes on the F030R8, and show how far round it is on an SG90 gauge driven by TIM3, everything powered from…
Motors · Intermediate
In a real product: Motorised valve with position gauge (Industrial): A geared stepper opens the valve by exact steps; the needle shows its position at a glance.
The 28BYJ-48 is a small geared stepper: 64 half steps turn its motor once, and a 64:1 gearbox makes that 4096 half steps a turn of the shaft. A ULN2003 board switches its four coils, one pin each. Here the Nucleo-F030R8 drives those four pins straight from its GPIO registers, and an SG90 servo points at how far round the shaft has turned.
Both motors run from a 4 × AA pack (6.4 V fresh). Its − goes to the Nucleo's GND, so the pins' signals and the motors share a ground.
The pins are the Nucleo's real Arduino-header pins: PA9 is D8, PA10 is D2, PB5 is D4 and PB4 is D5. (D9–D11 are other pins on this board: PC7, PB6 and PA7.)
What you'll learn
- Stepping a unipolar stepper in half steps from a table of coil patterns
- Setting and clearing pins on two ports at once with
BSRR - Making a servo signal with TIM3 at the F030's 8 MHz reset clock
- Wiring a battery pack so the motors have their own supply and a ground in common with the board
How it works
SEQUENCE holds the eight half steps: one coil, then two, round all four. Each pattern goes out with one BSRR write per port: the low 16 bits set pins, the high 16 clear them, so a pin is never left half changed. Every 2 ms the position moves one half step, so a full turn of the shaft takes about 8 s; at each end the coils are switched off for half a second, then it goes back.
The F030 runs at 8 MHz after reset. TIM3's prescaler of 8 makes it count microseconds, and ARR = 19999 gives the servo its 20 ms period. Channel 1 is PA6 (D12, alternate function 1). gauge() turns 0–180° into 500–2400 µs, and the loop sets it from the position: half a turn of the stepper is 90° on the gauge.
The ULN2003's COM is its supply, and the 28BYJ-48's red wire is the coils' common. A ULN2003 output pulls its coil's end to GND while its input is high, so the coil between the pack's 6.4 V and GND turns on.
Try this
- Set the pack's charge to 0: at 4.0 V the servo still turns, but check what an LM2596 would do with that.
- Shorten
STEP_MSto 1: on a real 28BYJ-48 the motor starts missing steps around 1 ms a half step. - Use full steps (only the two-coil patterns): twice as fast, with a little more torque.
- Wire IN2 to D9 instead and see the motor shudder: D9 is PC7 on this board, which the code never drives.
main.c
// Bare-metal C on the Nucleo-F030R8: no Arduino core and no HAL. CMSIS names every register.
// A 28BYJ-48 on a ULN2003 board (IN1-IN4 on D8, D2, D4, D5: PA9, PA10, PB5, PB4) turns one way a full turn, then back,
// in half steps; an SG90 on D12 (PA6, TIM3 channel 1) points at how far round the turn is, 0-180 degrees. Both run
// from a 4 x AA pack, with its - joined to the board's GND.
#include <stdio.h>
#include "stm32f0xx.h"
#define HALF_STEPS_PER_TURN 4096 // 64 half steps a motor turn, through its 64:1 gearbox
#define STEP_MS 2
static volatile uint32_t ticks; // milliseconds since SysTick started
// The coils IN1-IN4 for each half step: one coil, then two, round the four.
static const uint8_t SEQUENCE[8] = { 0x1, 0x3, 0x2, 0x6, 0x4, 0xC, 0x8, 0x9 };
void SysTick_Handler(void) {
ticks++;
}
static void delay_ms(uint32_t ms) {
uint32_t start = ticks;
while ((uint32_t)(ticks - start) < ms) {}
}
int __io_putchar(int ch) {
while (!(USART2->ISR & USART_ISR_TXE)) {}
USART2->TDR = ch;
return ch;
}
// IN1-IN4 from a pattern, each pin set or reset in one BSRR write per port.
static void coils(uint8_t p) {
GPIOA->BSRR = (p & 0x1 ? 1u << 9 : 1u << (9 + 16)) | (p & 0x2 ? 1u << 10 : 1u << (10 + 16));
GPIOB->BSRR = (p & 0x4 ? 1u << 5 : 1u << (5 + 16)) | (p & 0x8 ? 1u << 4 : 1u << (4 + 16));
}
// The gauge: 0-180 degrees as 500-2400 us at TIM3's 1 us a count.
static void gauge(uint32_t degrees) {
TIM3->CCR1 = 500 + degrees * 1900 / 180;
}
int main(void) {
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN | RCC_APB1ENR_TIM3EN;
// PA9, PA10 outputs (IN1, IN2); PA2 USART2 TX (AF1); PA6 TIM3_CH1 (AF1).
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2 | GPIO_MODER_MODER6 | GPIO_MODER_MODER9 | GPIO_MODER_MODER10))
| GPIO_MODER_MODER2_1 | GPIO_MODER_MODER6_1 | GPIO_MODER_MODER9_0 | GPIO_MODER_MODER10_0;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~((0xFu << 8) | (0xFu << 24))) | (1u << 8) | (1u << 24);
// PB5, PB4 outputs (IN3, IN4).
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER4 | GPIO_MODER_MODER5)) | GPIO_MODER_MODER4_0 | GPIO_MODER_MODER5_0;
USART2->BRR = SystemCoreClock / 115200;
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
// TIM3: 8 MHz / 8 = 1 MHz, 20000 counts = 20 ms, PWM mode 1 on channel 1.
TIM3->PSC = 8 - 1;
TIM3->ARR = 20000 - 1;
gauge(0);
TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
TIM3->CCER = TIM_CCER_CC1E;
TIM3->EGR = TIM_EGR_UG;
TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
SysTick->LOAD = SystemCoreClock / 1000 - 1;
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
printf("28BYJ-48 gauge: %d half steps a turn\r\n", HALF_STEPS_PER_TURN);
int32_t position = 0; // half steps from the start
int direction = 1;
while (1) {
position += direction;
coils(SEQUENCE[position & 7]);
gauge((uint32_t)position * 180 / HALF_STEPS_PER_TURN);
if (position % 512 == 0) printf("%ld degrees\r\n", (long)position * 360 / HALF_STEPS_PER_TURN);
if (position == HALF_STEPS_PER_TURN || position == 0) {
direction = -direction;
coils(0); // rest the coils while it pauses
delay_ms(500);
}
delay_ms(STEP_MS);
}
}