IR Emitter and Detector

About 2 years ago I worked for a company that made hardware and went on a RadioShack buying spree.  I bought an IR Emitter and Detector, but didn’t know anything about hooking them up.  Over the couple of weeks I’ve gotten back into wanting to do some cool stuff with electronics including sending some sort of signal to a device via IR.  Eventually, I’d like to hook this up via some ATTinys that I bought about the same timeframe.  In any case, I wrote a quick piece of code that was giving me some info, then I decided to see what else was out there.  After all, I’d accomplished figuring it out on my own – someone else probably knew better about how to wire these puppies up.  I was right.

Now, I just need to decide how I’m going to proceed with my project.  I also found this blog location to format my code (http://codeformatter.blogspot.com/).

Here’s the Arduino topic that contains the code I’m now modifying (http://forum.arduino.cc/index.php?topic=94690.0).

I’m pasting the code (from the forum post above) here, just in case that forum ever dies for some reason – I have modified the original code somewhat.  My output pin is 8 and my input pin is 7 (the original code is 2 and 0, respectively).  I also added a line to turn off the LED at the end of the loop – (on my board I have the LED out at pin 9) – so that the LED will flash each time a TX is happening.

 /*---------------------------  
IR LED and phototransistor beam test and calibration
debug stuff:
min/max val = whatever the value is when it changes
LED is always on
----------------------------*/
// defines for pins
#define IRoutputPin 8 // digital output pin
#define IRinputAnalogPin 7 // analog input pin
#define LEDsignalPin 13 // LED signal pin
// int and long values
int val = 0; // variable to store the value read from input pin
int minVal = 1000;
int maxVal = 0;
int oldVal= 0;
// strings
String outputString;
String intro = "JTD IR test and calibration init";
boolean needIntro = true;
String signalBase = "Signal Strength: ";
String minBase = "Min: ";
String maxBase = "Max: ";
void setup()
{
pinMode(IRoutputPin,OUTPUT);
digitalWrite(IRoutputPin,HIGH);
pinMode(LEDsignalPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// print header
if (needIntro == true)
{Serial.println(intro);
needIntro = false;}
digitalWrite(LEDsignalPin, HIGH); // turn signal LED on
// read value of analog input
val = analogRead(IRinputAnalogPin); // read the input pin
//Serial.println(val); // debug value
if (val != oldVal) // if the new value is different than the old value, then do stuff
{
String outputString = signalBase + val; // concatenates output for signal strength
Serial.println(outputString);
outputString = minBase + minVal; // concatenates output for min
Serial.println(outputString);
outputString = maxBase + maxVal; // concatenates output for max
Serial.println(outputString);
oldVal = val;
digitalWrite(LEDsignalPin, LOW); // turn the signal LED off - this way the LED flashes when there's a transmit happening -- timo
delay(500); // wait half a second
}
if (val < minVal) // conditional if val is less than prior minVal
{ //String outputString = minBase + val; // concatenates output
Serial.println(outputString);
minVal = val;}
if (val > maxVal) // conditional if val is greater than prior maxVal
{ String outputString = maxBase + val; // concatenates output
//Serial.println(outputString);
maxVal = max(maxVal, val);}
}

This is a second sketch I found later on that incorporates a speaker.

 //  
// IR_Sensors_2.ino -- play the current value of an IR Sensor on a 5v speaker, adjusting
// from a base pitch of C3 (220hz)
//
// Dervied from http://arduino.cc/forum/index.php?topic=94690.0
//
// Note: Detector is clear, emitter is dark
//
//
// Constants
const int IR_IN_ANALOG_PIN = 0; // analog input pin -- Detector (clear)
const int IR_OUTPUT_PIN = 2; // digital output pin -- Emitter/dark)
const int SPKR_PIN = 8; // Speaker
const int NOTE_C3 = 220;
const String INTRO = "Starting...";
// IR level values
int irLevel = 0; // variable to store the value read from input pin
int minIRLevel = 1000;
int maxIRLevel = 0;
int lastIRLevel = 0;
int irTone = 0;
bool firstLoop = true;
bool debug = false; // Set false to turn off Serial tracing
bool newRange = false;
//
// setup
//
void setup() {
// Set up the output LED
pinMode( IR_OUTPUT_PIN, OUTPUT );
digitalWrite( IR_OUTPUT_PIN, HIGH ); // Turns on the emitter
// Turn on tracing
if ( debug ) {
Serial.begin( 9600 );
}
}
//
// loop -- show the current input to the detector. Sense and highlight changes to the observed
// min, max and avg values of the signal
//
void loop() {
// read value of analog input
irLevel = analogRead( IR_IN_ANALOG_PIN );
// Play its value
irTone = NOTE_C3 + irLevel;
tone( SPKR_PIN, irTone );
// print header & initial irLevel
if ( firstLoop == true ) {
if ( debug ) {
Serial.println( INTRO );
Serial.print( "initial irLevel = " );
Serial.println( irLevel );
}
firstLoop = false;
}
// Check if irLevel changed
newRange = false;
if ( irLevel != lastIRLevel ) {
// Trace the new IR level
if ( debug ) {
// Print the level
Serial.print( "irLevel = " );
Serial.print( irLevel );
// Print increase/decrease
if ( irLevel > lastIRLevel ) {
Serial.print( " +++" );
} else {
Serial.print( " ---" );
}
}
// Save new minimum
if ( irLevel < minIRLevel ) {
if ( debug ) {
Serial.print( " new MIN" );
}
newRange = true;
minIRLevel = irLevel;
}
// Save new maximum
if ( irLevel > maxIRLevel ) {
if ( debug ) {
Serial.print( " new MAX" );
}
newRange = true;
maxIRLevel = irLevel;
}
// Finish the trace line
if ( debug ) {
Serial.println( "" );
}
// Print new range
if ( newRange ) {
if ( debug ) {
Serial.print( "Range: [ " );
Serial.print( minIRLevel );
Serial.print( ", " );
Serial.print( maxIRLevel );
Serial.println( " ]" );
}
}
// Save the irLevel
lastIRLevel = irLevel;
// wait half a second
if ( debug ) {
delay( 500 );
}
}
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.