SIDST-GP2

From Curuxa

SIDST-GP2 is a distance sensor with analog output. You can use this module for measuring distance to objects located between 10 to 80cm.

For example, a robot could use SIDST-GP2 to detect obstacles or find objects.

Electronic circuit

Wiring

Bill of materials:

  • 1x Sharp GP2D-12 or GP2Y0A21YK
    • Be careful, any other model will not work: GP2D-02, GP2D-15, GP2Y0D21YK...
  • 6x Straight female headers (usually sold as 40-pin strips)
    • Note: usually headers are spaced 100mils, but GP2's are closer, you can't use standard 0.1 inch headers
  • Some thin wire
  • Some heat shrink tubing

This module doesn't require any kind of homemade electronic circuit. Just buy a GP2D-12 or GP2Y0A21YK and build a simple cable for connecting it to the Main Board.

Printable version: Single, Double, Quad

How it works

SIDST-GP2 will output a voltage level depending on the distance to the closest object put in front of it.

It can be powered from 4.5 to 5.5V, and can measure objects in the 10-80cm range.

The Main Board will receive a given voltage through pin #1 (the data pin). It should be converted using the microcontroller internal Analog-to-Digital Converter.

The distance to the object is inverse to the output voltage, for example:

  • 15cm - 1.65V
  • 25cm - 1.1V
  • 40cm - 0.75V
  • 80cm - 0.4V

All the information and possible output voltages can be found in its datasheet.

Construction

SIDST-GP2 SIDST-GP2 SIDST-GP2 SIDST-GP2

Test

The following code can be compiled using SDCC.

/*==================================================================
* Sample program for SIDST-GP2 using an MBP18
*
* Plug SIDST-GP2 to RA0/AN0 (pin 17) and LTIND-A to RB4 (pin 10).
* The LED will light up when there is an object closer than 25cm
*
* http://curuxa.org
*=================================================================*/
 
#include <MBP18.h>
#include <GP2.h>
 
ConfigBits1(_CP_OFF & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO);
 
#define LED RB4
#define LedON 1
#define LedOFF 0
 
void main() {
	TRISB4=0;
	EnableAN0();
 
	while(1) {
		//note: distance and ADC values are inversely correlated
		if(AdcMeasure() > GP2_25cm) LED=LedON;
                else LED=LedOFF;
	}
}


Snippets

/*==================================================================
* Snippet for SIDST-GP2
*
* Using a SIDST-GP2 connected to AN0 and a LTIND-A to RB4.
* The LED will light up when there is an object closer than 25cm
*
* http://curuxa.org
*=================================================================*/
 
#include <pic16fxxx.h>
 
// your config bits here...
 
// insert here code for making A/D conversion (see the snippets category)
 
//digital values for distances when Vcc=5V
//Warning: values and distances are inversely proportional, this means a value bigger than GP2_10cm is actually closer than 10cm
#define GP2_6cm  635
#define GP2_10cm 471
#define GP2_15cm 338
#define GP2_25cm 225
#define GP2_40cm 154
#define GP2_80cm 82
 
#define LED RB4
#define LedON 1
#define LedOFF 0
 
#define bool unsigned int8
#define true 1
#define TRUE 1
 
// Return true if the measured value is closer than a given distance
bool isCloserThan(int measure, int distance) {
    return measure > distance;
}
 
// Return true if the measured value is further away than a given distance
bool isFurtherThan(int measure, int distance) {
    return measure < distance;
}
 
void main() {
	TRISB4=0;
	EnableAN0();
 
	while(1) {
                int adcResult = AdcMeasure();
		if( isCloserThan(adcResult,GP2_25cm) ) LED=LedON;
                else LED=LedOFF;
	}
}