Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 08: IR Obstacle Avoidance Sensor Module
In this lesson, you will learn how to use an Infrared obstacle avoidance sensor with an Arduino Uno. We will explore how to read digital signals from the sensor to detect obstacles. You’ll see how the sensor’s red indicator light illuminates in the presence of obstacles and how it sends a low-level signal to the Arduino. This lesson is perfect for beginners, providing hands-on experience with reading digital inputs and practicing serial communication on the Arduino platform.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
Wiring
Code
Code Analysis
Define pin number for sensor connection:
const int sensorPin = 2;
Connect the sensor’s output pin to Arduino pin 2.
Setup serial communication and define sensor pin as input:
void setup() { pinMode(sensorPin, INPUT); Serial.begin(9600); }
Initialize serial communication at 9600 baud rate to print to serial monitor. Set sensor pin as input to read input signal.
Read sensor value and print to serial monitor:
void loop() { Serial.println(digitalRead(sensorPin)); delay(50); }
Continuously read digital value from sensor pin using
digitalRead()and print value to serial monitor usingSerial.println(). Add 50ms delay between prints for better viewing.Note
If the sensor is not working properly, adjust the IR transmitter and receiver to make them parallel. Additionally, you can adjust the detection range using the built-in potentiometer.