What Will I Learn?
- You will learn what a sharp IR sensor module is.
- You will learn how to use a GP2Y0A21 sharp IR sensor module .
- You will learn how to integrate the Sharp IR sensor module with the arduino uno board.
Requirements
Hardware
- Arduino Uno
- Sharp IR sensor module(GP2Y0A21)
- Breadboard
- Jumper Wires
- USB type A to B cable
- Computer
Software
- Arduino Software/ Arduino IDE
Knowledge
- Basic electronics and programming
Difficulty
- Basic
Project description
- This project uses a sharp IR sensor module that is connected to the arduino uno board. The IR sensor is set to be the input and will be measuring the distance from the sensor to the object. It does this by sending a narrow beam of light using its built-in transmitter and then its built-in receiver will receive the light beam that bounced back from the object to the sensor. The difference in the transit time will be measured as an angle, these angles will then be the indication of the distance.
Component description
Arduino Uno - type of arduino board that is used commonly by specialist and hobbyist because of its robust design. It is equipped with a microcontroller board that is based on ATMega328P with 14 advanced I/O pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power jack, a USB port, an ICSP header and a reset button.Reference
Sharp IR sensor - is a small device that uses infrared light to measure the distance of the object. It has a very low power consumption and a variety of outputs. There are 2 main types of these infrared sensors: the analog output and the digital output.
The analog sensors like GP2Y0A710, GP2D120, GP2Y0A41, GP2Y0A21 , and GP2Y0A02 offers true ranging information in the form of analog outputs, meaning it gives the exact distance value of the object from the sensor. While the digital sensor like GP2D15 offers a fixed value, like a threshold, when it reaches that certain range it gives a HIGH signal and if not it gives a LOW signal. Reference
In the figure, you can see the ranges of the analog sensors and the digital sensor. As you noticed, the digital only has a line drawn to it. That is its triggering point or the threshold point, once the object is inside the threshold it will send a HIGH and once outside it will send a LOW signal.
The sensor has 3 pins, the VCC for the voltage source of 5+(red wire). the GND for the ground (black wire)and the SIG pin for the signal(yellow wire).
Tutorial Contents
Using a Sharp IR sensor (analog)
Step 1: Gather the Parts
- The parts can be purchased in your local electronics shop or just order it online for less hassle.
Step 2: Construct the circuit
Connect the Sources
- Connect the 5 Volts pin of the arduino uno board into the top/bottom slot of the breadboard. The connections here are horizontally connected making it an ideal for the sources.
- Connect the GND pin of the arduino uno board into the top/bottom slot of the breadboard but make sure not to place them together to avoid shorting the arduino board.
Connect the Sensor
- Connect the +5V (red) pin of the sharp IR sensor to the voltage source in the breadboard.
- Connect the GND (black) pin of the of the sharp IR sensor to the common ground in the breadboard.
- Connect the SIGNAL (yellow) pin of the IR sensor to the analog pin 5 (A5) of the arduino uno because we will be reading its analog output.
Step 3: Programming
- Connect the arduino uno to the computer by using the USB type A to B cable.
Once connection has been established, open the arduino IDE and go to Tools > Board: > then select Arduino/Genuino Uno.
Now, copy the code below and paste it to your own sketch in the arduino IDE.
int IRpin = 5; //sets the IR sensor @pin 5
//defines the values
float IRvalue;
float IRdistance;
//setup command
void setup() {
Serial.begin(9600); //starts the serial at 9600 baud rate
}
//loop command
void loop() {
IRvalue = analogRead(IRpin); //sets the IRvalue to read the IRpin
IRdistance = 2076*pow(IRvalue, -11); //converts the distance to cm
Serial.println(IRdistance);
Serial.print("Distance = "); //prints the distance measured in cm
delay(100); //delay of 100 milliseconds for every reading
}
The code first defines the pin of the sharp IR sensor at pin A5 and the variables 'IRdistance' and 'IRvalue'. In the setup command, it starts the serial communication at 9600 baud rate. In the loop command, the IRvalue is set to read the analog data of IRpin. The IRdistance is set to convert the distance to centimeters, using the formula found on this link: GP2Y0A21 (4 - 30 cm) . After the conversion the serial will print the values of the 'IRdistance', with a delay of 100 milliseconds for every reading.
- After you have type in the code to your sketch in the arduino IDE, click the Verify button to save the sketch and compile the program.
- If no error is found, click the Upload button to start installing the program to the arduino uno board.
Step 4: Testing
- After the programming is finished, open the Serial monitor in the arduino IDE.
- Once opened, hover your hand in front of the sensor, this should display the readings in the serial monitor.
Curriculum
Check out my other arduino tutorials below
- Arduino 101: Using a KY-036 metal touch sensor
- Arduino 101: Using a Flame sensor module
- Arduino 101: Using a Capacitive Touch sensor to control an LED
- Arduino 101: Using a Reed switch
- Arduino 101: Using a Force sensitive resistor to adjust an LED
- Arduino 101: Using a Gas sensor module
- Arduino 101: Controlling a solenoid lock
- Arduino 101: Alcohol sensor circuit using MQ-3 module
- Arduino 101: Using 2 ultrasonic sensors and servo motor
- Arduino 101: Automated parking gate circuit using ultrasonic and servo motor
- Arduino 101: Motion activated Servo motor with Arduino
- Arduino 101: PIR based burglar alarm with Piezoelectric buzzer and Arduino
- Arduino 101: Tilt Sensor with Arduino
- Arduino Shields
- Arduino for beginners
Posted on Utopian.io - Rewarding Open Source Contributors