Monday, April 18, 2016

Sharp ir sensor arduino interfacing code

This tutorial demonstrate how to interface sharp IR Sensor with arduino with code and circuit connections. Sharp Distance sensor gives us linear distance, As we have seen in IR Proximity we get only two states presence and absence of object, but in this we can get the distance of object from sensor, and this sensor gives 80cm sensing distance.

For longer distance measurement you can use GP2Y0A02YK it can measure upto 20cm to 150cm

First we will see the types of shart distance sensors

GP2Y0A21Y:
GP2Y0A21Y Sharp IR Distance Sensor

  • Distance Measuring Sensor Unit 
  • Measuring distance: 10 to 80 cm 
  • Analog output type


GP2Y0A21YK0F is a distance measuring sensor unit, composed of an integrated combination of PSD (position sensitive detector) , IRED (infrared emitting diode) and signal processing circuit. The variety of the reflectivity of the object, the environmental temperature and the operating duration are not influenced easily to the distance detection because of adopting the triangulation method. This device outputs the voltage corresponding to the detection distance. So this sensor can also be used as a proximity sensor.

Applications:
1. Touch-less switch (Sanitary equipment, Control of illumination, etc. )
2. Robot cleaner
3. Sensor for energy saving (ATM, Copier, Vending machine)
4. Amusement equipment (Robot, Arcade game machine)

Features:
1. Distance measuring range : 10 to 80 cm
2. Analog output type
3. Package size : 29.5×13×13.5 mm
4. Consumption current : Typ. 30 mA
5. Supply voltage : 4.5 to 5.5 V

Pin Diagram of Sharp IR Distance Sensor:
Pin diagram of Sharp IR Distance Sensor
Distance vs Output Voltage:


From this graph you can know that the sensor is suitable for distances from 10cm to 60cm and the characteristic is non linear.

For longer distance measurement you can use GP2Y0A02YK it can measure upto 20cm to 150cm

Step 1: Circuit connection
Vo - Analog output voltage
Vcc - +5V Supply In
GND - Ground
Sharp IR Distance sensor interface with arduino

Step 2: Arduino code for sharp distance sensor
/*
This tutorial Demonstrates the use of Sharp Distance sensor
 
 This sketch prints "circuits4you.com" to the LCD
 and shows the distance.
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 1K resistor:
 * One end to ground
 * Another end to LCD VO pin (pin 3)
 
 This example code is in the public domain.
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("circuits4you.com");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print("Distance:");
  // print the distance value
  lcd.print(analogRead(A0));
  lcd.print("mm");
}


Step 3: Testing
1. To test the code turn on the circuit
2. observe the reading on LCD
3. The lcd will show the proportional analog value

No comments:

Post a Comment