Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

How to Control a Servo Motor with an Arduino and IR Remote

小编

Published2025-10-15

Sure! Here’s the first part of your article on "Arduino IR Remote Control Servo Motor Code":

Introduction to Arduino Servo Control and IR Remote Technology

When it comes to electronics projects, one of the most exciting aspects is creating systems that can be controlled remotely. Whether you're working on a robot, a model, or even a home automation project, the ability to control components from a distance opens up a world of possibilities. One such setup that has garnered popularity is using an Arduino to control a servo motor via an IR remote. In this article, we will guide you through the process of building a system that allows you to control a servo motor with the push of a button on an IR remote, providing an intuitive way to manipulate mechanical components remotely.

What is an Arduino?

Arduino is a popular open-source electronics platform based on easy-to-use hardware and software. It consists of a microcontroller board that can be programmed to control sensors, motors, lights, and other electronic components. The versatility of Arduino makes it a fantastic choice for hobbyists, students, and even professionals looking to create interactive projects and prototypes.

What is a Servo Motor?

A servo motor is a type of motor that can be precisely controlled to rotate to specific angles. Unlike regular motors that rotate continuously, a servo motor typically rotates to a set position within a specific range (usually 0 to 180 degrees for standard hobby servos). This makes servos perfect for tasks like controlling the position of a robotic arm, steering mechanisms in robots, or even adjusting the position of an antenna. Servo motors are widely used in robotics and automation due to their accuracy and ease of control.

What is IR Remote Control?

An IR (infrared) remote control system works by sending pulses of infrared light to communicate with a receiver. The receiver detects the signals and decodes them, allowing you to perform actions like turning on a TV or adjusting the volume. For this project, we'll use an IR remote control and an IR sensor module to send signals to the Arduino, which will then control the servo motor accordingly.

Components You’ll Need:

To follow along with this guide, you’ll need the following components:

Arduino Uno (or any compatible board): This is the brain of the project, controlling the servo motor based on input from the IR remote.

Servo Motor (SG90 or similar): A small, lightweight motor for precise control of position.

IR Remote Control: Any basic remote control will work, but using a universal remote can provide flexibility.

IR Receiver Module (e.g., TSOP1838): This module receives signals from the IR remote and passes them to the Arduino.

Jumper Wires: These will be used to make connections between the Arduino and other components.

Breadboard (optional): If you prefer to avoid soldering, a breadboard is an excellent choice for prototyping.

Setting Up the Circuit and Writing the Code

Now that you know the basic components, let’s move on to setting up the circuit and writing the code to control the servo motor via an IR remote.

Circuit Diagram

Here’s how to connect the components:

IR Receiver to Arduino:

Connect the VCC pin of the IR receiver to the 5V pin of the Arduino.

Connect the GND pin of the IR receiver to the GND pin of the Arduino.

Connect the OUT pin of the IR receiver to a digital input pin on the Arduino (e.g., pin 11).

Servo Motor to Arduino:

Connect the VCC pin of the servo motor to the 5V pin of the Arduino.

Connect the GND pin of the servo motor to the GND pin of the Arduino.

Connect the Signal pin of the servo motor to a PWM pin on the Arduino (e.g., pin 9).

After you’ve made the connections, you can proceed to write the code that will allow your Arduino to interpret the signals from the IR remote and control the servo motor accordingly.

Writing the Code

The code for controlling a servo motor using an IR remote on the Arduino is relatively straightforward. Here’s the complete code to get started:

#include

#include

// Define the pin numbers

const int recv_pin = 11; // IR receiver connected to digital pin 11

Servo myservo; // Create a Servo object to control the servo motor

IRrecv irrecv(recv_pin); // Create an IR receiver object

decode_results results; // Store the decoded IR results

void setup() {

Serial.begin(9600); // Start serial communication for debugging

irrecv.enableIRIn(); // Start the IR receiver

myservo.attach(9); // Attach the servo to pin 9

}

void loop() {

if (irrecv.decode(&results)) { // If an IR signal is received

long int decCode = results.value; // Store the received IR code

Serial.println(decCode); // Print the code to the Serial Monitor

if (decCode == 16753245) { // If the specific button is pressed (IR code can be different)

myservo.write(90); // Rotate servo to 90 degrees

}

else if (decCode == 16736925) { // If another button is pressed

myservo.write(0); // Rotate servo to 0 degrees

}

irrecv.resume(); // Receive the next IR signal

}

}

Explanation of the Code

Libraries: The Servo library allows easy control of the servo motor, while the IRremote library makes it simple to interface with the IR receiver and decode the signals from the remote.

Pin Setup: The recv_pin is the pin where the IR receiver is connected, and myservo.attach(9) binds the servo motor to pin 9 on the Arduino.

IR Signal Decoding: Inside the loop(), the irrecv.decode(&results) function listens for incoming IR signals. Once a signal is detected, it stores the IR code (results.value) and checks if it matches the specific values associated with the remote control buttons.

Servo Control: Depending on the received code, the servo motor’s position is adjusted using myservo.write(degrees), where degrees is the angle (0 to 180) you want the servo to rotate to. For example, pressing one button rotates the servo to 90 degrees, while another rotates it back to 0 degrees.

Conclusion of

In this part, we’ve learned how to set up the hardware components and write the code necessary to control a servo motor with an IR remote using Arduino. With this foundation in place, you now have a powerful, versatile way to control mechanical systems remotely.

In the next part of this article, we’ll dive deeper into troubleshooting, expanding the project, and exploring other features like multiple servo controls and advanced IR remote functionalities. Stay tuned!

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

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