Nucleo-F401RE: pan-tilt camera head on a stepper turntable
Pan an MG996R and tilt an SG90 with TIM3 PWM, turn a NEMA 17 turntable through a TMC2209 with TIM2 step interrupts, all powered from a 12 V supply and an…
Motors · Advanced
In a real product: Motorised camera head (Robotics): Servos aim the camera quickly; the stepper turns the whole head to an exact angle without a position sensor.
A camera head needs two kinds of motion. Pan and tilt must be quick and hold their angle: that is a job for servos. The whole head must also turn round, any number of degrees, and come back exactly: that is a stepper's job.
Here an MG996R (pan) and an SG90 (tilt) are driven by one timer, TIM3, making 50 Hz pulses on two channels. A NEMA 17 turns the turntable through a TMC2209 driver, and TIM2's interrupt makes its STEP pulses. Neither motor runs from the Nucleo: a 12 V supply feeds the TMC2209 directly, and an LM2596 buck converter turns the same 12 V into 6 V for the servos.
Type commands in the Serial Monitor: pan 150, tilt 20, turn 90, turn -45, where.
What you'll learn
- Making two servo signals from one timer: a 1 µs counter,
ARRfor the 20 ms period, oneCCRper channel for the pulse - Generating stepper STEP pulses in a timer interrupt, and counting the motor's position in microsteps
- Why motors get their own supply: a 12 V rail for the stepper driver, and a buck converter stepping it down for the servos
- Reading commands from USART2 by polling
RXNE
How it works
After reset the F401 runs at 16 MHz. TIM3's prescaler divides that by 16, so it counts microseconds, and ARR = 19999 makes the 20 ms period a servo expects. Channel 1 (PB4, D5) pans and channel 2 (PC7, D9) tilts: each CCR is the pulse width in µs. The MG996R takes 500–2500 µs for 0–180°, the SG90 500–2400 µs, so set_servos() scales each angle to its own range. The timer keeps the pulses coming with no CPU time at all.
The TMC2209 moves one microstep for each rising edge on STEP. With MS1 and MS2 left open it runs at 1/8 steps, so a 200-step NEMA 17 needs 1600 microsteps a turn. TIM2 interrupts 1600 times a second; while the turntable is short of its target, the handler sets DIR, raises STEP, counts one microstep and drops STEP again. A TMC2209 needs STEP high for only 100 ns, which two register writes already give. turn only moves the target, so the motor runs while the main loop keeps reading commands.
The power is wired as on a real build. The servos together can draw 3 A when stalled, far more than the Nucleo's USB gives, so the LM2596 makes 6 V for them from the 12 V supply. Its IN− and OUT− are one, and the supply's V− is joined to the Nucleo's GND, so every signal has a common ground to be measured against.
Try this
- Set the LM2596's output to 9 V and run
pan 30: the servos burn out (the simulator reports the over-voltage). - Wire MS1 to
+3V3(1/32 steps) and changeMICROSTEPS_PER_TURNto match. - Add acceleration: start TIM2 slow and shorten
ARRstep by step as the turntable gets going. - Add a
scancommand that pans from 0 to 180° in 5° steps, one every 100 ms.
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// A camera head: an MG996R pans on D5 (PB4) and an SG90 tilts on D9 (PC7), both on TIM3 at 50 Hz; a NEMA 17 on a TMC2209
// turns the turntable under it, its STEP pulses made by TIM2's interrupt. Commands arrive over USART2 (the Serial Monitor):
// pan 0-180 tilt 0-180 turn <degrees, - for back> where
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#define STEP_PIN 10 // PA10 (D2)
#define DIR_PIN 3 // PB3 (D3)
#define EN_PIN 5 // PB5 (D4)
#define MICROSTEPS_PER_TURN 1600 // 200 full steps at 1/8 (MS1 and MS2 open)
#define STEP_RATE_HZ 1600 // one turntable turn a second
static volatile int32_t position; // turntable, in microsteps from where it started
static volatile int32_t target;
static int pan = 90, tilt = 90;
// One STEP pulse each update while the turntable is short of its target. The TMC2209 needs it high for 100 ns: the
// two writes are further apart than that.
void TIM2_IRQHandler(void) {
TIM2->SR = ~TIM_SR_UIF;
if (position == target) return;
int forward = target > position;
if (forward) GPIOB->BSRR = 1u << DIR_PIN; else GPIOB->BSRR = 1u << (DIR_PIN + 16);
GPIOA->BSRR = 1u << STEP_PIN;
position += forward ? 1 : -1;
GPIOA->BSRR = 1u << (STEP_PIN + 16);
}
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
// 0-180 degrees as a pulse of 500-2500 us (the MG996R) or 500-2400 us (the SG90), at TIM3's 1 us a count.
static void set_servos(void) {
TIM3->CCR1 = 500 + pan * 2000 / 180;
TIM3->CCR2 = 500 + tilt * 1900 / 180;
}
static void setup_pins(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
// PA2/PA3: USART2 (AF7). PA10: STEP, an output.
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2 | GPIO_MODER_MODER3 | GPIO_MODER_MODER10))
| GPIO_MODER_MODER2_1 | GPIO_MODER_MODER3_1 | GPIO_MODER_MODER10_0;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFFu << 8)) | (7u << 8) | (7u << 12);
// PB3 (DIR) and PB5 (EN) outputs; PB4 TIM3_CH1 (AF2) for the pan servo.
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER3 | GPIO_MODER_MODER4 | GPIO_MODER_MODER5))
| GPIO_MODER_MODER3_0 | GPIO_MODER_MODER4_1 | GPIO_MODER_MODER5_0;
GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(0xFu << 16)) | (2u << 16);
GPIOB->BSRR = 1u << (EN_PIN + 16); // EN low: the driver on
// PC7 TIM3_CH2 (AF2) for the tilt servo.
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER7) | GPIO_MODER_MODER7_1;
GPIOC->AFR[0] = (GPIOC->AFR[0] & ~(0xFu << 28)) | (2u << 28);
}
static void setup_timers(void) {
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN | RCC_APB1ENR_USART2EN;
// TIM3: 16 MHz / 16 = 1 MHz, 20000 counts = 20 ms. PWM mode 1 on channels 1 and 2.
TIM3->PSC = 16 - 1;
TIM3->ARR = 20000 - 1;
set_servos();
TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE
| TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;
TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E;
TIM3->EGR = TIM_EGR_UG;
TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
// TIM2: an update STEP_RATE_HZ times a second.
TIM2->PSC = 16 - 1;
TIM2->ARR = 1000000 / STEP_RATE_HZ - 1;
TIM2->DIER = TIM_DIER_UIE;
TIM2->CR1 = TIM_CR1_CEN;
NVIC_EnableIRQ(TIM2_IRQn);
// USART2 at 115200 baud, transmit and receive.
USART2->BRR = SystemCoreClock / 115200;
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}
static int clamp180(int x) { return x < 0 ? 0 : x > 180 ? 180 : x; }
static void command(char *line) {
char *arg = strchr(line, ' ');
int value = arg ? atoi(arg + 1) : 0;
if (strncmp(line, "pan", 3) == 0) { pan = clamp180(value); set_servos(); }
else if (strncmp(line, "tilt", 4) == 0) { tilt = clamp180(value); set_servos(); }
else if (strncmp(line, "turn", 4) == 0) target += value * MICROSTEPS_PER_TURN / 360;
else if (strncmp(line, "where", 5) != 0) { printf("? pan|tilt|turn|where\r\n"); return; }
printf("pan %d tilt %d turntable %ld deg\r\n", pan, tilt, (long)target * 360 / MICROSTEPS_PER_TURN);
}
int main(void) {
setup_pins();
setup_timers();
printf("Pan-tilt head ready: pan, tilt, turn, where\r\n");
char line[32];
unsigned len = 0;
while (1) {
if (!(USART2->SR & USART_SR_RXNE)) continue;
char c = (char)USART2->DR;
if (c == '\r' || c == '\n') {
line[len] = '\0';
if (len) command(line);
len = 0;
} else if (len < sizeof line - 1) {
line[len++] = c;
}
}
}