Arduino 101: Using a Sharp IR sensor (analog) (GP2Y0A21)

Words
975
Reading
5 min
Listen
Play
9y

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

image.png

Image source

  • Sharp IR sensor module(GP2Y0A21)

image.png

Image source

  • Breadboard

image.png

Image source

  • Jumper Wires

image.png

Image source

  • USB type A to B cable

image.png

Image source

  • Computer

image.png

Image source

Software

  • Arduino Software/ Arduino IDE

image.png

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.

image.png

Image source

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

image.png

Image source

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).

image.png

Image source

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.
    image.png

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.

image.png

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.

image.png

Step 3: Programming

  • Connect the arduino uno to the computer by using the USB type A to B cable.

image.png

  • Once connection has been established, open the arduino IDE and go to Tools > Board: > then select Arduino/Genuino Uno.
    image.png

  • 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.

image.png

  • If no error is found, click the Upload button to start installing the program to the arduino uno board.

image.png

Step 4: Testing

  • After the programming is finished, open the Serial monitor in the arduino IDE.

image.png

  • Once opened, hover your hand in front of the sensor, this should display the readings in the serial monitor.
    image.png

Curriculum

Check out my other arduino tutorials below



Posted on Utopian.io - Rewarding Open Source Contributors

Arduino 101: Using a Sharp IR sensor (analog) (GP2Y0A21) | Ecency