SparkFun: QRD1114,Optical Detector/Phototransistor

Jump to: navigation, search

Overview:  2
Wiring:    1, I'd rather see a picture with a bone
Code:     2
git/Compiles with make: 1, no makefile
Demo:     2
Total:    8/10
Comments:  More details are needed.  I think someone else in the class would have trouble
reproducing what you have done.
Optical Detector/Phototransistor - QRD1114


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

Wiring Layout for the Bone but with more information mentioned in "Connecting to the Bone" on the left.

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. (ain5 in this case) Also, with the other legs of the device connected to VDD_ADC(1.8V) and GND respectively.

Note: The only change thats is different from the image and on the bone: One of the legs is connected to the VDD_ADC(1.8V) pin on the bone (Pin 32) instead of the VDD_5V (pin 5) on the bone and the other shown is connected to GNDA_ADC (Pin 34) instead of the default pin 1.


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.

Below is the git repository in the MiniProject03 Directory that the code can be cloned from:

git clone


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.