Model Train Switch Control with an Arduino!

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 มี.ค. 2019
  • Welcome back everybody! First of all, if you haven't subscribed already, make sure you do to eligible for the 5,000 subscriber contest!
    Today we are controlling a snap switch with an arduino! Snap switches move the switch motor my powering one side or another of a motor and use magnetism to throw the switch.
    Arduino Sketch
    docs.google.com/document/d/1B...
    Here is what you will need
    Arduino UNO
    amzn.to/2FE0vL4
    Arduino Relay Shield
    amzn.to/2JUinH0
    12 volt power supply
    amzn.to/2U6kr3p
    (you will need 2 of these if you don't want to run your arduino off of USB)
    Dupont Wires
    amzn.to/2FFKHHF
    Bread Board
    amzn.to/2YHCefZ
    Button
    amzn.to/2HTfOD8
    Support the channel with some Merchandise!
    teespring.com/shop/diy-digita...
    You can contact me at diyanddigitalrr@gmail.com
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 51

  • @newsletterman
    @newsletterman 4 ปีที่แล้ว

    Looking to try to implement this instead of my Atlas switch machine MOM-off-MOM buttons. I want to include some LED's into the mix to indicate point direction too... Thanks for this tutorial.

  • @peterwhiting6303
    @peterwhiting6303 4 ปีที่แล้ว +1

    You can improve things with a latching push button.
    Adding an led to the button bias resistor gives an indicator.
    The required state is remembered through a power off sequence.
    And the code is simplified.

  • @JoeG-firehousewhiskey
    @JoeG-firehousewhiskey 5 ปีที่แล้ว +1

    Very cool, thanks for the information, these are great tutorials

  • @ariegt
    @ariegt 5 ปีที่แล้ว +1

    i do follow the wiring but maybe some people like to see a schematic. Especialy when in future there is more to wire like signalling or when sensors are added. Nothing fancy , the old pen&paper will do fine. Nice vid keep them comming thanks

  • @jimhyslop
    @jimhyslop 3 ปีที่แล้ว +5

    As a professional C and C++ programmer, I'd like to share some feedback on your sketch.
    First, remember that you are writing code for the person who's going to maintain it down the line. Even if that person is you! I can't tell you the number of times I've gone into code that I wrote only six months prior, and could not immediately see what was going on. To aid in that department, I'd like to offer some tips I've learned:
    - avoid using enums that differ only by a numeric suffix. I should be able to look at your enum list and understand immediately what each value is for. ST_OFF1 and ST_OFF2 do not immediately tell me what they are for.
    - Comments should document the "why", not the "what". I can see the "what" by reading the code: switchState = ST_TURN; does not need a comment.
    - Avoid "magic numbers". you nicely provided aliases Red, Green, and button for some I/O pins, but you left 6 and 7 as "magic numbers".
    On the code itself, I can see some room for improvement. You're tracking four states, when you really only need to track 3: set turnout to Mainline, set turnout to Diverge, wait for person to let go of button. Also, in the setup() function, ensure that the turnout matches the state. As it is right now, your code assumes that when it powers up, the turnout is in the mainline (straight) position - if you power up with the turnout in the diverge position, then you'll have to push the button twice to get it into the correct position.
    I'm not (yet) familiar with Arduino programming, but you should not need to write the same value to the same output twice. In switchturn you call digitalWrite(7, LOW) twice. Unless there's some quirk of the Arduino that I'm not aware of, only one is necessary.
    My version would look something like this (warning: uncompiled and untested code, for illustration purposes only):
    int mainlineRelay = 6;
    int divergeRelay = 7;
    int button = A0;
    bool isMainline;
    void setMainline()
    {
    digitalWrite(mainlineRelay, HIGH);
    digitalWrite(divergeRelay, LOW);
    delay(200);
    digitalWrite(mainlineRelay, LOW);
    isMainline=true;
    }
    void setDiverge()
    {
    // left as an exercise to the reader :)
    }
    void setup()
    {
    // as per your original code, plus:
    setMainline();
    }
    void loop ()
    {
    if ( analogRead(button) > 500 ) {
    if ( isMainline )
    setDiverge();
    else
    setMainline();
    // Wait for operator to release the button
    while ( analogRead(button) > 500 )
    delay(100);
    }
    }
    One thing I'm not sure of, is if the Arduino likes embedded loops like the one I have waiting for the operator. If not, then you can change loop() to:
    bool isWaitingForButtonRelease=false;
    void loop()
    {
    if ( analogRead(button) > 500 ) {
    if ( ! isWaitingForButtonRelease ) {
    if ( isMainline )
    setDiverge();
    else
    setMainline();
    isWaitingForButtonRelease = true;
    }
    } else {
    isWaitingForButtonRelease = false;
    }
    }
    I hope you find this helpful.
    By the way, while I was analyzing the code I paused the video, and I kept trying to scroll up and down through the code. As you can guess, that didn't work :D
    Thanks for all the videos!
    Now, what was I looking for when I came across this video?

    • @jmporter34
      @jmporter34 2 ปีที่แล้ว

      I’d probably add an Assert somewhere that both relays aren’t powered simultaneously.

  • @LZACCARO
    @LZACCARO 4 ปีที่แล้ว

    Nice your explanation about set up Arduino !!!! Thank you :-)

  • @nesr8786
    @nesr8786 5 ปีที่แล้ว

    Great presentation Jimmy ,i use a Mega so i get more I/O pins ,keep em coming buddy ,Much appreciated ,Take care till next time ...Mac

  • @rinjk3a
    @rinjk3a 5 ปีที่แล้ว +1

    Jimmy, you can do that with just one relay. the open close of a relay can change which side of the relay is being sent. So you can have up to 4 switches on one shield. Josh

    • @peterwhiting6303
      @peterwhiting6303 4 ปีที่แล้ว +1

      If you do it with one relay you leave the point motor permanently powered.

  • @gamepad3173
    @gamepad3173 ปีที่แล้ว

    I've thought about that idea, but I would rather do it automatically like install an IR sensor for when a train (or trains) are coming near and the arduino throws the switch or switches. That way the trains can proceed to the next model country.

  • @oceancity1042
    @oceancity1042 3 ปีที่แล้ว

    Hi,
    I have mastered the project so far , but you mention controlling indication LEDs at the beginning of your sketch, then there is no further information. Has there been any progress on this you can share ?
    Regards Robert

  • @sebsi23
    @sebsi23 ปีที่แล้ว

    Apologies in advance for a couple of dumb question from a total newbie...but here goes! My Bachmann turnout comes with a button and a wire to run it from a power supply. When connected, it does exactly what's depicted in this video but without the Arduino. What is this Arduino setup doing differently than my non-Arduino setup? Do some turnouts (such as the one in this video) not include a switch and power hookups? If that's the case, it seems like overkill to use a computer to handle this kind of task. Again, sorry for the dumb questions and thanks for the video.

  • @ajsettlemyer3646
    @ajsettlemyer3646 5 ปีที่แล้ว +1

    Good morning Jimmy, I enjoyed that video. I'm one for running as much "stuff" on any given Arduino as I can. I used your Track Signaling video to do my own thing and have gotten two sets of signal lights to run in the same sketch on a Nano. One is a two light tower set and the other is a three light tower set.
    So what I'm getting at here is to control as many turnouts as possible with as little electronics as possible. NESR says he using a Mega, would that allow for more relay connections for more turnouts? Can the relay cards be stacked? Hmmmmm, lots of food for thought.
    Thanks.

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  5 ปีที่แล้ว +1

      So I used the relay shield because of its ease of use, but you can hook up relays to the arduino that arent on a shield and then use as many as you have pins for! you just need 2 relays per turnout.

    • @ajsettlemyer3646
      @ajsettlemyer3646 5 ปีที่แล้ว

      @@DIYDigitalRailroad What you're saying is that I can get any regular relay board and have at it? :)

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  5 ปีที่แล้ว +1

      @@ajsettlemyer3646 exactly!

    • @ariegt
      @ariegt 5 ปีที่แล้ว

      take a look at this ..... th-cam.com/video/mSlXNcmUjZE/w-d-xo.html
      imagine you have 4 of these relay boards. if you look into i2c you can use even a arduino nano to drive them all

  • @jmporter34
    @jmporter34 2 ปีที่แล้ว

    Thanks for all the good info. A question - at about 1:00, you say "12V power supply with adapter". Is it 12V AC or DC ? What's the current rating ?

    • @jacobew2000
      @jacobew2000 ปีที่แล้ว

      12VDC If you have not found it.

  • @IndyMAB1964
    @IndyMAB1964 3 ปีที่แล้ว

    Two questions:
    1. beginning at 3:10, since there are three connections per relay, why can we not just use the third connection of "Relay 2" to switch the track from the turn to the straight? If you have a link that can explain the 4 Channel Relay Shield, I am okay with that.
    2. at 15:05, I understand that the analog input can be a number between 0 and 1024, but how does the program sense that from the button?

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  3 ปีที่แล้ว +1

      Great questions. So the way the snap switch works is it has a common wire with 2 wires that power a coil that "snaps the mechanism back and forth. The motor only needs a brief burst of power to switch. If the power remains constant, it will overheat and may melt. I use the relays to briefly cut the power on and off. One relay does the straight, and one does the diverging. If I used one relay, it would flip back and forth between straight and diverging, the power would stay constant and would eventually overheat.
      In terms of the button. The analog input is basically reading the power that is flowing through it. Buttons are momentary switches. When a button is pressed, it changes the amount of power flowing through the circuit and the analog input will read something other than it's normal power input. All you have to do is tell the program what the threshold is to activate the program.

    • @IndyMAB1964
      @IndyMAB1964 3 ปีที่แล้ว

      @@DIYDigitalRailroad Thank you for the help!

  • @MrGarthah
    @MrGarthah 4 ปีที่แล้ว

    Is there another relay board we can use as the one you list won't ship to Canada

    • @jimhyslop
      @jimhyslop 3 ปีที่แล้ว

      Garth, I found this at the Arduino store; looks like it should work exactly the same way: store.arduino.cc/usa/4-relays-shield

  • @richthomason4306
    @richthomason4306 4 ปีที่แล้ว +1

    I'm planning to use this for my turnouts, but how many turnouts can I hook up to 1 relay shield. I was thinking 2 to 4 turnouts per shield ? Or do i have to buy 1 relay per turnout. As you can tell I'm a beginner at this.

  • @captainchaos3174
    @captainchaos3174 5 ปีที่แล้ว +3

    Very interesting. Can you only throw one turnout? Do you need additional relay boards to control more than one turnout?
    Edit: Just saw your reply to AJ. You answered my question.

    • @gannonremington9167
      @gannonremington9167 2 ปีที่แล้ว

      a tip : you can watch movies on InstaFlixxer. I've been using it for watching a lot of movies these days.

    • @bentonjoseph6546
      @bentonjoseph6546 2 ปีที่แล้ว +1

      @Gannon Remington yea, been using InstaFlixxer for years myself :D

    • @musaalonso3119
      @musaalonso3119 2 ปีที่แล้ว

      @Gannon Remington Definitely, I have been watching on InstaFlixxer for since november myself :D

  • @andrewclarke406
    @andrewclarke406 3 ปีที่แล้ว

    I just have an 8 channel relay board can I use that instead of a relay shield?

  • @microplumb
    @microplumb 4 ปีที่แล้ว

    Hi anyone can answer me, if it possble to add Position LED on this project... thanks!

    • @oceancity1042
      @oceancity1042 3 ปีที่แล้ว

      Micro MultiServices
      Hi , would be interested if you ever got an answer , as I am at this stage now. Thank you

  • @normanrowe2831
    @normanrowe2831 5 ปีที่แล้ว

    Hi, first of all, thanks for sharing. You, amongst many others, are very much into Arduino products. This, though, seems like a lot of effort. Maybe you can explain to us ignorant MRR folk, i.e. old guys, why go to all this effort. What is the underlying reason? Thanks in advance for your reply/comment.

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  5 ปีที่แล้ว +1

      That's a fair question, and I am one who likes to manually throw turnouts. I am working on a more complex project that requires a precision throw of a turnout at a specific time. This will allow me to do that. My tutorials build upon eachother. This is the beginning of a larger project.

    • @normanrowe2831
      @normanrowe2831 5 ปีที่แล้ว +1

      DIY & Digital Railroad, a very fair answer. Thank you for explaining. I look forward to learning more. Later

    • @peterwhiting6303
      @peterwhiting6303 4 ปีที่แล้ว +1

      I use Arduino Nanos with Mosfets instead of relays. ( It's cheaper). 6 turnouts ( points) or semaphor signals per nano. Nanos sited at various places on layout. Comms from central control permits remote route selection. I select the route, the arduinos choose and switch the correct turnouts.

    • @jameswise7701
      @jameswise7701 3 ปีที่แล้ว

      I would offer one other reason to use this method rather than just a momentary push button switch. Using the Arduino and his code will provide protection against a user pressing the momentary switch "too long" and potentially damaging the switch relay. The code could be improved to look for the push button to be released before reading another input which should prevent burnout of the switch relay.

  • @TheSwitchList
    @TheSwitchList 5 ปีที่แล้ว

    Hi Jimmy. Another enjoyable video. Since any microcontroller, like he ATMega 328 on the Uno, has a limitied amount of program space (and memory space) I optimized your code to get the size down as low as possible (both program space and memory space). I have not tried this yet as I am still at work (don't tell the boss I was watching a DIY & Digital TH-cam video while still on the clock!). The sketch compiles without errors (checked at www.arduino.cc using the web based IDE) and I will let you know if it actually works when I get home and can give it a go on my Uno. Thanks again for all the great videos you post.
    Rick
    #define Red 12 //these first 2 will not affect the program but will let you hook up signal lights
    #define Green 13 //Hook up the lights to pins 12 and 13
    #define button A0
    #define Relay1 6
    #define Relay2 7
    void setup(){
    pinMode(Relay1, OUTPUT); //establishing Pin 6 as output, controls a relay
    pinMode(Relay2, OUTPUT); //establishing Pin 7 as output, controls a relay
    pinMode(button, INPUT); //establishes button as input
    pinMode(Green, OUTPUT); //establishes pin 13 as output
    pinMode(Red, OUTPUT); //establishes pin 12 as output
    digitalWrite(Relay1, LOW); // make sure Realy1 is off
    digitalWrite(Relay2, LOW); // make sure Realy2 is off
    }
    enum SWITCHSTATES
    {
    ST_OFF1,
    ST_OFF2,
    ST_STRAIGHT,
    ST_TURN,
    };
    SWITCHSTATES switchState=ST_OFF1; // Set initial state to ST_OFF1
    void loop(){
    int buttonRead=analogRead(button);
    delay(200);
    if (buttonRead > 500) { // if button pressed then
    switch(switchState) // process the button press
    {
    case ST_OFF1: // if current state is ST_OFF1 then set the swtich to straight
    switchstraight(); //sets up changes to ST_STRAIGHT
    break;

    case ST_OFF2: // if current state is ST_OFF2 then set the switch to turn
    switchturn(); // sets up changes to ST_TURN
    break;
    }
    }
    }
    void switchturn(){
    digitalWrite(6, HIGH); //activates relay 1
    delay(200);
    digitalWrite(6,LOW); //turns off relay 1
    switchState=ST_OFF2; //chages to ST_OFF2
    }
    void switchstraight(){
    digitalWrite(7, HIGH); // activates relay 2
    delay(200);
    digitalWrite(7,LOW); //turns off relay 2
    switchState=ST_OFF1; //chages to ST_OFF1
    }

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  5 ปีที่แล้ว +1

      I'll have to try it. Thanks!

    • @TheSwitchList
      @TheSwitchList 5 ปีที่แล้ว

      @@DIYDigitalRailroad One bug - need to swap the switchStates test in the switch statement. First case should be ST_OFF2 and the second one should be ST_OFF1. Other than that it seems to be working just fine. I had fun playing with this - thanks again Jimmy!

  • @oceancity1042
    @oceancity1042 3 ปีที่แล้ว

    hi,
    just uploaded your sketch , to arduino and relay board nothing else connected am getting this error message . Any ideas?
    - regards Robert
    Arduino: 1.8.13 (Mac OS X), Board: "Arduino Uno"
    Sketch uses 1238 bytes (3%) of program storage space. Maximum is 32256 bytes.
    Global variables use 11 bytes (0%) of dynamic memory, leaving 2037 bytes for local variables. Maximum is 2048 bytes.
    Problem uploading to board. See www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

  • @brucewoods9377
    @brucewoods9377 2 ปีที่แล้ว

    I find this very hard to follow without a schematic diagram

  • @sonysnapper
    @sonysnapper 5 ปีที่แล้ว

    Maybe I'm missing something, why go to this effort and expense to do what a simple switch can do? I suppose it is part of a larger scheme.

    • @DIYDigitalRailroad
      @DIYDigitalRailroad  5 ปีที่แล้ว +2

      That is a great question actually, and I totally understand, I actually manually throw all of my turnouts on my layout. Arduinos are really great for automation and custom control setups. With this, you can automate turnouts to sensors, or create custom control boards on your fascia. You are a good guesser though, this is part of a larger project that I am working on that does require an automated turnout.

    • @ariegt
      @ariegt 5 ปีที่แล้ว

      @@DIYDigitalRailroad mmmm , throw 3 switchpoints with one push button and control the signalling as well...