Get the IR sensor board at http://arduinotronics.blogspot.com/2017/06/6-channel-infra-red-transceiver-sensor.html
The Circuit:
The detection circuit is the same as the scale speedometer. A TCRT5000 IR LED / Phototransistor pair, with two resistors, connected to an Arduino input (D8). You can lower the value of the Phototransistor pullup resistor to 10k-50k ohm (instead of 80k-100k ohm) to reduce it's sensitivity to ambient light. Increase the value of the IR LED resistor (68 Ohm) to reduce range. Never drop IR resistor below 68 Ohms.
The output is a off the shelf LED crossbuck with 2 red LED's and a common anode. I put a 300 ohm resistor on the common and connected to 5v, and connected the two cathodes directly to arduino outputs D11 and D12. Add a second LED Crossbuck (with resistor) to the same outputs for the other side of the crossing. See the video below the code.
The code checks to see if the phototransistor is lit (a 0 or LOW), and activates the lights (LOW is lit, HIGH is off)) as long as it is.
//int sensor1 = 1;
void setup() {
Serial.begin(9600);
pinMode(8, INPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
int sensor1 = digitalRead(8);
Serial.println(sensor1);
if (sensor1 == LOW){
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
}
}
Finished board for two track sensors and 2 crossing lights.