Try Embedded

Nucleo-F446RE: camera slider with limit switches

Step a NEMA 17 through a DRV8833 from GPIO, home and reverse on limit switches with EXTI interrupts, and pan a camera with an FS90R continuous servo, all…

Motors · Advanced

In a real product: Motorised camera slider (Consumer electronics): The stepper glides the camera along the rail for time-lapse shots, homing on a switch so every run starts at the same place.

A camera slider moves a camera smoothly along a rail. A stepper knows how far it has gone by counting steps, but not where it started: so at power-up it homes, moving back until a limit switch closes, and counts from there.

Here a DRV8833 drives a NEMA 17's two coils as two H-bridges, the firmware stepping them itself. Two limit switches (HOME and END) raise EXTI interrupts when they close. An FS90R continuous-rotation servo turns the camera slowly as the carriage moves. Everything runs from a 2S Li-ion pack: the DRV8833 straight from its 7.4–8.4 V, and the servo from the 5 V rail of an MB102 module the pack feeds.

Run it, then click HOME to tell it the carriage has reached home. It slides out until you click END (or until its soft limit, 600 steps), comes back, and keeps going.

What you'll learn

  • Driving a bipolar stepper with an H-bridge: two-phase full steps as a table of coil directions
  • Limit switches on EXTI lines from two ports (SYSCFG->EXTICR), falling edges, and flags shared with main
  • Homing: why a stepper needs a reference switch, and a soft limit as a second line of defence
  • A continuous servo: 1500 µs stops it, the distance from 1500 µs sets its speed and direction

How it works

Each DRV8833 bridge has two inputs: 1-0 drives its coil forward, 0-1 backward. A full step changes one coil's direction, so STEPS walks round A+B+, A−B+, A−B−, A+B−: 200 of those turn the NEMA 17 once. step() writes the pattern with BSRR and waits 5 ms, so the carriage moves at one motor turn a second.

HOME is on PA8 (D7) and END on PB10 (D6), both inputs with pull-ups, the switches closing to GND. EXTI lines 8 and 10 are routed to port A and port B in SYSCFG->EXTICR[2], set to trigger on a falling edge, and unmasked. Their handlers clear the pending bit and set a flag; main checks the flags between steps. Both flags are volatile because an interrupt changes them.

The FS90R is on TIM2 channel 1 (PA0, A0), with the same 1 µs count and 20 ms period as any servo. 1500 µs holds it still, 1600 µs creeps one way at about 14 rpm and 1400 µs the other, so the camera pans one way going out and back coming in.

The 2S pack gives 8.4 V full, inside the DRV8833's 2.7–10.8 V. That is too much for the FS90R (it burns out over 7.2 V), so the MB102 module, fed from the pack on its VIN pins, makes 5 V for it on its left rail.

Try this

  • Wire the FS90R's V+ straight to the pack's + and run it: watch the simulator report the burnt-out servo.
  • Set the pack's charge to 0 (6.0 V): the MB102's 5 V rail sags to 4.9 V, and the servo slows.
  • Add half steps (one coil off between two-coil steps) for smoother motion at the same speed.
  • Make HOME a hard stop: if it closes while sliding out, something is wrong; stop the motor and report a fault.

main.c

// Bare-metal C on the Nucleo-F446RE: no Arduino core and no HAL. CMSIS names every register.
// A camera slider: a NEMA 17 on a DRV8833 (AIN1, AIN2, BIN1, BIN2 on D10, D11, D12, D8: PB6, PA7, PA6, PA9), stepped by
// the GPIO, moves the carriage; an FS90R on A0 (PA0, TIM2 channel 1) slowly pans the camera while it moves. Limit
// switches on D7 (PA8, HOME) and D6 (PB10, END) close to GND and raise EXTI interrupts. It homes first, then
// slides back and forth between the switches (or its soft limit).
// Power: a 2S Li-ion pack runs the DRV8833 and feeds an MB102 module whose 5 V rail runs the servo.
#include <stdio.h>
#include "stm32f4xx.h"

#define STEP_MS 5             // 200 full steps a second: one motor turn
#define SOFT_LIMIT 600        // full steps from home: the far end if the END switch never closes
#define PAN_SLOW_US 1600      // the FS90R creeping round (1500 stops it)

static volatile uint32_t ticks;      // milliseconds since SysTick started
static volatile int home_hit, end_hit;

// Two-phase full steps: both coils on, the pattern rotating A+B+, A-B+, A-B-, A+B-.
static const int8_t STEPS[4][2] = { { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 } };

void SysTick_Handler(void) {
  ticks++;
}

// HOME (PA8, EXTI8) and END (PB10, EXTI10): each falling edge sets its flag.
void EXTI9_5_IRQHandler(void) {
  if (EXTI->PR & EXTI_PR_PR8) { EXTI->PR = EXTI_PR_PR8; home_hit = 1; }
}
void EXTI15_10_IRQHandler(void) {
  if (EXTI->PR & EXTI_PR_PR10) { EXTI->PR = EXTI_PR_PR10; end_hit = 1; }
}

static void delay_ms(uint32_t ms) {
  uint32_t start = ticks;
  while ((uint32_t)(ticks - start) < ms) {}
}

int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

// A coil: +1 drives IN1 high and IN2 low (forward), -1 the other way round.
static void coil_a(int d) {
  GPIOB->BSRR = d > 0 ? 1u << 6 : 1u << (6 + 16);
  GPIOA->BSRR = d < 0 ? 1u << 7 : 1u << (7 + 16);
}
static void coil_b(int d) {
  GPIOA->BSRR = (d > 0 ? 1u << 6 : 1u << (6 + 16)) | (d < 0 ? 1u << 9 : 1u << (9 + 16));
}

static int32_t position;   // full steps from home
static void step(int direction) {
  position += direction;
  const int8_t *s = STEPS[position & 3];
  coil_a(s[0]);
  coil_b(s[1]);
  delay_ms(STEP_MS);
}

static void pan(uint32_t us) { TIM2->CCR1 = us; }

static void setup(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN | RCC_APB1ENR_TIM2EN;
  RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
  // PA0 TIM2_CH1 (AF1), PA2 USART2 TX (AF7), PA6 PA7 PA9 outputs, PA8 input with pull-up.
  GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER2 | GPIO_MODER_MODER6 | GPIO_MODER_MODER7
                                   | GPIO_MODER_MODER8 | GPIO_MODER_MODER9))
               | GPIO_MODER_MODER0_1 | GPIO_MODER_MODER2_1 | GPIO_MODER_MODER6_0 | GPIO_MODER_MODER7_0 | GPIO_MODER_MODER9_0;
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu | (0xFu << 8))) | 1u | (7u << 8);
  GPIOA->PUPDR = (GPIOA->PUPDR & ~GPIO_PUPDR_PUPDR8) | GPIO_PUPDR_PUPDR8_0;
  // PB6 output, PB10 input with pull-up.
  GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER6 | GPIO_MODER_MODER10)) | GPIO_MODER_MODER6_0;
  GPIOB->PUPDR = (GPIOB->PUPDR & ~GPIO_PUPDR_PUPDR10) | GPIO_PUPDR_PUPDR10_0;

  // EXTI8 from port A, EXTI10 from port B, on falling edges (a switch closing to GND).
  SYSCFG->EXTICR[2] = (SYSCFG->EXTICR[2] & ~(SYSCFG_EXTICR3_EXTI8 | SYSCFG_EXTICR3_EXTI10)) | SYSCFG_EXTICR3_EXTI10_PB;
  EXTI->FTSR |= EXTI_FTSR_TR8 | EXTI_FTSR_TR10;
  EXTI->IMR |= EXTI_IMR_MR8 | EXTI_IMR_MR10;
  NVIC_EnableIRQ(EXTI9_5_IRQn);
  NVIC_EnableIRQ(EXTI15_10_IRQn);

  USART2->BRR = SystemCoreClock / 115200;
  USART2->CR1 = USART_CR1_TE | USART_CR1_UE;

  // TIM2: 16 MHz / 16 = 1 MHz, 20 ms period, PWM mode 1 on channel 1; 1500 us holds the FS90R still.
  TIM2->PSC = 16 - 1;
  TIM2->ARR = 20000 - 1;
  pan(1500);
  TIM2->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
  TIM2->CCER = TIM_CCER_CC1E;
  TIM2->EGR = TIM_EGR_UG;
  TIM2->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;
}

int main(void) {
  setup();
  printf("Slider: homing (press HOME when the carriage reaches it)\r\n");
  home_hit = 0;
  while (!home_hit) step(-1);
  position = 0;
  printf("Home. Sliding\r\n");

  int direction = 1;
  while (1) {
    end_hit = home_hit = 0;
    pan(direction > 0 ? PAN_SLOW_US : 3000 - PAN_SLOW_US);   // pan one way going out, the other coming back
    while (direction > 0 ? !end_hit && position < SOFT_LIMIT : !home_hit && position > 0) step(direction);
    pan(1500);
    printf("%s at %ld steps\r\n", direction > 0 ? (end_hit ? "END switch" : "Soft limit") : (home_hit ? "HOME switch" : "Home"), (long)position);
    direction = -direction;
    delay_ms(300);   // settle before going back
  }
}

Lessons behind it