This DCC project will be controlled by Raspberry Pi 3, A Sprog Controller and a Breadboard adapter. Please note you can use any controller supported by Rocrail.
We will be using IR breakbeams to detect where a train is on a circuit, and automatically tell the train to slow down, speed up or halt.
The first IR receiver is connected to GPIO 21
The second IR receiver is connected to GPIO 20
+5v to breadboard positive supply.
Gnd pins to breadboard ground supply.
The receivers use a 1M resistor to ground.
The transmitters use 220ohm or 330ohm resistors to ground. (you should experiment as to which one you need if possible)
import RPi.GPIO as GPIO
import time
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN)
GPIO.setup(20, GPIO.IN)
while True:
if(GPIO.input(21) == 1):
print("Beam1 Broken")
sys.exit()
if(GPIO.input(21) == 0):
print("Solid")
time.sleep(0.05)
if(GPIO.input(20) == 1):
print("Beam2 Broken")
sys.exit()
if(GPIO.input(20) == 0):
print("solid")
time.sleep(0.05)
The code on the left is saved as "Breakbeam2.py"
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.
Also, bad connections between dupont connectors and the breadboard or components will cause errors in the script. Make sure all connections are clean and seated well.
The script quits when one of the brakebeam LEDs are blocked. It achieves this with the sys.exit() command.
Please view the sub pages for instructions on using software.