Home > Industry Insights >Gear Motor
TECHNICAL SUPPORT

Product Support

STM32 Servo Control Using an Infrared Remote: Step-by-Step Guide

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.

01Core Concept – How It Works

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.

02Required Components (Generic, No Brand Names)

Component Specification
STM32 development board Any STM32F1/F4 series (e.g., STM32F103C8T6)
IR receiver module 38kHz carrier, 3-pin (VCC, GND, OUT)
Standard servo motor 4.8V–6.0V, 0°–180° rotation
IR remote control Any NEC-protocol remote (e.g., common TV/DVD remotes)
Power supply 5V/2A (for servo and STM32)
Breadboard & jumper wires For connections

03Circuit Connection (Verified Wiring)

Connect exactly as described below. Incorrect wiring will damage components.

STM32 Pin Connected To
5V (or external 5V) IR receiver VCC & Servo VCC (red wire)
GND IR receiver GND & Servo GND (brown/black wire)
PB0 (or any GPIO with timer channel) Servo signal (orange/yellow wire)
PB1 (or any external interrupt pin) IR receiver OUT

Important: If your servo draws >500mA, use a separate 5V supply with common ground to the STM32.

04IR Decoding – Capturing the Remote Code

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.

05Servo Control – Generating the Correct PWM

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);
}

06Complete Integration – Mapping IR Codes to Servo Angles

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”.

07Troubleshooting – Common Issues & Fixes

Problem Most Likely Cause Verified Solution
Servo jitters or doesn’t move Insufficient power Use external 5V supply for servo, share GND
No IR response Wrong receiver wiring or remote protocol Verify VCC=5V, GND connected; test with oscilloscope or LED on OUT pin
Servo rotates only to extremes PWM period incorrect Measure with oscilloscope; ensure 20ms (50Hz) period
Intermittent decoding IR receiver saturated by ambient light Shade the receiver; add 10kΩ pull-up on OUT pin

08Actionable Conclusion & Recommendation

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

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
+86 0769 8399 3238
 
kpowerMap