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

How to Control a Servo Motor Using an IR Sensor with Arduino

小编

Published2025-10-15

Sure! Here's the first part of your article about "IR Sensor and Servo Motor Arduino Code."

part 1: Introduction to IR Sensors and Servo Motors

When it comes to building interactive projects with Arduino, combining sensors and actuators can open up a world of possibilities. In this article, we’ll walk you through the process of using an IR (Infrared) sensor to control a servo motor. Whether you’re new to Arduino or a seasoned maker, this project will help you understand how to integrate sensors with motors to create more dynamic and interactive systems.

Understanding the IR Sensor

The IR sensor is a versatile and widely used component in many electronics projects. It works by emitting infrared light, which is then reflected back to the sensor by objects in its path. The sensor detects the reflected infrared light and outputs a signal. IR sensors are often used for object detection, distance measurement, and remote control applications.

In our case, we’re using the IR sensor to detect a signal from a remote control. The sensor sends data to the Arduino, which processes the input and controls the movement of a servo motor based on the command.

What is a Servo Motor?

A servo motor is a specialized motor used in many applications that require precise control of angular position, speed, and acceleration. Unlike regular DC motors, servo motors have a built-in feedback mechanism that allows them to hold a specific position. This makes them ideal for tasks like controlling the position of a robotic arm, steering wheels, or even in camera gimbals.

Servo motors are easy to control with an Arduino because they use Pulse Width Modulation (PWM) signals. This enables the Arduino to control the angle of the servo motor by varying the length of the pulse sent to the motor.

Components You’ll Need

Before we dive into the code, let’s make sure you have all the necessary components to complete this project:

Arduino board (e.g., Arduino Uno)

IR sensor module

IR remote control (any standard remote control should work)

Servo motor

Jumper wires

Breadboard (optional, but helpful for connecting components)

External power supply for the servo motor (optional, depending on your servo's power requirements)

Setting Up the IR Sensor

To begin, connect the IR sensor to your Arduino. Most IR sensors come with three pins: VCC, GND, and OUT. The VCC pin goes to 5V on the Arduino, the GND pin goes to ground, and the OUT pin is used to send the signal to an Arduino input pin (e.g., pin 2).

Now, let’s move on to the servo motor. The servo typically has three pins: VCC, GND, and PWM. The VCC pin connects to the 5V power supply, the GND pin goes to ground, and the PWM pin connects to a digital output pin on the Arduino (e.g., pin 9).

Writing the Code: Part 1

Now that we have our hardware set up, it's time to write some code to bring everything to life. Start by installing the IRremote library, which simplifies communication with the IR sensor. You can install this library directly from the Arduino IDE by navigating to Sketch > Include Library > Manage Libraries, then searching for "IRremote."

Here’s a basic outline of the code to get started:

#include

#include

const int recv_pin = 2; // Pin for IR sensor

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

decode_results results; // Variable to store the IR data

Servo myServo; // Create a Servo object

int servoPin = 9; // Pin connected to the servo

void setup() {

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

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

myServo.attach(servoPin); // Attach the servo to the specified pin

}

void loop() {

if (irrecv.decode(&results)) { // Check if data is received

long int decCode = results.value; // Get the IR signal value

Serial.println(decCode); // Print the IR signal for debugging

// Control the servo based on the IR signal received

if (decCode == 16724175) { // IR signal for button 1

myServo.write(0); // Move servo to 0 degrees

}

else if (decCode == 16718055) { // IR signal for button 2

myServo.write(90); // Move servo to 90 degrees

}

else if (decCode == 16743045) { // IR signal for button 3

myServo.write(180); // Move servo to 180 degrees

}

irrecv.resume(); // Receive the next value

}

}

How It Works

IR Sensor Setup: The IR sensor is connected to pin 2 of the Arduino. When the sensor detects an infrared signal from the remote control, it sends a corresponding signal to the Arduino.

Servo Control: The servo motor is connected to pin 9 of the Arduino. When a button on the remote control is pressed, the Arduino reads the signal and moves the servo to a corresponding position (0°, 90°, or 180° in this example).

Button Mapping: The values in the code, such as 16724175, correspond to the signals sent by the remote control buttons. These values are specific to the remote control being used, so you’ll need to use the serial monitor to check the codes for your specific remote. When you press a button on the remote, the corresponding code will be displayed in the serial monitor.

Debugging

Once your code is uploaded to the Arduino, open the Serial Monitor in the Arduino IDE. As you press the buttons on the remote, the IR sensor will detect the signal and print the IR code to the Serial Monitor. This allows you to ensure the sensor is correctly receiving data from the remote.

Next Steps

Now that you have the basic framework, you can experiment with different functionality. For example, you can modify the servo positions, add more buttons to control other servos, or even create more complex control schemes, such as controlling the speed of a motor or triggering multiple actions with a single button press.

In the second part of the article, we’ll dive deeper into expanding this project with more advanced features and ideas for further enhancing your IR sensor and servo motor setup. 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.