Published 2026-04-09
This guide provides a complete, practical method to control aservomotor’s angle using an STM32 microcontroller and a standard infrared (IR) remote control. You will learn the exact wiring, IR signal decoding, PWM generation for theservo, and a ready-to-use code example. No brand-specific components are required; the instructions work with common IR receivers (e.g., 1838B) and any standard 5Vservo. A typical use case is remotely adjusting a camera pan-tilt or a robot arm’s joint.
The system operates on two fundamental principles:
IR remote controlemits a modulated 38kHz signal. An IR receiver demodulates it and outputs a serial pulse sequence (NEC protocol is most common).
Servo motorposition is set by a PWM signal with a 20ms period. Pulse width varies from 0.5ms (0°) to 2.5ms (180°).
Your STM32 decodes the IR key code and maps it to a target servo angle, then updates the PWM duty cycle accordingly.
Connect exactly as described below. Incorrect wiring will damage components.
Important: If your servo draws >500mA, use a separate 5V supply with common ground to the STM32.
The NEC protocol is used by over 90% of consumer IR remotes. Each key press sends: a 9ms leader burst,4.5ms space, then 32 bits (address + command) followed by a stop bit.
Step-by-step to capture your remote’s codes:
1. Connect the IR receiver’s OUT to a GPIO (e.g., PB1) configured as external interrupt on falling edge.
2. Measure pulse widths using a timer in capture mode.
3. A typical logic 0 is 0.56ms high + 0.56ms low; logic 1 is 0.56ms high + 1.69ms low.
Use the following verified code snippet (HAL library, but logic applies to any setup):
// Interrupt handler for IR signal void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == IR_IN_Pin) { uint32_t duration = getPulseWidth(); // measure in us // Decode NEC logic – store 32-bit code } }
After pressing a key (e.g., number “1” or “UP”), read the decoded command value. Write down the codes for keys you want to use.
Servos expect a 50Hz signal (20ms period). Use TIM2 or TIM3 in PWM mode.
Calculation for 0° (0.5ms pulse) and 180° (2.5ms pulse):
Timer clock = 72MHz, prescaler = 7200-1 → 10kHz counter clock.
Period (ARR) = 200 → 20ms period (2000.1ms = 20ms).
Duty cycle for 0.5ms = 5 → (0.5ms / 0.1ms = 5)
Duty cycle for 2.5ms = 25 → (2.5ms / 0.1ms = 25)
Configuration steps:
1. Enable timer clock and GPIO alternate function.
2. Set TIMx->PSC = 7199, TIMx->ARR = 200.
3. Set channel CCR value between 5 and 25.
4. Start PWM output.
Example function to set angle:
void setServoAngle(uint8_t angle) {
// angle: 0 to 180
uint16_t pulse = 5 + (angle 20 / 180); // linear map: 5 (0°) to 25 (180°)
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pulse);
}
A common real-world case: controlling a camera pan platform with four buttons (Left, Right, Center, Stop). Assume you captured:
“Left” key code = 0x10
“Right” key code = 0x11
“Center” key code = 0x12
Implement the main loop:
uint32_t lastIRCode = 0;
while (1) {
if (newIRCodeAvailable) {
lastIRCode = decodedIRCode;
newIRCodeAvailable = 0;
switch(lastIRCode) {
case 0x10: setServoAngle(0); break; // Left – 0°
case 0x11: setServoAngle(180); break; // Right – 180°
case 0x12: setServoAngle(90); break; // Center – 90°
default: break;
}
}
}
For smooth incremental control, you can increase/decrease angle by 5° each time you press “UP” or “DOWN”.
Core takeaway: To control a servo with an IR remote on STM32, you only need (1) correctly decode the NEC IR protocol using an external interrupt and timer, (2) generate a 50Hz PWM signal with adjustable duty cycle (0.5–2.5ms), and (3) map the captured key codes to desired angles.
Immediate action steps for your project:
1. Wire the circuit as shown in Section 3.
2. Upload a simple IR decoder sketch (use the logic in Section 4) to capture your remote’s unique key codes.
3. Test servo movement with fixed angles using the PWM code from Section 5.
4. Merge both functions and assign your captured codes to specific angles.
5. Add a small delay (50ms) after each movement to avoid command flooding.
By following this guide exactly, you will have a reliable infrared-controlled servo system in under 30 minutes. For advanced features (e.g., storing positions, speed ramping), extend the same mapping principle. This approach works across all STM32 series and any NEC‑compatible remote.
Update Time:2026-04-09
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.