Please note that User Registration has been temporarily disabled due to a recent increase in automated registrations. If anyone needs an account, please request one here: RequestAccount. Thanks for your patience!--Wmat (talk)
Please email User:Wmat if you experience any issues with the Request Account form.

SparkFun: QRD1114,Optical Detector/Phototransistor

Revision as of 16:05, 3 October 2012 by Yoder (Talk | contribs)

Jump to: navigation, search

Overview:  1, show a picture of the device
Wiring:   0, Give a specific example of how to wire it.
Code:     2
git/Compiles with make: 0 put in git
Demo:     0
Total:    3/10
Comments:  More details are needed.  I think someone else in the class would have trouble
reproducing what you have done.


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.


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);
	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>");

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

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

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


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.