I am a beginner in the process of making a break beam sensor.
I bought a couple TSOP 4038 receivers and have a couple LED/LED IRs.
Im looking for a basic schematic and code to test (make sure they work). I tried to search around and try a couple from youtube but have had no luck getting it going.
For a very basic test, you can use the tone library to generate a 38kHz carrier wave to drive the IR led (with series resistor say 220 ohms). Ideally, you should generate bursts of the carrier wave (say 600uS on 600uS off). The output of the TSOP4038 can drive ( actually sink) an indicator led via a 1k series resistor.
#define LEDon LOW
#define LEDoff HIGH
#define noSIGNAL HIGH
#define SIGNAL LOW
const byte TX = 3;
const byte statusLED = 13;
const byte RX = 2;
byte lastSensorStatus;
//timing stuff
unsigned long checkInputsMillis;
unsigned long detectedMillis;
//************************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(statusLED, OUTPUT);
digitalWrite(statusLED, LEDoff);
pinMode(TX, OUTPUT);
pinMode(RX, INPUT);
tone(TX, 38000);
} //END of setup()
//************************************************************************************
void loop()
{
//********************************************
//time to check the inputs (every 20ms) ?
if (millis() - checkInputsMillis >= 20)
{
//restart this TIMER
checkInputsMillis = millis();
checkInputs();
}
//********************************************
//other non-blocking code goes here
//********************************************
} //END of loop
//************************************************************************************
void checkInputs()
{
byte inputStatus;
//********************************************
inputStatus = digitalRead(RX);
//was there a change in sensor status ?
if (lastSensorStatus != inputStatus)
{
//update to the new state
lastSensorStatus = inputStatus;
//**********************
//has the signal disappeared ?
if (inputStatus == noSIGNAL)
{
digitalWrite(statusLED, LEDon);
//the time the object went into the IR beam
detectedMillis = millis();
}
//**********************
//the signal has returned
else
{
digitalWrite(statusLED, LEDoff);
Serial.print("Object was in beam for ");
Serial.print(millis() - detectedMillis);
Serial.println(" milliseconds.");
}
}
} //END of checkInputs()
//************************************************************************************