Block signals on an Arduino Uno
Three IR detectors at the block boundaries and two searchlight signals that show red for an occupied block, yellow when the next one is occupied and green when both are clear. The whole build, an Arduino Uno and 7 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
What you'll learn
- Automatic block signaling with three aspects
- Tracking occupancy from spot detectors
- Making yellow from a red and a green LED leg
- Waiting for a signal to stay quiet before acting
- Keeping state in arrays
- Difficulty: Advanced
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 10
How it works
Three slot detectors on pins 8, 7 and 6 sit at the entrances to blocks A and B and at the exit of B. A detector reads HIGH while a car breaks its beam. The sketch remembers which blocks hold the train. Past the last detector is block C, off the panel.
Each signal is a two-color LED. Signal 1 on pins 5 and 4 guards block A and signal 2 on pins 3 and 2 guards block B. It shows red if its block is occupied. It shows yellow, both legs lit, if its block is clear but the next one is not. Otherwise it shows green.
A block clears once the train has fully passed the next detector, which means that detector has been quiet for 1.5 seconds. Block C stays occupied for 8 seconds after the exit.
Parts list
10 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Power, 5 V
- 3 × Photo interrupter, slot
- 2 × Two-color LED, KY-011
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Photo interrupter, slot (1) pin GND | Arduino Uno R3 pin GND |
| Photo interrupter, slot (2) pin GND | Arduino Uno R3 pin GND |
| Photo interrupter, slot (3) pin GND | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 2 | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (2) pin 2 | Arduino Uno R3 pin GND |
| Photo interrupter, slot (1) pin SIG | Arduino Uno R3 pin 8 |
| Photo interrupter, slot (2) pin SIG | Arduino Uno R3 pin 7 |
| Photo interrupter, slot (3) pin SIG | Arduino Uno R3 pin 6 |
| Two-color LED, KY-011 (1) pin R | Arduino Uno R3 pin 5 |
| Two-color LED, KY-011 (1) pin G | Arduino Uno R3 pin 4 |
| Two-color LED, KY-011 (2) pin R | Arduino Uno R3 pin 3 |
| Two-color LED, KY-011 (2) pin G | Arduino Uno R3 pin 2 |
| Photo interrupter, slot (1) pin VCC | 5 V |
| Photo interrupter, slot (2) pin VCC | 5 V |
| Photo interrupter, slot (3) pin VCC | 5 V |
| Resistor, 220 Ω (1) pin 1 | Two-color LED, KY-011 (1) pin C |
| Resistor, 220 Ω (2) pin 1 | Two-color LED, KY-011 (2) pin C |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Block signals: two three-aspect signals following a train through two blocks.
//
// pin 8 detector at the entrance to block A (a beam sensor, HIGH while a car breaks it)
// pin 7 detector at the entrance to block B
// pin 6 detector at the exit of block B
// pins 5, 4 signal 1 at the entrance to A: the red and green legs of a bicolor LED
// pins 3, 2 signal 2 at the entrance to B, the same
//
// Each signal is one bicolor LED, the way many modelers build a searchlight
// signal: red, green, or both legs lit together for yellow.
//
// Automatic block signaling, the simple one-direction kind. Each signal guards
// the block in front of it:
// red that block is occupied: stop
// yellow that block is clear but the next one is not: approach, ready to stop
// green the next two blocks are clear
//
// Real layouts usually detect occupancy by current: a detector sees the small
// current a locomotive motor or a resistor wheelset draws anywhere in the block.
// Mokxi does not model track current, so these are spot detectors at the block
// boundaries, and the sketch remembers which block the train is in: a block
// becomes occupied when the train trips its entrance detector, and clears once
// the train has fully passed the next detector. Past the exit detector is block
// C, beyond this panel, which the sketch counts as occupied for C_CLEARS_MS.
//
// Click a detector's card into its slot to put a car in the beam, and out again.
const int DETECTOR_PIN[3] = {8, 7, 6};
const int SIGNAL_PINS[2][2] = {{5, 4}, {3, 2}}; // red leg, green leg
const unsigned long QUIET_MS = 1500; // a detector clear this long: the train has passed
const unsigned long C_CLEARS_MS = 8000; // how long block C stays occupied after the exit
bool occupied[3]; // blocks A, B and C
bool sawTrain[3]; // detector i has a train over it, or had one recently
unsigned long lastSeen[3];
unsigned long cEnteredAt = 0;
int shown[2] = {-1, -1};
const char *const ASPECTS[] = {"red", "yellow", "green"};
const char BLOCK_NAMES[] = "ABC";
void enter(int block) {
if (occupied[block]) return;
occupied[block] = true;
Serial.print("block ");
Serial.print(BLOCK_NAMES[block]);
Serial.println(" occupied");
}
void leave(int block) {
if (!occupied[block]) return;
occupied[block] = false;
Serial.print("block ");
Serial.print(BLOCK_NAMES[block]);
Serial.println(" clear");
}
void setSignal(int s, int aspect) {
if (shown[s] == aspect) return;
shown[s] = aspect;
// red lights the red leg, green the green one, yellow both
digitalWrite(SIGNAL_PINS[s][0], aspect != 2 ? HIGH : LOW);
digitalWrite(SIGNAL_PINS[s][1], aspect != 0 ? HIGH : LOW);
Serial.print("signal ");
Serial.print(s + 1);
Serial.print(": ");
Serial.println(ASPECTS[aspect]);
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) pinMode(DETECTOR_PIN[i], INPUT);
for (int s = 0; s < 2; s++) {
for (int i = 0; i < 2; i++) pinMode(SIGNAL_PINS[s][i], OUTPUT);
}
Serial.println("Block signals ready: blocks A and B, then C beyond the panel");
}
void loop() {
unsigned long now = millis();
for (int d = 0; d < 3; d++) {
if (digitalRead(DETECTOR_PIN[d]) == HIGH) {
if (!sawTrain[d]) enter(d); // the train is at the entrance to block d
sawTrain[d] = true;
lastSeen[d] = now;
if (d == 2) cEnteredAt = now;
} else if (sawTrain[d] && now - lastSeen[d] >= QUIET_MS) {
// The whole train has passed detector d, so it has left the block behind it.
sawTrain[d] = false;
if (d > 0) leave(d - 1);
}
}
if (occupied[2] && !sawTrain[2] && now - cEnteredAt >= C_CLEARS_MS) leave(2);
// Each signal looks at the block it guards and the one after.
for (int s = 0; s < 2; s++) {
if (occupied[s]) setSignal(s, 0);
else if (occupied[s + 1]) setSignal(s, 1);
else setSignal(s, 2);
}
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
enter sketch.ino
Marks a block as occupied and prints a message. If the block is already occupied, it does nothing, so the message prints only once.
void enter(int block) {
if (occupied[block]) return;
occupied[block] = true;
Serial.print("block ");
Serial.print(BLOCK_NAMES[block]);
Serial.println(" occupied");
}leave sketch.ino
Marks a block as clear and prints a message. It also ignores blocks that are already clear.
void leave(int block) {
if (!occupied[block]) return;
occupied[block] = false;
Serial.print("block ");
Serial.print(BLOCK_NAMES[block]);
Serial.println(" clear");
}setSignal sketch.ino
Sets a signal to red, yellow or green. Red lights the red leg, green lights the green leg and yellow lights both. It skips the work if the signal already shows that aspect, and prints the change.
void setSignal(int s, int aspect) {
if (shown[s] == aspect) return;
shown[s] = aspect;
// red lights the red leg, green the green one, yellow both
digitalWrite(SIGNAL_PINS[s][0], aspect != 2 ? HIGH : LOW);
digitalWrite(SIGNAL_PINS[s][1], aspect != 0 ? HIGH : LOW);
Serial.print("signal ");
Serial.print(s + 1);
Serial.print(": ");
Serial.println(ASPECTS[aspect]);
}loop sketch.ino
Reads the three detectors. A beam break marks a block as entered. A detector that stays clear for QUIET_MS means the train has passed, so the block behind it is cleared. Then each signal looks at its own block and the next, and sets an aspect.
void loop() {
unsigned long now = millis();
for (int d = 0; d < 3; d++) {
if (digitalRead(DETECTOR_PIN[d]) == HIGH) {
if (!sawTrain[d]) enter(d); // the train is at the entrance to block d
sawTrain[d] = true;
lastSeen[d] = now;
if (d == 2) cEnteredAt = now;
} else if (sawTrain[d] && now - lastSeen[d] >= QUIET_MS) {
// The whole train has passed detector d, so it has left the block behind it.
sawTrain[d] = false;
if (d > 0) leave(d - 1);
}
}
if (occupied[2] && !sawTrain[2] && now - cEnteredAt >= C_CLEARS_MS) leave(2);
// Each signal looks at the block it guards and the one after.
for (int s = 0; s < 2; s++) {
if (occupied[s]) setSignal(s, 0);
else if (occupied[s + 1]) setSignal(s, 1);
else setSignal(s, 2);
}
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A signal shows only one color, and yellow never appears This project
Yellow needs both the red and green legs lit. Check both pins of that signal. - A block never clears This project
A block clears only after the next detector has been tripped and then quiet forQUIET_MS. Pass the train through the next detector, then take it out. - A signal is dim or both legs look the same This project
Each LED's common pin goes through a 220 ohm resistor to GND. - The output never changes, or is always the same. The LED side needs a series resistor, and the phototransistor side needs a pull-up. Photo interrupter
Put about 220 ohms in series with the LED from 5 V. Pull the output up with about 10k, or use INPUT_PULLUP. Output goes low when the beam is clear and high when blocked, or the reverse on some modules. - Pulses are missed or counted twice at higher speeds because the loop is too slow or the edge is noisy. Photo interrupter
Count with an interrupt on the pin, on one edge only. Make sure the wheel slots are wide enough to pass the beam and the object fully blocks it. - No color shows or only one color works. Two-color LED
Find which leg is the shared one, usually the middle leg or the marked one. Drive the other two legs, each through its own resistor of about 220 ohms. - Orange is hard to get. Two-color LED
Mix the two colors with PWM on both pins at different levels. The brightness of each die differs, so tune the values by eye. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.