# Model railroad crossing gates

**URL:** <https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253>\
**Category:** General Guidance\
**Created:** [November 7, 2023, 3:39am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253 "2023-11-07T03:39:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnd1024](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnd1024/32/1015147_2.png) [@johnd1024](https://forum.arduino.cc/u/johnd1024)\
**Post date:** [November 7, 2023, 3:39am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/1 "2023-11-07T03:39:56Z")

</div>

Ok  
I'm a novice at writing code and I don't have the vocabulary for this

However I know what I want my Arduino Uno to do

I am a model railroader and I want it my train to pass over a sensor and turn something on hold it on after it passes until it crosses a second one to turn it off

And I'm getting frustrated I don't know how to do it

I not sure I know how to hook it up but the code has me stumped

please be patient i have never programed anything in my life

Please HELP  
John

Any help would be greatly appreciated!

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [November 7, 2023, 5:34am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/2 "2023-11-07T05:34:26Z")

</div>

Hello [johnd1024](https://forum.arduino.cc/u/johnd1024)

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started.

Keep it simple and stupid firstly.

Divide et impera

Follow the example code that comes with the library or  
run some tutorials for the hardware selected.  
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

> **[Built-in Examples | Arduino Documentation](https://docs.arduino.cc/built-in-examples/)**
>
> Learn the basics of Arduino through this collection tutorials. All code examples are available directly in all IDEs.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [November 7, 2023, 6:01am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/3 "2023-11-07T06:01:10Z")

</div>

I moved your topic to an appropriate forum category @johnd1024.

In the future, please take some time to pick the [forum category](https://forum.arduino.cc/categories) that best suits the subject of your topic. There is an " **About the \_\_\_\_\_ category**" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in [the " **How to get the best out of this forum**" guide](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [November 7, 2023, 6:20am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/4 "2023-11-07T06:20:47Z")

</div>

Please list what hardware you’re planning to use…

I’m guessing a small R/C servo, an AVR based Arduino (Uno, nano etc), and whichever sensors you choose..

Please list parts and links to the web pages for your products.

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [November 7, 2023, 7:07am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/5 "2023-11-07T07:07:56Z")

</div>

When You have got it working maybe You want to make it like on the real railroad?  
Real gates go on in good time before the train enrers the crossing, and they go up when last part of the train has left the crossing,

---

<div class="post-metadata">

**Author:** ![outbackhut](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/outbackhut/32/1099892_2.png) [@outbackhut](https://forum.arduino.cc/u/outbackhut)\
**Post date:** [November 7, 2023, 7:15am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/6 "2023-11-07T07:15:02Z")

</div>

I wrote some code for a model railroad crossing lights, maybe you can adapt the code for this. Wouldn't be hard, all the triggers are setup already, it's just a matter of what the triggers trigger.

```arduino
const int IRPin1 = 2;
const int IRPin2 = 3;
const int LED1 = 10;
const int LED2 = 11;
const int Output = 7;

int train = 0;

unsigned long previousMillis = 0;
unsigned long prevMillis = 0;

const long interval = 7000;

boolean ledState = false;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Output, OUTPUT);
  pinMode(IRPin1, INPUT);
  pinMode(IRPin2, INPUT);
  digitalWrite (Output, HIGH);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 500) {
    previousMillis = currentMillis;

    ledState = !ledState;
    digitalWrite(LED1, ledState);
    digitalWrite(LED2, !ledState);
  }
  if ((!digitalRead(IRPin1) == 1) || (!digitalRead(IRPin2) == 1)) {
    train = 1;
  }
  if (train == 1) {
    digitalWrite (Output, LOW);
    unsigned long currMillis = millis();
    if (currMillis - prevMillis >= interval) {
      prevMillis = currMillis;
      digitalWrite (Output, HIGH);
      train = 0;
    }
  }
  if (train == 0) {
    digitalWrite (Output, HIGH);
  }
}

```

I don't think that is the final code iteration, but I seem to have lost the final one in the depths of my many hard drives. But that should give you a good start!

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [November 7, 2023, 11:38am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/7 "2023-11-07T11:38:41Z")

</div>

Dr. Geoff Bunza has done an excellent series of model Railroad-related Arduino projects. They're published on the Model Railroad Hobbyist Forum. Here's a link to the index he put together of his projects. You can ask about specifics here, but you can also approach him directly by joining that forum and posting to a particular thread, or actually direct message him.  
Not trying to chase you away, but Happy Reading, and good luck!

> **[Geoff Bunza's Blog/Journal Index](https://forum.mrhmag.com/post/geoff-bunzas-blogjournal-index-12218222?highlight=bunza%20crossing%20arduino&pid=1330943429)**
>
> Aug 03, 2022 · Edited Geoff Bunza 's Journal/Blog Index 
> 
> SMA47 WILD1: Wireless On-Board for DC and DCC Locos and Devices from any Browser An Operating Steam Throttle You Can Customize SMA46 Comments on 3D Printing the Steam Throttle and...

(sorry, got distracted reading, forgot to insert link!)

---

<div class="post-metadata">

**Author:** ![theeccentricgenius](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/theeccentricgenius/32/993275_2.png) [@theeccentricgenius](https://forum.arduino.cc/u/theeccentricgenius)\
**Post date:** [November 7, 2023, 11:49am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/8 "2023-11-07T11:49:59Z")

</div>

~~There is no obvious link.  
Sorry, I tried to find it but couldn't.~~

Thank you....I'll leave this post here for future readers.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 7, 2023, 12:45pm UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/9 "2023-11-07T12:45:04Z")

</div>

> [@johnd1024](#):
>
> I am a model railroader and I want it my train to pass over a sensor and turn something on hold it on after it passes until it crosses a second one to turn it off

Will this idea work? Pseudocode:

```arduino
    If train triggers sensor1
         Turn on whatever

    If train triggers sensor2
        Turn off whatever

```

  

Right there in your loop.

a7

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 7, 2023, 9:10pm UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/11 "2023-11-07T21:10:58Z")

</div>

Practice your train code on Wokwi... (here's a pedestrian-facing simulation, not train-facing)

> **[RRX - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/370078430529203201)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![johnd1024](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnd1024/32/1015147_2.png) [@johnd1024](https://forum.arduino.cc/u/johnd1024)\
**Post date:** [November 14, 2023, 12:47am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/12 "2023-11-14T00:47:53Z")

</div>

Thank you all so very very much for responding with your help and suggestions.

I found a you tube video entitled "Crossing gate Project for Arduino" and through that was able to understand the code and get the result i needed.

I also understand now through all you help in the forum to activate another pin.

The code seems to be simple and easy if you just takr the time to read it and stay with it even for a novice such as me.

like i said at the beginning this is the first time i have ever tried anything like this, this is so far out of my wheelhouse i never thought i would get it.

actually I'm having more trouble navigating thorough this forum than writing code. . .LOL. but I will get it!  
Thank you all again for your support, help and patience.

John.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 12, 2024, 12:48am UTC](https://forum.arduino.cc/t/model-railroad-crossing-gates/1186253/13 "2024-05-12T00:48:42Z")

</div>

Closed on May 12, 2024, 12:48am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
