SparkFun: QRD1114,Optical Detector/Phototransistor

From eLinux.org
Revision as of 02:00, 1 October 2012 by Ngop (Talk | contribs)

Jump to: navigation, search


Overview

The QRD1114 is a Optical Detector/Phototransistor that can be purchased from SparkFun. The datasheet describes it as:

This sensor uses an infrared emitted diode combined with an infrared phototransistor to detect the reflected infrared signal. Ideal for sensing black-to-white transitions or can be used to detect nearby objects (.5-1cm).

Inputs and Outputs

The QRD1114 takes a supply voltage (Vs) of 0-5 V, while outputs via serial terminal.

Connecting to the Bone

The QRD1114 connects directly to the Beaglebone via one of the Analog In pins namely ain0-ain5. Also, with the other legs of the device connected to VDD_1.8V and GND respectively.

Code

Sample C Code


/****************************************************************
****************************************************************/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>


/****************************************************************
* Constants
****************************************************************/
#define MAX_BUF 64

/****************************************************************
* Global Variables
****************************************************************/
int keepgoing = 1;

void signal_handler(int sig)
{
	printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
	keepgoing = 0;
}
	
/****************************************************************
 * read_ain
 ****************************************************************/
int read_ain(char* ain){
	FILE *fp;
	char ainPath[MAX_BUF];
	char ainRead[MAX_BUF];

	snprintf(ainPath, sizeof ainPath, "/sys/devices/platform/omap/tsc/%s", ain);

	if((fp = fopen(ainPath, "r")) == NULL){
		printf("Cannot open specified ain pin, %s\n", ain);
		return 1;
	}

	if(fgets(ainRead, MAX_BUF, fp) == NULL){
		printf("Cannot read specified ain pin, %s\n", ain);
	}
	
	fclose(fp);
	return atoi(ainRead);	
}

/****************************************************************
 * Main
 ****************************************************************/
int main(int argc, char **argv){

	int ainPin;
	char ain[MAX_BUF];
	float duty_cycle = 0;
	float avgDutyCycle = 0;
	int avgCount = 1;
	
	if (argc < 2){
		printf("Usage: ./MiniProject02 <ainpin>");
		exit(-1);
	}

	signal(SIGINT, signal_handler);
	ainPin = atoi(argv[1]);
	snprintf(ain, sizeof ain, "ain%d", ainPin);

	while (keepgoing) {
		usleep(100000);
		duty_cycle = read_ain(ain);
			printf("                                 \r");
			printf("Value:  %d\r\n",(int)duty_cycle);
			fflush(stdout);
		
	}
	
	fflush(stdout);
	return 0;
}

You must specify the ain pin number as the first argument when executing this code after it is compiled.

Results: From observation, the darker the room, the higher the value that is outputted and vice versa the lighter the room, the lower the value is that is outputted.