This page will lay the foundations to get a Raspberry Pi 3 to control trains through means of controller software and signals from break beam IR diodes placed across the layout.
The IR receiver is connected to GPIO 21
+5v to breadboard positive supply.
Gnd pins to breadboard ground supply.
The Raspberry Pi above is connected to just two LED's. This is to get started with learning the necessary code and getting to grips with IR Breakbeam control.
The LED's used are 940nm Infra Red receivers (dark) and transmitters (clear). The receiver requires a 1M resistor to ground, the transmitter needs a 220ohm or 330ohm resistor to ground.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN)
while True:
if(GPIO.input(21) ==1):
print("Beam Broken")
if(GPIO.input(21) == 0):
print("Solid")
time.sleep(0.05)
The code on the left is saved as "IRBreakbeam.py" using a standard text editor such as nano.
You can run it with "sudo python IRBreakbeam.py" at the command line.
You should experiment with working out how far apart the led's can be, and which materials are most effective at blocking the IR flow.
Stray IR can contaminate the signal, stopping the beam from breaking.
Please note if you cut and paste the code into your system, you will need to check the formatting. Tabs and bad spaces can be generated during the process and these are rejected in Python and will cause errors.
Useful info
GPIO pin numbers | Resistor Calculator | Remote Raspberry Pi Setup