Thanks! i have been watching yr videos session for a long time. u do it very clear and slowly for anyone to follow. I started ardunio for train few weeks ago and I fine yr work very helpful.
Cool video Jimmy!! As an HO steel mill modeler I have been experimenting with similar Arduino automation schemes. I'd like to automate a small switcher dedicated to a coke oven battery (to move coke receiver car from oven door to quench tower) or dedicated to a blast furnace (to move a hot metal car from the furnace to other set locations in the mill). Steel mills have lots of these short runs that can be automated. Your example is perfect for these applications! Have three comments. First, some DCC engines will operate in DC mode as well. I've been using a HO Athearn EMD 40 switcher previously equipped with NCE DCC decoder. It's the right size for my steel mill applications. Glad I didn't have to buy another engine. Second, I'm using a higher voltage for HO scale ... 15V DC instead of 12. With a change of voltage, your automation method will work with many scales. I've made something similar work with T scale using 5V DC supply to motor driver. Third, using a random() function call, the time spent at any of your stations can be made slightly more realistic. Simply add (or subtract) a reasonable random value from your 5000 msec delay. All the best ... Happy Model Railroading ...
What's the reason for wiring the potentiometer as you've done, with the extra resistor? Usually you'd wire the outer legs of the pot to GND and VCC and the center leg to the analog input with no extra resistors. This standard voltage divider configuration gives a linear response from potentiometer position to the voltage output, and makes it possible to use the full 0 to 1023 range of the Arduino's ADC input, while the circuit you've shown gives what's called a "harmonic" response curve -- the ADC input value becomes extremely sensitive at one end, and much less sensitive at the other, while only reaching 94% of the full-scale voltage (with your resistor values).
Thanks so much for this video. I’ve just made it. Filling the extra add of a a station coding I’ve added 2 extra and changed the timings. I’ve gone for 6 seconds at each end and 3 seconds in the stops. Thanks again for your idiot proof video as I did it!!! First time I’ve tried anything like this
Excellent video Jimmy! This is a really helpful topic that will benefit those who have commuter trains they want to run independently as automatic start/stop/reverse etc. Cheers from Melbourne, Australia!
Thanks, Jimmy. This is exactly what I need to model the NJRT from the early 1900's on my upcoming new build! Timing could not be better! The link to the Kato Tram is an added extra to the arduino hardware and sketch. The tram is a close approximation to the NJRT Jewett cars!
cool video Jimmy, in the coding you should use sub voids in the code. like this, void loop(){ station1=analogRead(A0); station2=analogRead(A1); station3=analogRead(A2); speedval=analogRead(A3); speed=map(speeval,0,1023,0,255); analogWrite(3,speed); delay(50); if (station1
For station3, here's a better way to code it: if (station3 < 500) { // Direction doesn't matter for the 3 following lines digitalWrite(5, LOW); digitalWrite(6, LOW); delay(5000); if (Direction == 1) { digitalWrite(6, HIGH); // No need to set port 5 to LOW as it is already set to LOW } else { digitalWrite(5, HIGH); // No need to set port 6 to LOW as it is already set to LOW } // Direction doesn't matter for the 1.5 sec delay delay(1500); }
And since we use this multiple times for other potential "in between" stations it also is much more neater to put all of this in a function with stationN as argument. No need to copypasta stuff when you add a station and no spaghetti code.
I have a project I am working on. it is a g scale train in a store window christmas display. I want to add a push-button (a big "Easy" button from Staples, hopefully) that when pressed will make the train run for a certain amount of time and then stop until the button is pressed again. also, if the button isn't pressed for longer than 20 minutes, the train would run on it's own, once. I am hoping to learn enough from your channel to achieve this! thank you for posting!
Nice video. BUT! You have wired the potentiometer completely wrong - side legs got to gnd and vcc (which is which doesn't matter), middle leg is the output. No resistor needed
I always wanted to do that. My day dream is to run my bullet trains point to point while I do my switching.. My thought was to put the electronics in a lineside building so I could just push a button. Nicely done.
Thank you so much. With this I was able to make the rigi duo ski lift autonomous. It works great. And I know there will be more to make with this. Thanks again. Great teacher too.
I am currently building a future city layout with a bullet train that goes through different futuristic cities. I came across this and it was absolutely perfect for what I need. I just got a soundboard that will kick off train station chimes and information on arrivals and departures. I hope to get the sensors down the track a bit and time the train to slow into and out of the stations. Thanks!
I started an Arduino course a few weeks ago in order to be able to control my HO DC train layout. your videos are a great, super helpful for a 78 yrs old man. I m confused about what to start first: train control? I have 3 different DC controllers. Can I apply only on one line (controller) . from your experience, can you write to me, about what to do first and that second...and the next stages? many thanks Eytan Barak from Israel😇😍
Nice. However, based on the type of IR sensors you use, which have a built in comparator / trigger, you can use simple digital inputs for the station1, station2, station 3 detection. ALso, if you really want this to "go pro" you can create slow start and stop routines using variation in PWM signals. But hey, for a startere, this is a great example ! End you do explain it clearly and decently in easy-to-understand language, cudos for that !
Do you know of a sensor that could be attached to a toy train running in a loop and the sensor would be used to make the train stop once it detects an objection in front of it?
Very useful and your details in typing with patience great I got this realy you are a great teacher... God bless you always make such tutorial for lay man like me 🙏🙏🙏🙏👍
Hi Jimmy, I'm looking at doing a similar thing except adding in some turnout control and likely a second Tram. But, I have a couple of questions: 1) While I've used the Arduino for a few things, from what I see it "knows" the difference between the Analog 3 port and Digital 3 port. When I do an analogRead(A3 - it knows to read from the Analog port. But when I do an analogWrite(3 - it knows to write to the Digital port as a PCM port. Have I got that right? 2) You've used digital ports 3, 5 and 6 for motor control. All PCM ports. But, you really only need to use one for the speed control - right. Seeing as the other are written with either "HIGH" or "LOW". While I'll put together my own test system to test this out I just thought I'd check with you first, could save some time. Oh and I'll be "borrowing" more of your code from your turnout automation video as well. Thanks for all the good work!
Don't do this. Nearly 1 year later, I made this project and you get a very high pitched noise from the motors during the ramps, at least using this driver. You should use an alternate driver for that
Nice video, a great help to anyone that likes to play around with an Arduino on their layout. One minor note from my side, please try to omit the use of 'sleep()' functions in any sketch, as they are blocking the core from doing anything else. If this code is ported to an Arduino ESP32 with WiFi, it will fail to run correctly in combination with the WiFi. In addition, modification of the speed will currently also only happen 1.5 seconds after leaving the station.
Mr. Congratulations on your project! I thought it was fantastic. Mr. can you explain to me or post the schematic where the engine is and how the train runs?
Thanks for the video. I just figured out how to automate my street cars using JMRI Jython scripts but I'll need to run some DC motors for Magnorail to control my autos, so this tutorial is great. We all have those 30 year plans we've been waiting to get to. I think I'll use an ESP32 so I'll have MQTT remote control over WiFi.
This looks just great and i have a spot for this on my track but i can't seem to get it working. I have no power to my test track for the engine. Am i missing something , I have gone over all the coding and the pin plugin on the arduino. I'm stumped.
I take advantage of the portal for a question on roco geoline motorized exchanges with 61195 without decoder. Is it possible to control the switch via Arduino and a relay? Thank you. Carlo
Great info Jimmy, I have been wanting something like this for years long before I got into DCC. The one thing I would do differently is to put the pot on A0 so that the station numbers would be the same as the analog numbers, but that is just me and my ODC. Are you going to remove the LEDs from the IR sensor boards and extend the leads so that the boards can be hidden or are you going to just put the IR sensor boards below the roadbed with the LEDs pointing up? I've been watching you for a while and realized I am not subscribed, will I just took care of that.
my car starts when I wave my hand infront of the #2 sensor and does one forward and one backward cycle but will not run continuosly. Do I have something reversed?
Hello, Very clear explanation. Thank you. I still have one question: If I don't want station 3, I can still use all "//" where station 3 is concerned? In other words; i can skip station 3.
Hi I attempted to build this design (last August (!) and can NOT figure out why it still won't work. I have had it checked by several friends who say the sketch did load accurately, the jumpers are correct, and all appropriate lights on the arduino and L298N are lit. I get NO DC output to the rails at any time. The sensors show changes when they are "blocked", but no joy. Can you offer any assistance? Thx, Art
I'd be happy to guide you, but if someone just tells you, you won't learn. So with that in mind. Consider the information you have so far, what do you think you would have to do to add another station?
This is always fasinating to me but I get lost after a bit. Do you have any recommendations on a book I could get that would help to understand all this? Keep up the good idea's and have fun. GOD BLESS 🚂💖🚂💖🚂💖
I have a G scale New Bright train for my granddaughter. It is battery-powered (no track power) with a very crude mechanical method of forward / stop / reverse. I would like to add an Arduino / Bluetooth system to control the train motion. I have an Arduino Uno. Any help would be appreciated. A company called BlueRail has already solved this problem, but they have no product due to chip shortage
That’s not a good way to wire up the potentiometer.. the centre pin is the wiper which goes to the arduino input.. the outer pins are the vcc & gnd supply to the potentiometer track. You don’t need the resistor. And if you did need a resistor you’d use 470 or 430 ohm.. 440 ohms are not a member of either the E12 or the E24 resistor ranges.. However, I do like the idea to use the IR sensors.
For the stations in the middle, would it be possible to connect multiple IR sensors in parallel to the same input? That would allow adding additional stations to the setup without altering the code. Or is it imperative that each sensor have its own input pin? Also, would it hurt any to write the code to have additional stations even if there's no sensor connected to those pins? Like write the code to have 5 stations even if there are only 3 on the layout. I'm thinking about adding something like this to some T-TRAK modules, but I would likely be building the end station modules first, and adding modules with the additional stations at a later date, most likely one at a time, and it would be easier to not have to remember to adjust the code when adding a new station.
Hey Brian! I am not sure about the sensors in the middle. I am leaning toward no, but I would be willing to try and be happy to be proven wrong. As for the additional code, not at all! you can actually just put a "//" at the beginning of each line of code you want to be inactive.
What´s the purpose of the jumper on the motordriver ? Why to remove it ? Would it not be easier to use functions for the motorcontrol ? Nevertheless: thanks for the description. I see, you didn´t use pwm for speedcontrol ?
The jumpers basically make the motor outputs simple on and offs. By removing them, you can control the speed. Sometimes DC locomotives aren’t great with pwm, so I stuck with the tried and true. As for functions, I try to keep the tutorials as easy to understand as possible. This builds on previous tutorials I have done, so it’s easier to understand.
@@DIYDigitalRailroad aaah okay thank you, im trying to make my own train that can tow 50-100 pounds, but i was trying to figure out how i wanted to control the 2 motors and was interested in the set up
Like DCC, this will be more used as the install and programming process is higher level 4GL or 5GL Visual. EEs still like doing this like coding traffic sensors IRL, but most people don't. When the sensor is a simple up sensor drilled between the ties, they are all that plug in to the controller, with simple plugs and to program it: You pull up a screen with the stations drawn on a line as objects and you give the stations actions in English as you would PowerPoint animation... Stop, hold 1 minute, Reverse Or... proceed at speed 5 for sensor 10 Proceed at speed 3, blow horn 3 times at sensor 11 Better yet name the objects as MainStreet, Pine and Vine, Lockwood For old school LISP, PLC and even C programmers... that sounds boring... But it makes it user friendly for the average automator.... And easier to change.
Much different. It's possible, but it requires that you translate into DCC commands, and use a booster to translate that to a DCC signal. In principle it's not crazily hard to write code to control the train, especially if there's just a single train. It's just a bit more expensive or complex to set up. For DC, in principle the sensors plus two relays is enough for automating a there and back layout. Nicely oldfashioned electronics, nothing overly complex. For DCC it's largely the same, except that the train control is more involved. Oh, and the example of the video expects there to be only one train, so if there's more than one then you may run into issues.
Anyone had any luck using an arduino to make a passing loop on an oval single track layout with opposite direction trains? I'm using Kato unitrack turnouts.
If I were to make a homemade dcc setup, would I need a separate arduino for everything (sensors, switches, etc.) Or can a single arduino run multiple operations?
It really depends on your level of code ability. This is something that I have always wanted to try, but the level of code required is quite daunting to me.
@@DIYDigitalRailroad so I had a shower thought (on TonesTheGeek's question), there's a simple (as in minimal extra learning) solution using the DCC++Ex (thanks for finding that for me by the way). The DCC++Ex can be given instructions from a PC over the serial port (see their documentation for details), if you take the arduino from this project and connect its serial port pins to that of the DCC++Ex then this project can control the command station. At which point instead of your code controlling the device which powers the track it sends commands to the command station instead. It will require finding the right speeds to send for the effect you want and will be limited to a specific loco (until you change and reupload the code).
I picked up a few KATO trolleys from eBay and I love them, very reliable. But I should note that about half of my locomotives are also KATO, so yes I am biased.
I had a quite nice Märklin setup when I was young and wished the Arduino was available then! Now I have Arduino but no train and no space. It would be nice to add some acceleration and deceleration at the stops and maybe some signal lights? I always love to put in those kind of small details to make it even more real, and with the Arduino that would be easy and not expensive. Cheers!
Brilliant! Very inspirational! Do you know if these IR modules are available in a smaller form factor, e.g with SMD LED's? I have issues hiding such bulky IR sensors on my layout.
How about putting leads between the boards and the LEDs and remoting the board underneath or somewhere else? Just need a little heatshrink to insulate the LED's leads.
just put a phototransistor under the track from gnd to a gpio, then use a pinMode of a gpio INPUT_PULLUP. The shadow of the train will make the pin high.
I wired up this a demo and to see how it easy to get my Arduino and getting motor control working. I bought a set of 4 L298n boards and only could get about 2 Volts with 12 vdc. After a lot of troubleshooting I found I possibility received 4 bad L298 boards. I tried by passing the Arduino and just using 12 v in, and remove applying 5v from the the pin s 5/6 would go an still no output Is likely I received 4 badL298N Motor Driver Controller Board or am I just missing something?
There is a lot of bad practice here. Use ferrules on your wires, don't put dupont pins in a screw terminal, use case statements if it can't be more than one at once, you should use functions to set direction and at station 3 you have already set the direction so you don't need to change it.
@@eduardkompast3063 int Speed; int Direction; int station; void setup() { Serial.begin(9600); pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); } void loop() { station = map(analogRead(A0) + (analogRead(A1) * 2) + (analogRead(A2) * 4), 0, 7161, 0, 14) Speed=map(analogRead(A3), 0, 1023, 0, 255); switch(station) { case 1: //between 511-1023 Stop(); Dir(1); delay(1500); break; case 2: //between 1023-1534 Stop(); Dir(0); delay(1500); break; case 5: //between ~2500-3000 Stop(); Dir(Direction); delay(1500); break; } void Dir(Direction) { switch(Direction) { case 0: digitalWrite 5, HIGH); digitalWrite 6, LOW); Direction = 0; break; case 1: digitalWrite 5, LOW); digitalWrite 6, HIGH); Direction = 1; break; } } void Stop() { digitalWrite(5, LOW); digitalWrite(6, LOW); delay(5000); } This isn't tested as I don't have any of the hardware and I am sure someone else could condense it further, but the point is it is much easier to add stations and there is far less repetition.
I would have written this code: int Lstation; int Cstation; int Rstation; int speedVal; void setup () { Serial.begin (9600); pinMode (5, OUTPUT); //Enable pinMode (6, OUTPUT); //Direction. 1: Left, 0: Right pinMode (3, OUTPUT); //PWM //This while moves the train to the right station while (analogRead (2) < 500) { analogWrite (3, 50); digitalWrite (6, LOW); digitalWrite (5, HIGH); }
digitalWrite (5, LOW); } void loop () { Lstation = analogRead (0); Cstation = analogRead (1); Rstation = analogRead (2); speedVal = analogRead (3); speedVal = map (speedVal, 0, 1023, 0, 255); if (Lstation < 500) { analogWrite (3, 0); digitalWrite (5, LOW); digitalWrite (6, LOW); delay (5000); analogWrite (3, speedVal); digitalWrite (5, HIGH); } if (Rstation < 500) { analogWrite (3, 0); digitalWrite (5, LOW); digitalWrite (6, HIGH); delay (5000); analogWrite (3, speedVal); digitalWrite (5, HIGH); } if (Cstation < 500) { analogWrite (3, 0); digitalWrite (5, LOW); delay (5000); analogWrite (3, speedVal); digitalWrite (5, HIGH); } while (Lstation < 500 || Cstation < 500 || Rstation < 500) {} //wait doing nothing until the train leave any station } It's similar, but the while loop on the setup moves the train to the right station, no matter where it is
I don't understand the wiring of the pot and the need for the resistor. Couldn't you just connect one side of the pot to VCC, the other side to GND, and the wiper to the analog input?
Yes you could go without a resistor in wiring the POT, but the additional resistor helps to stabilize the readings from the pot and makes it more sensitive. Without it, it tends to jump from max to min with very little in between.
@@DIYDigitalRailroad the way you wired it didn't make any sense to me either. Typically the center pin is the wiper and the two outer pins are the ends of the fixed total resistance, so you would normally connect the two outer pins to gnd and 5V, then the center pin should follow a nice linear sweep 0-5V
Great video, Jimmy! I am trying to do DC automated running in Z scale using Arduinos thanks to your tutorials and some other ideas I’ve found around the internet. Any thoughts on accel/decel going into and out of the stations? I’d like to have it ramp up and down but I think that’s going to mean using like 4 million sensors…
I think you can do it without adding too many more sensors. If you know how much distance it takes to stop the train, just set sensors to either side of the station at that distance, and set the delay before reading sensors after the train starts again to long enough for the train to clear the sensor on the other side. I built an automated DC controller for train shows that runs a train for x time and then stops for y time, and adjusts the speed variable as it transitions so that it speeds up and slows down. I'm not at home or I could share the code that I used, but it's basically increase/decrease by x, then sleep y milliseconds until reaching max speed or 0 depending on whether it's accelerating or decelerating.
I think when we get into speed ramps and slowdowns, you may have to look into current sensing if you want ot limit your number of sensors. It's another project I would like to tackle.
Hello! i have followed some of the arduino code you shared and it worked, but this time i seem to have a problem with the IR3 sensor (aka Station 3) it seems to have no effect on starting and reversing afternoon. Please give me your contact email or facebook and I will send you a clip of the problem, hope you can reply to this soon, dear!
Thanks! i have been watching yr videos session for a long time. u do it very clear and slowly for anyone to follow.
I started ardunio for train few weeks ago and I fine yr work very helpful.
Cool video Jimmy!! As an HO steel mill modeler I have been experimenting with similar Arduino automation schemes. I'd like to automate a small switcher dedicated to a coke oven battery (to move coke receiver car from oven door to quench tower) or dedicated to a blast furnace (to move a hot metal car from the furnace to other set locations in the mill). Steel mills have lots of these short runs that can be automated. Your example is perfect for these applications!
Have three comments.
First, some DCC engines will operate in DC mode as well. I've been using a HO Athearn EMD 40 switcher previously equipped with NCE DCC decoder. It's the right size for my steel mill applications. Glad I didn't have to buy another engine.
Second, I'm using a higher voltage for HO scale ... 15V DC instead of 12. With a change of voltage, your automation method will work with many scales. I've made something similar work with T scale using 5V DC supply to motor driver.
Third, using a random() function call, the time spent at any of your stations can be made slightly more realistic. Simply add (or subtract) a reasonable random value from your 5000 msec delay.
All the best ... Happy Model Railroading ...
What's the reason for wiring the potentiometer as you've done, with the extra resistor? Usually you'd wire the outer legs of the pot to GND and VCC and the center leg to the analog input with no extra resistors. This standard voltage divider configuration gives a linear response from potentiometer position to the voltage output, and makes it possible to use the full 0 to 1023 range of the Arduino's ADC input, while the circuit you've shown gives what's called a "harmonic" response curve -- the ADC input value becomes extremely sensitive at one end, and much less sensitive at the other, while only reaching 94% of the full-scale voltage (with your resistor values).
Thanks so much for this video. I’ve just made it. Filling the extra add of a a station coding I’ve added 2 extra and changed the timings. I’ve gone for 6 seconds at each end and 3 seconds in the stops. Thanks again for your idiot proof video as I did it!!! First time I’ve tried anything like this
Very intriguing 👍 I'm too old school and would be using bump switches & relays for this. Still watching🚂🇨🇦
Excellent video Jimmy! This is a really helpful topic that will benefit those who have commuter trains they want to run independently as automatic start/stop/reverse etc.
Cheers from Melbourne, Australia!
Thank you - great project. Used 5VDC from Motor driver to power Arduino Nano and sensors - works great.
Thanks, Jimmy. This is exactly what I need to model the NJRT from the early 1900's on my upcoming new build! Timing could not be better! The link to the Kato Tram is an added extra to the arduino hardware and sketch. The tram is a close approximation to the NJRT Jewett cars!
The link to the Kato Tram doesn’t work for me. Can you direct me to a new link so I can purchase?
cool video Jimmy, in the coding you should use sub voids in the code.
like this,
void loop(){
station1=analogRead(A0);
station2=analogRead(A1);
station3=analogRead(A2);
speedval=analogRead(A3);
speed=map(speeval,0,1023,0,255);
analogWrite(3,speed);
delay(50);
if (station1
Cool idea. I’d consider adding an input for a start/stop button. If it’s running, continue to next sensor and stop, if it’s not running, run.
For station3, here's a better way to code it:
if (station3 < 500) {
// Direction doesn't matter for the 3 following lines
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(5000);
if (Direction == 1) {
digitalWrite(6, HIGH);
// No need to set port 5 to LOW as it is already set to LOW
}
else {
digitalWrite(5, HIGH);
// No need to set port 6 to LOW as it is already set to LOW
}
// Direction doesn't matter for the 1.5 sec delay
delay(1500);
}
And since we use this multiple times for other potential "in between" stations it also is much more neater to put all of this in a function with stationN as argument. No need to copypasta stuff when you add a station and no spaghetti code.
I have a project I am working on. it is a g scale train in a store window christmas display. I want to add a push-button (a big "Easy" button from Staples, hopefully) that when pressed will make the train run for a certain amount of time and then stop until the button is pressed again. also, if the button isn't pressed for longer than 20 minutes, the train would run on it's own, once. I am hoping to learn enough from your channel to achieve this! thank you for posting!
Nice video. BUT! You have wired the potentiometer completely wrong - side legs got to gnd and vcc (which is which doesn't matter), middle leg is the output. No resistor needed
Sweet setup, Jimmy! Thanks for sharing that with us.
I always wanted to do that. My day dream is to run my bullet trains point to point while I do my switching..
My thought was to put the electronics in a lineside building so I could just push a button.
Nicely done.
Great video. Potentiometer type/ resistance? Cheers
Thank you so much. With this I was able to make the rigi duo ski lift autonomous. It works great. And I know there will be more to make with this. Thanks again. Great teacher too.
Forgot to add....I had to add a ground from arduino to power side of motor control. Works great
I am currently building a future city layout with a bullet train that goes through different futuristic cities. I came across this and it was absolutely perfect for what I need. I just got a soundboard that will kick off train station chimes and information on arrivals and departures. I hope to get the sensors down the track a bit and time the train to slow into and out of the stations. Thanks!
Can you share your progress? I want to do this
What occurs to me is that this might be a great foundation for a tram system that runs in a loop. I love trams, so thanks for this video.
I started an Arduino course a few weeks ago in order to be able to control my HO DC train layout. your videos are a great, super helpful for a 78 yrs old man. I m confused about what to start first: train control? I have 3 different DC controllers. Can I apply only on one line (controller) .
from your experience, can you write to me, about what to do first and that second...and the next stages?
many thanks
Eytan Barak from Israel😇😍
Thanks for making this so simple and accessible to everyone
you can shift right 2 bits an int to get from 10 bits to 8 bits :-)
Nice. However, based on the type of IR sensors you use, which have a built in comparator / trigger, you can use simple digital inputs for the station1, station2, station 3 detection. ALso, if you really want this to "go pro" you can create slow start and stop routines using variation in PWM signals. But hey, for a startere, this is a great example ! End you do explain it clearly and decently in easy-to-understand language, cudos for that !
Do you know of a sensor that could be attached to a toy train running in a loop and the sensor would be used to make the train stop once it detects an objection in front of it?
The pin you were having a hard time reading was the "ENable A" pin ENA. On the other side is ENB or "ENable B"
Very useful and your details in typing with patience great I got this realy you are a great teacher... God bless you always make such tutorial for lay man like me 🙏🙏🙏🙏👍
Thank you! 😃
Hi Jimmy, I'm looking at doing a similar thing except adding in some turnout control and likely a second Tram. But, I have a couple of questions:
1) While I've used the Arduino for a few things, from what I see it "knows" the difference between the Analog 3 port and Digital 3 port. When I do an analogRead(A3 - it knows to read from the Analog port. But when I do an analogWrite(3 - it knows to write to the Digital port as a PCM port. Have I got that right?
2) You've used digital ports 3, 5 and 6 for motor control. All PCM ports. But, you really only need to use one for the speed control - right. Seeing as the other are written with either "HIGH" or "LOW".
While I'll put together my own test system to test this out I just thought I'd check with you first, could save some time.
Oh and I'll be "borrowing" more of your code from your turnout automation video as well.
Thanks for all the good work!
Small tweak, perhaps make it so that speed ramps down/up when arriving/departing a station.
Don't do this. Nearly 1 year later, I made this project and you get a very high pitched noise from the motors during the ramps, at least using this driver. You should use an alternate driver for that
Nice video, a great help to anyone that likes to play around with an Arduino on their layout. One minor note from my side, please try to omit the use of 'sleep()' functions in any sketch, as they are blocking the core from doing anything else. If this code is ported to an Arduino ESP32 with WiFi, it will fail to run correctly in combination with the WiFi. In addition, modification of the speed will currently also only happen 1.5 seconds after leaving the station.
Mr. Congratulations on your project! I thought it was fantastic. Mr. can you explain to me or post the schematic where the engine is and how the train runs?
I am using 3 TCRT5000 IR sensors that have 4 pins, should I use the Digital out pins or the Analogue out pins - thanks
Thanks for the video. I just figured out how to automate my street cars using JMRI Jython scripts but I'll need to run some DC motors for Magnorail to control my autos, so this tutorial is great. We all have those 30 year plans we've been waiting to get to. I think I'll use an ESP32 so I'll have MQTT remote control over WiFi.
Model trains meets electrical/computer engineering 😍
Do you need to solder the wire on the train track ? If so how can you solder the wire onto the train track?
Is this train special for aurdiona or any train can used for this project?
Any DC train can be used for this project.
I'm just wondering can you replace the infra red sensor by an reed switch
thanks for the project, in reverse direction the voltage in yhe tracks goes very low and the locomotive does not move request for a solution
Fantastic video Jimmy very helpful
Could you imaging doing the same stuff on a digitized locomotive
Is there a way to increase/decrease speed while the train is moving. Slow start/stop.
if i want to connect a screen for each station shows me the time of arrival , what will be the code ???
This looks just great and i have a spot for this on my track but i can't seem to get it working. I have no power to my test track for the engine. Am i missing something , I have gone over all the coding and the pin plugin on the arduino. I'm stumped.
Nice project and cool video! I'm thinking to do something similar, but for wooden train.
Is there a way to make the Tram to add more realistic slow down before stop and speed up gradually?
I take advantage of the portal for a question on roco geoline motorized exchanges with 61195 without decoder. Is it possible to control the switch via Arduino and a relay? Thank you. Carlo
Hi Can this be used over a stretch of a dcc layout.
cheers ,
Great info Jimmy, I have been wanting something like this for years long before I got into DCC. The one thing I would do differently is to put the pot on A0 so that the station numbers would be the same as the analog numbers, but that is just me and my ODC.
Are you going to remove the LEDs from the IR sensor boards and extend the leads so that the boards can be hidden or are you going to just put the IR sensor boards below the roadbed with the LEDs pointing up?
I've been watching you for a while and realized I am not subscribed, will I just took care of that.
double equal means compare the values, where as single equal means assign.
thanks for doing the video.
my car starts when I wave my hand infront of the #2 sensor and does one forward and one backward cycle but will not run continuosly. Do I have something reversed?
Hello, Very clear explanation. Thank you. I still have one question: If I don't want station 3, I can still use all "//" where station 3 is concerned? In other words; i can skip station 3.
Sorry for the delayed reply. Yes you can do that.
Hi I attempted to build this design (last August (!) and can NOT figure out why it still won't work. I have had it checked by several friends who say the sketch did load accurately, the jumpers are correct, and all appropriate lights on the arduino and L298N are lit. I get NO DC output to the rails at any time. The sensors show changes when they are "blocked", but no joy. Can you offer any assistance? Thx, Art
i have the l298n shield without leds on. And there are the same connections... i use a train trafo for give energy to shield...
Thanks Jimmy!
I'm going to give this ago. However, how would I go about adding additional stations?
I'd be happy to guide you, but if someone just tells you, you won't learn.
So with that in mind. Consider the information you have so far, what do you think you would have to do to add another station?
This is always fasinating to me but I get lost after a bit. Do you have any recommendations on a book I could get that would help to understand all this? Keep up the good idea's and have fun.
GOD BLESS 🚂💖🚂💖🚂💖
I would check out the sponsor in my next Monday video Skillshare. They have a bunch of beginner classes.
@@DIYDigitalRailroad
Can't wait for Monday then.
Thank you. GOD BLESS
🚂💖🚂💖🚂💖🚂💖🚂💖
I have a G scale New Bright train for my granddaughter. It is battery-powered (no track power) with a very crude mechanical method of forward / stop / reverse. I would like to add an Arduino / Bluetooth system to control the train motion. I have an Arduino Uno. Any help would be appreciated. A company called BlueRail has already solved this problem, but they have no product due to chip shortage
New drinking game: Take a shot every time he says "go ahead and" :P
That’s not a good way to wire up the potentiometer.. the centre pin is the wiper which goes to the arduino input.. the outer pins are the vcc & gnd supply to the potentiometer track. You don’t need the resistor.
And if you did need a resistor you’d use 470 or 430 ohm.. 440 ohms are not a member of either the E12 or the E24 resistor ranges..
However, I do like the idea to use the IR sensors.
good vid jimmy keep up the good vids thanks lee
For the stations in the middle, would it be possible to connect multiple IR sensors in parallel to the same input? That would allow adding additional stations to the setup without altering the code. Or is it imperative that each sensor have its own input pin?
Also, would it hurt any to write the code to have additional stations even if there's no sensor connected to those pins? Like write the code to have 5 stations even if there are only 3 on the layout. I'm thinking about adding something like this to some T-TRAK modules, but I would likely be building the end station modules first, and adding modules with the additional stations at a later date, most likely one at a time, and it would be easier to not have to remember to adjust the code when adding a new station.
Hey Brian! I am not sure about the sensors in the middle. I am leaning toward no, but I would be willing to try and be happy to be proven wrong. As for the additional code, not at all! you can actually just put a "//" at the beginning of each line of code you want to be inactive.
The pin you removed the jumper from with letters on it is the enable pin. So the letters read E N A
I’m not getting very much power to the track is there anyone that can help?
Could you provide your sources for the components of this control?
What´s the purpose of the jumper on the motordriver ?
Why to remove it ?
Would it not be easier to use functions for the motorcontrol ?
Nevertheless: thanks for the description.
I see, you didn´t use pwm for speedcontrol ?
The jumpers basically make the motor outputs simple on and offs. By removing them, you can control the speed. Sometimes DC locomotives aren’t great with pwm, so I stuck with the tried and true. As for functions, I try to keep the tutorials as easy to understand as possible. This builds on previous tutorials I have done, so it’s easier to understand.
@@DIYDigitalRailroad Thank you very much.
So how does the infrared red sensor go on the train, or how does the train know to stop?
The infrared sensors are proximity sensors. When the train trips the sensor the arduino cuts power to the track which stops the train.
@@DIYDigitalRailroad aaah okay thank you, im trying to make my own train that can tow 50-100 pounds, but i was trying to figure out how i wanted to control the 2 motors and was interested in the set up
I am absolutely inspired!
Like DCC, this will be more used as the install and programming process is higher level 4GL or 5GL Visual.
EEs still like doing this like coding traffic sensors IRL, but most people don't.
When the sensor is a simple up sensor drilled between the ties, they are all that plug in to the controller, with simple plugs and to program it:
You pull up a screen with the stations drawn on a line as objects and you give the stations actions in English as you would PowerPoint animation...
Stop, hold 1 minute, Reverse
Or... proceed at speed 5 for sensor 10
Proceed at speed 3, blow horn 3 times at sensor 11
Better yet name the objects as MainStreet, Pine and Vine, Lockwood
For old school LISP, PLC and even C programmers... that sounds boring... But it makes it user friendly for the average automator.... And easier to change.
Nice job jimmy! Do you think you could do the same for a DCC layout instead of a 12v DC?
Much different. It's possible, but it requires that you translate into DCC commands, and use a booster to translate that to a DCC signal.
In principle it's not crazily hard to write code to control the train, especially if there's just a single train. It's just a bit more expensive or complex to set up. For DC, in principle the sensors plus two relays is enough for automating a there and back layout. Nicely oldfashioned electronics, nothing overly complex. For DCC it's largely the same, except that the train control is more involved.
Oh, and the example of the video expects there to be only one train, so if there's more than one then you may run into issues.
Anyone had any luck using an arduino to make a passing loop on an oval single track layout with opposite direction trains? I'm using Kato unitrack turnouts.
Thank you so much sir😊
Thanks, that is what i looking for!
If I were to make a homemade dcc setup, would I need a separate arduino for everything (sensors, switches, etc.) Or can a single arduino run multiple operations?
It really depends on your level of code ability. This is something that I have always wanted to try, but the level of code required is quite daunting to me.
@@DIYDigitalRailroadfeel free to get in touch, I'm happy to explore giving you a hand with this.
@@DIYDigitalRailroad so I had a shower thought (on TonesTheGeek's question), there's a simple (as in minimal extra learning) solution using the DCC++Ex (thanks for finding that for me by the way). The DCC++Ex can be given instructions from a PC over the serial port (see their documentation for details), if you take the arduino from this project and connect its serial port pins to that of the DCC++Ex then this project can control the command station. At which point instead of your code controlling the device which powers the track it sends commands to the command station instead.
It will require finding the right speeds to send for the effect you want and will be limited to a specific loco (until you change and reupload the code).
Many KATO items, including trolleys are available on ebay, there are some actual Japanese hobby shops that sell on ebay.
I picked up a few KATO trolleys from eBay and I love them, very reliable. But I should note that about half of my locomotives are also KATO, so yes I am biased.
Thanks for this video ,Great video ,
That pin would be ENA “Enable”
Thank you!
I had a quite nice Märklin setup when I was young and wished the Arduino was available then! Now I have Arduino but no train and no space.
It would be nice to add some acceleration and deceleration at the stops and maybe some signal lights? I always love to put in those kind of small details to make it even more real, and with the Arduino that would be easy and not expensive. Cheers!
Nice video and great project :) - Cheers NordicTram
Brilliant! Very inspirational!
Do you know if these IR modules are available in a smaller form factor, e.g with SMD LED's?
I have issues hiding such bulky IR sensors on my layout.
How about putting leads between the boards and the LEDs and remoting the board underneath or somewhere else? Just need a little heatshrink to insulate the LED's leads.
@@garrettswoodworx1873 That would make it smaller indeed!
What Garret said!
just put a phototransistor under the track from gnd to a gpio, then use a pinMode of a gpio INPUT_PULLUP. The shadow of the train will make the pin high.
Thanks for the nice video.
I wired up this a demo and to see how it easy to get my Arduino and getting motor control working. I bought a set of 4 L298n boards and only could get about 2 Volts with 12 vdc. After a lot of troubleshooting I found I possibility received 4 bad L298 boards.
I tried by passing the Arduino and just using 12 v in, and remove applying 5v from the the pin s 5/6 would go an still no output
Is likely I received 4 badL298N Motor Driver Controller Board or am I just missing something?
I just want to make sure that you have pin 3 from the arduino. connected to the ENA pin for motor 1 on the arduino.
@@DIYDigitalRailroad The L298 was not grounded to the Arduino. Whoops! All better now
Super video
Well, that was cool.
Nice and easy!
There is a lot of bad practice here. Use ferrules on your wires, don't put dupont pins in a screw terminal, use case statements if it can't be more than one at once, you should use functions to set direction and at station 3 you have already set the direction so you don't need to change it.
So, how would your code look?!
@@eduardkompast3063
int Speed;
int Direction;
int station;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
station = map(analogRead(A0) + (analogRead(A1) * 2) + (analogRead(A2) * 4), 0, 7161, 0, 14)
Speed=map(analogRead(A3), 0, 1023, 0, 255);
switch(station) {
case 1: //between 511-1023
Stop();
Dir(1);
delay(1500);
break;
case 2: //between 1023-1534
Stop();
Dir(0);
delay(1500);
break;
case 5: //between ~2500-3000
Stop();
Dir(Direction);
delay(1500);
break;
}
void Dir(Direction) {
switch(Direction) {
case 0:
digitalWrite 5, HIGH);
digitalWrite 6, LOW);
Direction = 0;
break;
case 1:
digitalWrite 5, LOW);
digitalWrite 6, HIGH);
Direction = 1;
break;
}
}
void Stop() {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(5000);
}
This isn't tested as I don't have any of the hardware and I am sure someone else could condense it further, but the point is it is much easier to add stations and there is far less repetition.
Awesome!
Wow I think hooking up my home entertainment system was easier
Ever think of laying your own track
Kato track is atrocious
I would have written this code:
int Lstation;
int Cstation;
int Rstation;
int speedVal;
void setup ()
{
Serial.begin (9600);
pinMode (5, OUTPUT); //Enable
pinMode (6, OUTPUT); //Direction. 1: Left, 0: Right
pinMode (3, OUTPUT); //PWM
//This while moves the train to the right station
while (analogRead (2) < 500)
{
analogWrite (3, 50);
digitalWrite (6, LOW);
digitalWrite (5, HIGH);
}
digitalWrite (5, LOW);
}
void loop ()
{
Lstation = analogRead (0);
Cstation = analogRead (1);
Rstation = analogRead (2);
speedVal = analogRead (3);
speedVal = map (speedVal, 0, 1023, 0, 255);
if (Lstation < 500)
{
analogWrite (3, 0);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
delay (5000);
analogWrite (3, speedVal);
digitalWrite (5, HIGH);
}
if (Rstation < 500)
{
analogWrite (3, 0);
digitalWrite (5, LOW);
digitalWrite (6, HIGH);
delay (5000);
analogWrite (3, speedVal);
digitalWrite (5, HIGH);
}
if (Cstation < 500)
{
analogWrite (3, 0);
digitalWrite (5, LOW);
delay (5000);
analogWrite (3, speedVal);
digitalWrite (5, HIGH);
}
while (Lstation < 500 || Cstation < 500 || Rstation < 500)
{} //wait doing nothing until the train leave any station
}
It's similar, but the while loop on the setup moves the train to the right station, no matter where it is
That's such a great idea! I never thought of moving the train to the station in the setup!
I don't understand the wiring of the pot and the need for the resistor. Couldn't you just connect one side of the pot to VCC, the other side to GND, and the wiper to the analog input?
Yes you could go without a resistor in wiring the POT, but the additional resistor helps to stabilize the readings from the pot and makes it more sensitive. Without it, it tends to jump from max to min with very little in between.
@@DIYDigitalRailroad the way you wired it didn't make any sense to me either. Typically the center pin is the wiper and the two outer pins are the ends of the fixed total resistance, so you would normally connect the two outer pins to gnd and 5V, then the center pin should follow a nice linear sweep 0-5V
Great video, Jimmy! I am trying to do DC automated running in Z scale using Arduinos thanks to your tutorials and some other ideas I’ve found around the internet.
Any thoughts on accel/decel going into and out of the stations? I’d like to have it ramp up and down but I think that’s going to mean using like 4 million sensors…
I think you can do it without adding too many more sensors. If you know how much distance it takes to stop the train, just set sensors to either side of the station at that distance, and set the delay before reading sensors after the train starts again to long enough for the train to clear the sensor on the other side. I built an automated DC controller for train shows that runs a train for x time and then stops for y time, and adjusts the speed variable as it transitions so that it speeds up and slows down. I'm not at home or I could share the code that I used, but it's basically increase/decrease by x, then sleep y milliseconds until reaching max speed or 0 depending on whether it's accelerating or decelerating.
I think when we get into speed ramps and slowdowns, you may have to look into current sensing if you want ot limit your number of sensors. It's another project I would like to tackle.
SWEET
NO one, I repeat NO ONE ever uses red or white for positive power EVER.
HAHAHA! Some of us use what we have.
toot toot!
Adweo is to confusing
Hello! i have followed some of the arduino code you shared and it worked, but this time i seem to have a problem with the IR3 sensor (aka Station 3) it seems to have no effect on starting and reversing afternoon. Please give me your contact email or facebook and I will send you a clip of the problem, hope you can reply to this soon, dear!
Hello! You can email me at ddrrcommunity@gmail.com
Very complicated