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

Mastering Arduino Projects: IR Sensor and Servo Motor Control

小编

Published2025-10-15

Introduction to Arduino, IR Sensors, and Servo Motors

Arduino has revolutionized the world of electronics by providing an easy and accessible platform for hobbyists, engineers, and creators. It allows users to prototype projects, build intricate systems, and even learn programming concepts through hands-on experimentation. In this article, we’ll explore a common yet highly useful project: controlling a servo motor with an infrared (IR) sensor using Arduino.

What is an IR Sensor?

An IR (infrared) sensor is a device that detects infrared radiation, a type of light that is invisible to the human eye. IR sensors are commonly used in applications like object detection, distance sensing, and communication systems. The most basic form of an IR sensor consists of two main components: an emitter and a detector. The emitter emits infrared light, and the detector receives the reflected light from objects in its path. If an object is detected, the sensor sends a signal to the microcontroller, such as Arduino, which processes it and can trigger specific actions.

In the context of Arduino projects, an IR sensor is often used for tasks like object detection, motion sensing, or even remote control functions.

What is a Servo Motor?

A servo motor is a type of motor that can be precisely controlled to rotate within a specific angle range. Unlike traditional motors, which continuously rotate in one direction, a servo motor’s position is adjustable, typically within 0° to 180° (although some models can go beyond this). Servo motors are used in a wide range of applications, from robotics to mechanical systems that require precise control.

A servo motor consists of a small DC motor, a gear train, and a position feedback system. The motor receives control signals in the form of PWM (pulse-width modulation) signals, which determine the angle of rotation. Servo motors are easy to control using microcontrollers like Arduino, which can send PWM signals to control the motor’s position accurately.

Why Use IR Sensors and Servo Motors Together?

Combining an IR sensor with a servo motor in an Arduino project allows you to create interactive systems that respond to the presence or movement of objects. This combination has numerous applications, including robotic arms, automatic doors, gesture control systems, and even surveillance systems.

For instance, in a robotic arm, an IR sensor could be used to detect objects in its path, while the servo motors control the arm’s movement. In a security system, an IR sensor could trigger the movement of a camera or a robotic platform to track objects.

The Basics of Setting Up the IR Sensor and Servo Motor

To begin with, you need the following components:

Arduino board (e.g., Arduino Uno)

IR sensor (e.g., TSOP38238 or similar)

Servo motor (e.g., SG90 or MG995)

Jumper wires and breadboard

The IR sensor typically has three pins: VCC (for power), GND (for ground), and OUT (for output). The output pin sends a HIGH signal when the sensor detects infrared light, and a LOW signal when no object is detected.

The servo motor has three pins as well: VCC (for power), GND (for ground), and Control (for controlling the position via PWM signals). Arduino provides a straightforward way to control the servo motor by sending PWM signals through the Control pin.

Connecting the Components

IR Sensor to Arduino:

Connect the VCC pin of the IR sensor to the 5V pin on the Arduino.

Connect the GND pin of the IR sensor to the GND pin on the Arduino.

Connect the OUT pin of the IR sensor to a digital I/O pin on the Arduino (for instance, pin 2).

Servo Motor to Arduino:

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

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

Connect the Control pin of the servo motor to a PWM-capable pin on the Arduino (for example, pin 9).

Once your hardware is set up, you can start writing the Arduino code that will make everything work.

Writing the Arduino Code and Testing the System

Now that you have the hardware set up, the next step is writing the Arduino code to control the servo motor based on input from the IR sensor. This section will guide you through the entire process of coding, from initializing the components to controlling the servo’s movement.

Arduino Code Setup

Include Necessary Libraries:

You will need the Servo library to control the servo motor. This library allows you to easily send PWM signals to the servo motor to control its position. The IR sensor will be read directly from the digital pin connected to it.

#include // Include the Servo library

// Define the pins

const int irSensorPin = 2; // IR sensor connected to digital pin 2

const int servoPin = 9; // Servo connected to digital pin 9

// Create a Servo object

Servo myServo;

// Variables

int irSensorValue = 0; // Variable to store IR sensor value

void setup() {

// Initialize the IR sensor pin as an input

pinMode(irSensorPin, INPUT);

// Initialize the servo motor

myServo.attach(servoPin);

myServo.write(0); // Set the servo motor to initial position (0 degrees)

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

}

void loop() {

// Read the value from the IR sensor

irSensorValue = digitalRead(irSensorPin);

// If the sensor detects an object (sensor output is HIGH)

if (irSensorValue == HIGH) {

Serial.println("Object detected!");

// Move the servo motor to a new position

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

delay(500); // Wait for half a second

// Reset the servo motor to the initial position

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

delay(500); // Wait for half a second

}

else {

// No object detected, servo stays at 0 degrees

myServo.write(0);

}

}

Understanding the Code

Servo Setup: The Servo.h library is included to help control the servo motor. The Servo myServo; line creates a Servo object, and myServo.attach(servoPin); links the servo to the pin on the Arduino.

IR Sensor Input: The IR sensor is connected to digital pin 2. The digitalRead(irSensorPin) function reads whether the IR sensor detects an object (HIGH) or not (LOW).

Servo Movement: The servo motor's position is adjusted using the myServo.write(angle); function. In this example, the servo moves to 90 degrees when an object is detected and returns to 0 degrees when the object is no longer detected.

Testing the System

After uploading the code to your Arduino, you should test the system. When the IR sensor detects an object, the servo motor should rotate to 90 degrees and then return to 0 degrees after a short delay. If no object is detected, the servo motor should remain at the initial position (0 degrees).

You can experiment with different movements by adjusting the angle values passed to myServo.write(). For example, try setting the servo motor to 45 degrees or 180 degrees for different effects.

Expanding the Project

This basic project is just the beginning. Here are a few ways you can expand and modify this setup:

Multiple Servo Motors: You can connect more than one servo motor to the Arduino and control them in response to the IR sensor. This could be useful for more complex robotics or automation systems.

Timed Movements: Introduce delays or timers to control how long the servo remains in a certain position before returning to the starting point.

Adding More Sensors: Combine multiple IR sensors to create more interactive systems. For example, use an array of sensors to control a robotic arm with more precise movement.

Conclusion

Controlling a servo motor with an IR sensor using Arduino is a great way to start exploring the world of sensors and actuators. By understanding the basic principles of infrared sensing and servo control, you can create a wide variety of interactive systems that respond to their environment. Whether you’re building a simple robotic arm, creating an automatic door, or working on a more advanced automation project, mastering this concept will provide you with a solid foundation for further exploration.

This concludes the two-part guide on controlling a servo motor with an IR sensor using Arduino. With the knowledge gained, you're ready to take on more complex projects and dive deeper into the world of robotics and sensor integration. Happy building!

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.