Lesson 23 Obstacle Avoidance Sensor

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

An Obstacle Avoidance Sensor (as shown below) uses infrared reflection principle to detect obstacles. When there is no object in front, infrared-receiver cannot receive signals; when there is an object in front, it will block and reflect infrared light, then infrared-receiver can receive signals.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Obstacle Avoidance Sensor module

– Several jumper wires

Experimental Principle

An obstacle avoidance sensor mainly consists of an infrared-transmitter, an infrared-receiver and a potentiometer. According to the reflecting character of an object, if there is no obstacle, emitted infrared ray will weaken with the propagation distance and finally disappear. If there is an obstacle, when infrared ray encounters an obstacle, it will be reflected back to the infrared-receiver. Then the infrared-receiver detects this signal and confirms an obstacle exists in front.

In this experiment, we will use an obstacle avoidance sensor module and an LED attached to pin 13 of the SunFounder board to build a simple circuit. With the LED attached to pin 13, connect pin OUT to D8 of the SunFounder board. When the obstacle avoidance sensor detects an obstacle, the LED will be on. Otherwise, it will be off.

Note: The obstacle avoidance distance of infrared sensor is adjustable; you can adjust the obstacle avoidance distance by adjusting the potentiometer.

Experimental Procedures

Step 1: Build the circuit

             Obstacle Avoidance Sensor module             SunFounder Uno

                                               out ————————————– D8

                                                –  —————————————- GND

                                                + —————————————- 5V

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno

Now, put an obstacle in front of the Obstacle Avoidance Sensor and the LED attached to pin 13 on the SunFounder Uno board will light up. 

Code

#define ledPin 13
#define avoidPin 8void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(avoidPin, INPUT);
}void loop()
{
int avoidVal = digitalRead(avoidPin);
if(avoidVal == LOW)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}