Trains.com

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Block detector using arduino

6859 views
20 replies
1 rating 2 rating 3 rating 4 rating 5 rating
  • Member since
    November 2021
  • 4 posts
Block detector using arduino
Posted by OCN Perry on Friday, January 21, 2022 9:56 AM

I am installing a simple non prototype signal sytem using the atlas products.

I started out planning to use BD20. I am now rethinking that as I would need to add a lot of resistors or resistance paint to rolling stock. I am wondering if anybody has experience with Arduino and could tell me if i could build a Detector using arduino and photo cell that would sent a 5 volt signal to my atlas Control board. Basicly I want to replace the BD 20 with photo cells. and if Possibel I would like to have multiple photo cells in a block, so if any one is tripped or covered it would send the 5 volts for that zone.

Thanks

Perry

  • Member since
    December 2004
  • From: Pa.
  • 3,355 posts
Posted by DigitalGriffin on Friday, January 21, 2022 10:55 AM

Yes it is quite easy to do.  There are several types of sensors you can use including IR and Photo.  Each has it's advantages and disadvantages.  The problem with photo sensors is they don't work when you want to do dark running (night time)

Don - Specializing in layout DC->DCC conversions

Modeling C&O transition era and steel industries There's Nothing Like Big Steam!

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Friday, January 21, 2022 11:45 AM

I built up a 14 block signal controller using a Arduino MEGA and IR optical detection.  It has been in and operating for a little over two years and I’m very happy with it.

I have several posts on my Blog about signaling.  I have tried all kinds of detection systems and I now prefer IR optical over current detection.

https://melvineperry.blogspot.com/p/blog-page_49.html

The Arduino IR sensors work very good and they are priced right too.  Under $1.50 in bulk.

https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2047675.m570.l1313&_nkw=IR+Infrared+Obstacle+Avoidance+Sensor+Module&_sacat=0



Good Luck


Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.

  • Member since
    March 2021
  • From: Vermont
  • 135 posts
Posted by Ablebakercharlie on Friday, January 21, 2022 11:51 AM

If I could just mention that Mel's blog is a GOLDMINE of information on just about any topic you might be interested in.

I find myself referring to his posts often when I am starting a new project and looking for guidance. 

Now that I am starting on Arduinos I am going to use a bunch of the sketches and wiring examples that he has posted for lighting and other activities.

Thanks Mel for all the work you have put into your blog to make it available to the model railroading community!

charles

  • Member since
    February 2018
  • From: Flyover Country
  • 5,432 posts
Posted by York1 on Friday, January 21, 2022 12:01 PM

Ablebakercharlie
Thanks Mel for all the work you have put into your blog to make it available to the model railroading community!

 

I'll second that!  Mel was the inspiration for me to learn Arduino.  I haven't done nearly the complicated stuff Mel has done, but I'm working on it.

Thanks, Mel!

York1 John       

I asked my doctor if I gave up delicious food and all alcohol, would I live longer?  He said, "No, but it will seem longer."

  • Member since
    November 2021
  • 4 posts
Posted by OCN Perry on Monday, March 21, 2022 3:09 PM

Thanks group. I have not been able to access my account. I was locked out when I changed my email. I am not familiar with Blogs. I would like to read more of Mel's stuff. How do Blogs work? 

I am an N scale person and one of my concerns in using I/R is drilling between the ties. But was actually looking at I/R. I have started to use the Atlas signal system so all I need is to decide on what type of detection to use. I/R or even Photo cell may be tricky as I have 3/4 ply with 1" foam on top. 

Perry

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Monday, March 21, 2022 6:25 PM

Optical sensors for N scale would be difficult because size.  The smallest I’ve worked with are a pair of 3mm LED type.

You might try a hall effect sensor between the ties and magnets on the rolling stock and locomotives.

https://www.electronics-tutorials.ws/electromagnetism/hall-effect.html

I’ve tinkered around with hall effect sensors and they do work very reliably, the SEN14709 is priced right at about $1.  I just found using the pre made IR modules much easier to use as I model HO scale. Size does make a difference. I can easily hide a 3mm sensor or emitter in HO goodies.







All of my across the track IR beams are at coupler height.
 


Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.

  • Member since
    December 2016
  • 168 posts
Posted by speedybee on Monday, March 21, 2022 6:55 PM

Tiny optical sensors are indeed available! I use them between the ties (no need to drill at all) on my HO scale layout but they're so small that I expect they could be hidden in N scale track too. They're about 2mm long, 1mm wide, and 1mm thick. IMO an optical sensor between the ties is the best way of detecting a train... They're basically invisible when surrounded by ballast and you can place them anywhere you want.

 

I was so pleased with these things that I posted about them a while ago but to my dismay no one shared my enthusiasm at that time.... However my previous post may still be of interest in this matter

http://cs.trains.com/mrr/f/744/t/291463.aspx

edit: this is the phototransistor I use, though there are probably others similar as well

http://www.mouser.ca/datasheet/2/143/PT19-21C-L41-TR8-1166191.pdf

  • Member since
    February 2018
  • From: Flyover Country
  • 5,432 posts
Posted by York1 on Monday, March 21, 2022 7:37 PM

My layout is N Scale.

I used photoresistors to work on my signal lights.

I was able to drill small holes in my N scale track.  The holes were visible, but were not noticeable.

The downside of the photo sensors is that room light affects the results.  I have a south facing window in the layout room, and my adjustments were made for the signals when the sun was out.

The issue was that on a cloudy day, if I happened to cast a shadow on the track, the signals were activated.

I spent a lot of time adjusting the sensors, and got them to a satisfactory level so the ambient light didn't cause too much of a problem.

However, if I build any more signals, I am going to switch to IR sensors.

York1 John       

I asked my doctor if I gave up delicious food and all alcohol, would I live longer?  He said, "No, but it will seem longer."

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Monday, March 21, 2022 7:47 PM

speedybee

Tiny optical sensors are indeed available! I use them between the ties (no need to drill at all) on my HO scale layout but they're so small that I expect they could be hidden in N scale track too. They're about 2mm long, 1mm wide, and 1mm thick. IMO an optical sensor between the ties is the best way of detecting a train... They're basically invisible when surrounded by ballast and you can place them anywhere you want.

 

I was so pleased with these things that I posted about them a while ago but to my dismay no one shared my enthusiasm at that time.... However my previous post may still be of interest in this matter

http://cs.trains.com/mrr/f/744/t/291463.aspx

edit: this is the phototransistor I use, though there are probably others similar as well

http://www.mouser.ca/datasheet/2/143/PT19-21C-L41-TR8-1166191.pdf

 

What are you using for your light source?

Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.

  • Member since
    December 2016
  • 168 posts
Posted by speedybee on Monday, March 21, 2022 8:06 PM

Just the ambient room light works fine. Although the sensitivity does peak in the IR range, the sensors work across a wide spectrum, still performing somewhat all the way to UV. I pull them up to 5V with a pull-up resistor and read the voltage on them with the Arduino's analog input, and simply comparing the reading to a threshold value reliably indicates whether the track is blocked or not.

There does need to be some light in the room, but it's ok whether dim or bright

edit: oops hit reply before I was finished typing

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Monday, March 21, 2022 8:37 PM

I do alot of night running with very little ambient light.  They would not work as a direct replacement in the FC-51 module in reflective mode.  

I ordered some 940nm SMD (0603) emitters and receivers from Digikey.  They might be a direct drop in replacement for the FC-51 module.

Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.


  • Member since
    December 2004
  • From: Pa.
  • 3,355 posts
Posted by DigitalGriffin on Tuesday, March 22, 2022 3:18 PM

RR_Mel

I do alot of night running with very little ambient light.  They would not work as a direct replacement in the FC-51 module in reflective mode.  

I ordered some 940nm SMD (0603) emitters and receivers from Digikey.  They might be a direct drop in replacement for the FC-51 module.

Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.


 

 

KEYESTUDIO Infrared PIR Motion Sensor Detector Module for Arduino DIY Project | eBay

This might also suit your needs.  One piece.  Bury it below the track, pointing upwards, or hide it on the backside of a tower building.  It's infrared, so it will work in the dark.  They all work on a similar principal.  Some just have more bells and whistles like adjustability.  You should be able to safely trim back the dome.  It acts like a collector lens to measure over a wide angle.  As you only care what's going on above the track, you only need the upward lens.

Arduino - Motion Sensor | Arduino Tutorial (arduinogetstarted.com)

 (1) PIR Motion Sensor with Arduino - YouTube

 

Don - Specializing in layout DC->DCC conversions

Modeling C&O transition era and steel industries There's Nothing Like Big Steam!

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Tuesday, March 22, 2022 5:25 PM

I’ve looked at the HC-SR501 its super slick but too big for model railroading at a 1” square and both are PIR looking for ambient IR not a pin point source for reflective or break beam detection.

The FC-51 works great except for sensor size, the SMD sensors might work and the 0603 size would be perfect.

Mel


 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
Turned 84 in July, aging is definitely not for wimps.

  • Member since
    July 2009
  • From: lavale, md
  • 4,644 posts
Posted by gregc on Tuesday, March 22, 2022 5:48 PM

DigitalGriffin
KEYESTUDIO Infrared PIR Motion Sensor Detector Module for Arduino DIY Project | eBay

This might also suit your needs.  One piece.  Bury it below the track, pointing upwards, or hide it on the backside of a tower building.  It's infrared, so it will work in the dark.  They all work on a similar principal.  Some just have more bells and whistles like adjustability.  You should be able to safely trim back the dome.  It acts like a collector lens to measure over a wide angle.  As you only care what's going on above the track, you only need the upward lens.

have you used this to detect a loco/rolling stock?

my understanding is a PIR sensor actually detects the movement of some IR source across its field.  it relies on the heat (IR) of the object such as an animal.   i can see how it might detect the heat of a loco, but not sure about rolling stock.

doubt a PIR detector is an inexpensive ($$) as a paired IR emitter/detector.   while the emitter requires a series resistor like any other LED, the detector, a transistor can be connected directly between an Arduino digital pin with a pullup resistor and ground

not sure this thread's title, "Block Detector is accurate since it's considering optical dectection which monitor specific locations on a track and not a "block" determined electrically

greg - Philadelphia & Reading / Reading

  • Member since
    December 2004
  • From: Pa.
  • 3,355 posts
Posted by DigitalGriffin on Wednesday, March 23, 2022 9:39 AM

gregc

 

 
DigitalGriffin
KEYESTUDIO Infrared PIR Motion Sensor Detector Module for Arduino DIY Project | eBay

This might also suit your needs.  One piece.  Bury it below the track, pointing upwards, or hide it on the backside of a tower building.  It's infrared, so it will work in the dark.  They all work on a similar principal.  Some just have more bells and whistles like adjustability.  You should be able to safely trim back the dome.  It acts like a collector lens to measure over a wide angle.  As you only care what's going on above the track, you only need the upward lens.

 

have you used this to detect a loco/rolling stock?

my understanding is a PIR sensor actually detects the movement of some IR source across its field.  it relies on the heat (IR) of the object such as an animal.   i can see how it might detect the heat of a loco, but not sure about rolling stock.

doubt a PIR detector is an inexpensive ($$) as a paired IR emitter/detector.   while the emitter requires a series resistor like any other LED, the detector, a transistor can be connected directly between an Arduino digital pin with a pullup resistor and ground

not sure this thread's title, "Block Detector is accurate since it's considering optical dectection which monitor specific locations on a track and not a "block" determined electrically

 



It depends on it's internal configuration.  Some are receive only.  Some transmit and recieve and look for a change in light.  The better ones are transmit/receive.

But you are right, that deserves some caution.  I'll have to try it with a plastic shim to see what happens.

 

Don - Specializing in layout DC->DCC conversions

Modeling C&O transition era and steel industries There's Nothing Like Big Steam!

  • Member since
    January 2012
  • From: Edgewood, WA
  • 54 posts
Posted by 2ManyHobbeez on Wednesday, March 23, 2022 10:34 AM

For your entertainment you may view this video showing a wheel counting BOD in action. It is connected to JMRI via MQTT over wifi and is configurable via Bluetooth using any tablet or phone. This requires the use of ESP32 as a controller rather than Arduino. More details and discussion are on the MRH forum under "BOD using axle detection - demo". It's experimental of course but has worked well in testing.

https://youtu.be/XXx7MxsFI0o

George

Edgewood, WA

 

  • Member since
    December 2016
  • 168 posts
Posted by speedybee on Wednesday, March 23, 2022 10:57 AM

RR_Mel

I do alot of night running with very little ambient light.  They would not work as a direct replacement in the FC-51 module in reflective mode.

 

Fair enough... A single overhead IR source would do the trick, but if your emitter/receiver pairs in reflective mode work, great.

When I tried that a few years ago I found them unreliable. The room at the time had a lot of windows letting sunlight in and tricking the sensors into thinking there is a train there. And on the other hand, some of my rolling stock was a poor reflector of IR, and the detectors didn't see them at all.

Overall, I had better results with a simple single receiver between the rails that is lit by ambient light when the track is clear and cast into darkness when there's a train atop it.

Point is, there's more than one way to skin a figurative cat

Re: the PIR sensor though, I don't see that being a good solution. It's bulkier and more expensive and has no advantage that I can see. And since it's designed to detect motion, what happens when there's a stationary train on it? Won't it incorrectly report that the track is clear?

  • Member since
    January 2009
  • From: Bakersfield, CA 93308
  • 6,526 posts
Posted by RR_Mel on Wednesday, March 23, 2022 11:53 AM

The trick to make the FC-51 work is tuning the focal point.  With the sensors at tie level and the focal point at .4” above the rails they never miss.

With the sensors on the board as is they work great from 2” to the side and above.

In hidden areas I mounted connectors for vertical position of the board 2” from the out side rail and bent the sensors 90° and again they never miss.


Mel



 
My Model Railroad   
http://melvineperry.blogspot.com/
 
Bakersfield, California
 
I'm beginning to realize that aging is not for wimps.

  • Member since
    December 2004
  • From: Pa.
  • 3,355 posts
Posted by DigitalGriffin on Wednesday, March 23, 2022 1:03 PM

speedybee
Re: the PIR sensor though, I don't see that being a good solution. It's bulkier and more expensive and has no advantage that I can see. And since it's designed to detect motion, what happens when there's a stationary train on it? Won't it incorrectly report that the track is clear?

 

Good point.  Not so good on stationary objects.

Don - Specializing in layout DC->DCC conversions

Modeling C&O transition era and steel industries There's Nothing Like Big Steam!

  • Member since
    July 2009
  • From: lavale, md
  • 4,644 posts
Posted by gregc on Wednesday, March 23, 2022 1:50 PM

speedybee
A single overhead IR source would do the trick, but if your emitter/receiver pairs in reflective mode work, great.

When I tried that a few years ago I found them unreliable. The room at the time had a lot of windows letting sunlight in and tricking the sensors into thinking there is a train there

using reflective IR detection in sunlit rooms or with incandescent room lighting is a significant limitation of this approach.

obviously more applicable with hidden trackage

greg - Philadelphia & Reading / Reading

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Users Online

There are no community member online

Search the Community

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Model Railroader Newsletter See all
Sign up for our FREE e-newsletter and get model railroad news in your inbox!