Project showcase

Using IR & Hall Type Sensors for Train Detection © Apache-2.0

Reliable and proven sensors designs. Example of code. Recommendations for use.

  • 9 views
  • 0 comments
  • 0 respects

Components and supplies

Ard nano
Arduino Nano R3
You can use any Arduino board
×1
Allegro a1324lua t image
Hall Effect Sensor
×2
Infrared Module (Generic)
×2

Apps and online services

About this project

Hello friends! I came up with a project for managing model railways using the Arduino boards and mobile apps. This project is called URB (Universal Railway Bus). You have given me support in the development and distribution of this project, for which many thanks. It made me be more responsible about the quality of my content. So I deleted all my old posts and now I will publish their better remakes.

In my project, I came up with the concept of the Open Protocol, which unifies and standardizes Arduino sketches and allows you to combine several Arduino boards into a "network". This solution allows you used devices stand-alone behavior, or manage them from any remote control, or even more fun with multiple consoles at the same time.

If you do not just use Arduino as a trains control system, but apply it to manage the entire railway layout – then there will be many new features. This is the management of switches and signals, and local and general lighting with many light effects and much more. But the most interesting is the automation of your layout.

The necessary attribute of automation are sensors. And their options are very many (ultrasonic, pressured, current and voltage levels, gyro sensors and so on). In most cases, modelers use infrared, light detection and magnetic reed sensors because they are very cheap and simpliest.

An infrared sensor

However, infrared sensors have many problems with false triggering. On video presents the DIY IR module that solves these problems.

But the lights detection and IR detection sensors have one more problem - they are very large, and it is hard hidden them into the rail track. Therefore I prefer the Hall sensor.

A Hall effect sensor

In comparison with other sensors, the Hall sensor has the advantage of a localized triggers dot. The second element — the magnet — is attached to any metal part underside of a car or a locomotive (for example, to the fastening screw, etc.). I recommend small cylinder neodymium rare earth magnets. By changing their number, you can adjust the distance between the magnet on the car and the sensor on the rails. I have reliable operation at distances of 1-5 mm. Hall sensors are small, if they are neatly put between the rails, then they are like real AWS inductor.

This allows you to correctly and accurately set the pickup location relative to the train and relative to the location on the layout, as well as easily adjust it by moving the magnets. You can place a magnet on any car or locomotive, at the beginning, middle or end of the train, thus adjusting the stopping place to within a centimeter. That is, it is difficult to move the sensor on rails itself, but the magnet on bottom of loco or cars of train is very easy to be moved.

Programming Arduino based on sensors signals

You certainly know what to get the state of a digital sensor is very simple:

if (digitalRead(ARDUINO_PIN) == HIGH) { // or LOW - depends on the type of the sensor
 // do something
}

There are many sensors with complex internal processing and data transfer to microcontrollers. It is also possible to apply complex algorithms for processing signals from sensors in the microcontroller itself, using timing, multiple reads, delays, etc., the most famous example of such code is included in the Arduino IDE examples and is called Debounce. Also, some types of sensors have versions with a so-called latch.

Since in my project do not use complex code and I use simple sensors, in this chapter I'll describe how to implement the latch programmatically using boolean variables and I'll present what oportunities this simple system gives.

As an example, I will give an automatic switching of signals when passing a train. By adding relays or motor-driver modules to this system, you can get a simple interlocking railway system.

Each of the two sensors has two boolean variables – trigger and latch. Code for processing signals from sensors:

 if (digitalRead(SENSOR_1) == HIGH) {
   trigger_s1 = true;
   latch_s1 = true;
 }
 else trigger_s1 = false;
 if (digitalRead(SENSOR_2) == HIGH) {
   trigger_s2 = true;
   latch_s2 = true; 
 }
 else trigger_s2 = false;

Applying various algorithms for comparing these boolean variables from two or more sensors, you can get a lot of models of the behavior of trains, the state of lines (turnouts, signals) and other devices of your layout and your control system.

In this example, the algorithm detect a train on the track between the two sensors. Also it is symmetrical, that is, it works correctly when trains move in any direction.

 if (latch_s1 && latch_s2 && !trigger_s1 && !trigger_s2) {
   latch_s1 = false;    
   latch_s2 = false;     
 }
 if (latch_s1 || latch_s2) digitalWrite(LED_BUILTIN, HIGH);
 else digitalWrite(LED_BUILTIN, LOW);

When the track is busy, the built-in Arduino LED turns on. You can also use variations of this code to control the railway crossing.

Code

Train detect Arduino
Detect the train on the track by signals from two sensors
bool trigger_s1, trigger_s2, latch_s1, latch_s2; 

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT); 
}

void loop() {

  if (digitalRead(10) == HIGH) {
    trigger_s1 = true;
    latch_s1 = true;
  }
  else trigger_s1 = false;
    
  if (digitalRead(9) == HIGH) {
    trigger_s2 = true;
    latch_s2 = true; 
  }
  else trigger_s2 = false;  

  if (latch_s1 && latch_s2 && !trigger_s1 && !trigger_s2) {
    latch_s1 = false;    
    latch_s2 = false;     
  }

  if (latch_s1 || latch_s2) digitalWrite(LED_BUILTIN, HIGH);
  else digitalWrite(LED_BUILTIN, LOW);
  
}

Schematics

Train detect
Simple model rail track with two sensors
Sensor algorytm c3xsq67hwd

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 454,882 views
  • 42 comments
  • 239 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 289,085 views
  • 96 comments
  • 676 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 231,033 views
  • 38 comments
  • 241 respects

OpenCat

Project in progress by Team Petoi

  • 196,336 views
  • 154 comments
  • 1,364 respects
Add projectSign up / Login