External sensor connectivity

Barry Gordon Jan 30, 2020

  1. Barry Gordon

    Barry Gordon TrainBoard Member

    23
    6
    2
    I am playing with control systems in a DCC++ system. The sensors are inexpensive ambient light detectors, which I modified to have the photo-resistor on a short cable instead of on the circuit board; powered by a small 3.3 power supply. The sensor output is a 1 (3.3v) or a zero (0v). The sensor output goes to an Arduino mega 2560 digital input pin. Since I am not using the Arduino 3.3 supply, I assume I have to tie the external power supply ground to the Arduino ground pin to establish the correct reference for signal detection. Correct?

    Every block has a sensor at each end. This tells me where a train is, but not which train. I am pretty sure I can keep track of which train is where knowing the initial location of the train. I am intending to write my own code in a Raspberry pi which communicates with the Arduino. I know I could do simple control with JMRI and will be playing with it also. I just like to write code!

    comments / advice appreciated.
     
    Hardcoaler likes this.
  2. ppuinn

    ppuinn Staff Member

    2,377
    1,446
    55
    Following...
     
  3. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    Hate no one answered you. Yes, you should have a common ground. You probably got it working already, I imagine. For a digital signal, you wouldn't need a different ARef, but for analog you might. Are these Geoff Bunza's photo sensors using a comparator? There is another using a cheap microcontroller like an esp8266? The latter would give you wifi ;)
     
  4. Barry Gordon

    Barry Gordon TrainBoard Member

    23
    6
    2
    Thanks for the reply. I am building a small pint-to-point N-scale railroad as a test facility. I have a home theater the front wall of which is the screen and cabinetry. In front of the screen is a shelf that is 120" x 10". That is where the layout will be. The control system is on a pull out shelf in one of the cabinets below the screen. I am a control/electronics enthusiast and am playing around with control systems for the railroad.

    The track is all Kato unitrack with four interconnected mainlines and four bypass sidings. There are a total of 8 #6 turnouts and one double crossover. All these are controlled from two Digitrax DS64 stationary decoders.

    The control system is DCC++ plus a Raspberry Pi 4 B running JMRI. There are 24 sensors that each go to the Arduino mega as digital inputs. The first experiment was with photo-receptors, with the photo receptors in the track. That required good lighting. The current experiment is with obstacle detectors (an IR emitter and an IR Receiver). I painted the locomotive undersides with gloss white paint which gave me good detection when a train passed over the sensor. However there seems to be a problem I need to trace down (See problem note at end of post). My next experiment is to eliminate the IR emitters and use a 940 nm LED strip at the ceiling above the layout. This should cause all sensors to read active (obstacle detected) until a train passes over them at which time they should go inactive. JMRI allows sensor inputs to be inverted.

    The sensors are small inexpensive circuit boards with either a photo-resistor to detect light, or an IR emitter/receiver combination to detect obstacles. The boards have a voltage comparator, status LED's, and a sensitivity pot. They are a 3 wire device (Vcc, Ground, Signal). In all cases I removed the sensors from the circuit board and soldered in 2-pin connectors. The connectors come in a package of 50 with a male for the board and a 6 inch cable with a mating connector. I terminated the cables with either a photo detector (photo resistor) an IR emitter or an IR receiver all of which can be bought in a pack of 50. The actual sensors are mounted between the tracks in a small hole (3 mm) for each sensor.

    Problem Note:
    With the obstacle detectors; when I trip a sensor (put a train over it or a white paper to cause the IR light to be reflected, the LED on the sensor board comes on as it should. The JMRI sensor table shows that sensor going active until I remove the reflective surface at which time it goes inactive. HOWEVER other sensors no where near the one I am playing with change state. Also when I start up the system the sensor table shows various states for the 24 sensors. Some active, some inactive, some unknown. I would have thought they would have all shown as inactive (no obstacle detected) unless a train was sitting over it. I need to watch the DCC++ traffic to see if the problem is in the Arduino/ DCC++ or in JMRI.
     
  5. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    LOL! You sound like me. Build one of everything and then do experiments to see what I like best. I remember reading articles and seeing reviews from people about different brands of sensor and different technologies. Most of them have a place somewhere, depending on your particular situation, lighting, etc. And they all have disadvantages too. I'm surprised you didn't try hall effect sensors yet ;)

    If you type <S> (capital S) in the command window (or use the arduino serial monitor), does it report all the sensors you think are saved?

    I can offer some generalities. You said these are 3.3V boards. The Mega uses 5V logic levels. What is the voltage at the digital input pins for high and low? Measure it with a voltmeter. Arduino wants .6Vcc for high and .3Vcc for low. That means a good 3V signal (5v * .6) at the pins for high. 3.3V may not enough headroom with any voltage drop by the time the signal gets to the input pins. DCC++ is active low! So when the pin goes from over 3v to ground, JMRI will see a train. Are you using internal or external pullup resistors to force the input HIGH when not triggered? These type sensors usually need an external 1k pullup (and turn off internal pullups).

    Are all the grounds tied together and do you try to keep the runs short? And by "tied together", I mean twisted pair wire with signal and ground coming together at the Mega with signal to the digital input pin and grounds all tied of to a ground pin somehow. (No point to point wiring of the grounds at the sensors). When you say sensors nowhere near the one you are playing with change state, do they change at the sensor? IOW, does their little LED light come on? Or do they think they are off but the Mega thinks the are on? If it's noise, we can talk about decoupling capacitors.

    One last thing. After picking up development of the code I have wanted to look at the sensors section. The original DCC++ hard coded some parameters related to pin reading. Look at the routine sensor.cpp at the sensor::check() method. There is a SENSOR_DECAY constant that affects how the reading is smoothed. And importantly, you might try to play with the test parameters. The code takes the smoothed value and wants it to be below .5 to be "on" and above .9 to be "off". You can look at the formula at the top of that routine to see how it generates a number for "signal". Here SENSOR_DECAY is .3:

    this_sensor->signal=this_sensor->signal*(1.0-SENSOR_DECAY)+digitalRead(this_sensor->data.pin)*SENSOR_DECAY

    There are some important notes in the comments at the top of the file about how this works and what is reported back to JMRI big Q or little q.
     
  6. Barry Gordon

    Barry Gordon TrainBoard Member

    23
    6
    2
    Flightrisk,

    Thanks for the detailed reply. I will look into your suggestions. In the current configuration I need to isolate the problem. I am using 3.3 volts as the Vcc for the sensors as there were reports of the LED emitters dying after a while at Vcc=5 volts. I did try Hall effect sensors, but with very limited success so I didn't mention it.

    Checking the voltage at the mega input pins is next on my troubleshooting list. I have a little test Sketch for the Arduino that just monitors the input pins and reports when they change state. Running that independent of the DCC++ should help me isolate the problem.

    The control complex (a separate board with all components except the sensors and DS64 decoders) has all the PSU's (5V, 3.3V, 15V, and 12V), the RPi 4 and the Arduino Mega. It also has a small 7" screen connected to the RPi to allow me to watch things in JMRI.

    I have tried it with the JMRI sensor table set with and without pullup/pulldown but am not sure if that does anything. I have not been able to get JMRI to save that parameter in the sensor tables, but I think that is a knowledge problem (lack of) on my part.

    The whole layout is 10 feet long and less than 1 foot wide so wiring is pretty tight. The Vcc and grounds are wired through a feeder buss and going to each sensor similar to the way track power is supplied. The status LED's on the sensors appear to track properly, coming on when an obstacle is detected and going off when not. There does not appear to be any issue with the sensors so noise or low voltage at the input pins can be the problem. My little test sketch should help me isolate that.

    There are two umbilical cables that connect the layout to the Control system. One cable (16 gauge wire) Brings the 4 voltages I am using and their grounds to the layout along with track power. The second has all the sensor signals coming back to the control system. It is a 25 pin D cable connector with a cable length of 10 feet. No twisted pairs, no ground(s) in the signal cable. I didn't worry about cross talk as the frequency of the signals as they change is very low and the voltage is also small, but . . .

    Thanks again for your reply. The current Covid-19 issues gives me a lot of free time to "Play"
     

Share This Page