티스토리 뷰
IR Infrared Obstacle Avoidance Sensor Control in Arduino: The Ultimate Guide with source code
코딩나우 2023. 2. 8. 10:13IR Infrared Obstacle Avoidance Sensor Control in Arduino: The Ultimate Guide with source code
Introduction:
Infrared (IR) obstacle avoidance sensors are widely used in various fields, such as robotics, automation, and others. These sensors help the robot to detect obstacles in its path and take necessary actions, avoiding collisions. In this article, we will be discussing the control of IR Infrared obstacle avoidance sensors using Arduino. This guide is aimed at helping beginners to get started with the IR Infrared obstacle avoidance sensor and how to interface it with an Arduino board.
H1: Understanding IR Infrared Obstacle Avoidance Sensors
Infrared obstacle avoidance sensors work on the principle of emitting infrared rays and detecting their reflections. When the IR rays hit an obstacle, they get reflected back to the sensor, which then triggers an output signal. This signal can be used to control the movements of the robot. IR obstacle avoidance sensors are available in various shapes and sizes, and they can be used in a wide range of applications.
H2: Interfacing IR Infrared Obstacle Avoidance Sensors with Arduino
The IR Infrared obstacle avoidance sensor has two pins, one for power (Vcc) and the other for ground (GND). To interface the IR Infrared obstacle avoidance sensor with the Arduino board, connect the Vcc pin of the sensor to the 5V pin of the Arduino board and the GND pin of the sensor to the GND pin of the Arduino board. The output pin of the sensor is connected to the digital pin of the Arduino board.
H3: Arduino Sketch for IR Infrared Obstacle Avoidance Sensor
The following is the Arduino sketch for the IR Infrared obstacle avoidance sensor. The sketch will detect the obstacle in front of the sensor and turn on the LED connected to the 13th pin of the Arduino board.
const int obstacleAvoidanceSensor = 2;
const int LED = 13;
void setup() {
pinMode(obstacleAvoidanceSensor, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int obstacleDetected = digitalRead(obstacleAvoidanceSensor);
if (obstacleDetected == HIGH) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
H4: Understanding the Code
The code starts with the declaration of two constants: obstacleAvoidanceSensor and LED. The obstacleAvoidanceSensor constant is used to specify the digital pin of the Arduino board to which the output pin of the IR Infrared obstacle avoidance sensor is connected. The LED constant is used to specify the digital pin of the Arduino board to which the LED is connected.
In the setup function, the pinMode function is used to set the mode of the digital pins as input or output. The obstacleAvoidanceSensor pin is set as input, and the LED pin is set as output.
In the loop function, the digitalRead function is used to read the value of the obstacleAvoidanceSensor pin. If the value of the obstacleAvoidanceSensor pin is HIGH, it means that an obstacle is detected in front of the sensor. In this case, the digitalWrite function is used to turn on the LED connected to the LED pin. If no obstacle is detected, the LED is turned off.