electric/magnetic interference on ldr (another slot car project)

i think what i have here is a hardware anomaly ("i'll take 'things programmers say' for $1,000").

about 2+ years ago i started a project that was supposed to be a simple slot car lap counter. as you can see from the attachment, it's grown substantially.
(btw, i included the picture of the test track just to give you a visual of the beast. i realize that you won't be able to glean any details concerning actual wiring and such).

here's the components that make up the counter architecture.
1. 4 lane track. each track has a ldr/resister circuit installed under it. 4 holes in the track allow the ldrs to be visible from above the track. these circuits are connected to IH pins on a mega.
2. 4 lasers mounted in a housing that point at the ldr's from above the track. these are connected to nothing. they simply supply the light source.
3. 4 interrupt handlers that detect the absence of laser on the ldr when the cars pass under the laser.
4. 4 "idiot" lights in the form of leds that tell me when an IH is dispatched. connected to pins on mega.
5. 4 more idiot lights that tell me when i'm processing the interrupt. connected to pins on mega.

here's what happens during test.
1. i boot this whole mess.
2. i pass a pencil through the laser, blocking the ldr, to simulate a lap. everything's cool - no problem what-so-ever. lap's are counted, speed is displayed, etc, etc, etc.
(btw, the pencil test is how i developed and tested 75% of the code. that is, i didn't connect the components to an actual track until about a year ago. that's when i ran into this anomaly. i then downsized the track length to make it the size of the attachment pic. easier to handle/test, and wife wanted the 4x8 sheet of plywood off her dining room table.)
3. then, with no cars on the track, i squeeze the trigger on the remote control applying voltage to the track(s). nothing happens - this is good. nothing should happen.
(btw, track runs off an adapter rated at 17v dc 6va.)
4. then, with car(s) on the track, i squeeze the trigger. this is when all hell breaks loose. idiot lights flash constantly and with no predictability. has nothing to do with whether the car passes through the laser. happens all the way around the track. and, has nothing to do with how many cars are on the track. even 1 car will blink all lights. this is most curious. how can 1 car on 1 track affect all tracks?

bottom line:
something is causing the IH pins on the mega to change state regardless of the ldr/laser interaction and thus dispatching the IH's randomly.

here's what i've tried.
1. changed the value of the resistor on the ldr/resistor circuit. don't remember exactly what values i used, but if i know me, i'm sure i went very high and then very low in an effort to divide and conquer. neither high or low made a difference, so i abandoned that approach.
(fyi - during the writing of this post i tried one more time first installing a 1M resistor. then adding 1 more at a time for a total of 3M's in series. here's the results:

  • a) with the ldr's close to the track - no difference.*
  • b) with the ldr's raised above the track, about 6" - same as "a)" except led indicators don't blink as bright.*
  • c) pencil test fails - seems that with this amount of resistance i can't even "force" a lap)*
    2. isolation. tried wrapping the ldr/resistor circuit in a variety of insulators. aluminum foil, copper, cardboard, a combination of each.
    3. ferrite cores. added them to every inch of wire from the ldr/resistor circuit to the pins on the mega, including 5v and gnd wires.
    4. reversed the location of the ldr/resistor circuit and the laser in an effort to move the ldr/resistor as far away from the track as possible. that is, shot the laser beam through the track up to the ldr. the ldr was about 6" above the track.

i hesitate to include the next 2 points because i don't want to steer the conversation in a particular direction. but ...
1. i've, of course, googled extensively. the most recent hit i ran across are these things called 'noise filtering circuits'. have no idea if it's what i need - the words just seem to fit. my problem, as an amateur in electronics, is that i've only ran across schematics which i can't fully read. for me to implement i'd need a wiring diagram.
2. is it possible that the cars themselves are causing the problem? that is, are they really capable of generating an electromagnetic field that would have this effect on the ldr's/IH pins?

willing to try whatever. been working on this for a long time. would really like to see it happen. <<<
thanks <<<

again, i don't think this is a software problem since it works flawlessly with the pencil test. i included the following snippets for the sake of completeness.

// ldr sensor pins. 
// the interrupt handlers are attached to these pins
//
const int car_1_pin = 2;    // pins per mega
const int car_2_pin = 3;
const int car_3_pin = 18;
const int car_4_pin = 19;

here's how the ldr's are wired. duplicated for each of 4 tracks. 2, 3, 18 and 19.
ldr is either GL5516 or KE-10720. sorry, i can't remember which. it's a basic ldr. nothing special.

      +5v (mega) .--- ldr ---.--- 15k ---. GND
                             |
                             |
                          IH pin

this is how i attach the interrupts. notice 2 versions. i use them interchangeably when testing and i think
one method might work better than the other. both versions seem to act exactly the same.

 /*
 pinMode( car_1_pin, INPUT_PULLUP );
 pinMode( car_2_pin, INPUT_PULLUP );
 pinMode( car_3_pin, INPUT_PULLUP );
 pinMode( car_4_pin, INPUT_PULLUP );
 //
 attachInterrupt( digitalPinToInterrupt(car_1_pin), car_1_ih, FALLING );
 attachInterrupt( digitalPinToInterrupt(car_2_pin), car_2_ih, FALLING );
 attachInterrupt( digitalPinToInterrupt(car_3_pin), car_3_ih, FALLING );
 attachInterrupt( digitalPinToInterrupt(car_4_pin), car_4_ih, FALLING );
 */
 digitalWrite( car_1_pin, HIGH );
 digitalWrite( car_2_pin, HIGH );
 digitalWrite( car_3_pin, HIGH );
 digitalWrite( car_4_pin, HIGH );
 //
 attachInterrupt( digitalPinToInterrupt(car_1_pin), car_1_ih, RISING );
 attachInterrupt( digitalPinToInterrupt(car_2_pin), car_2_ih, RISING );
 attachInterrupt( digitalPinToInterrupt(car_3_pin), car_3_ih, RISING );
 attachInterrupt( digitalPinToInterrupt(car_4_pin), car_4_ih, RISING );

here's the IH. duplicated for each of 4 tracks using appropriate array index for each.
basically flashes the "got the interrupt" led, and sets a trigger which is examined in loop()

// car 1 interrupt handler
//
void car_1_ih( void ) {
digitalWrite( ih_leds[0], HIGH );
if( flag_e == e_green ) {
  for( int i=0; i<num_cars; i++ ) {    // find first "empty" spot in array
    if( ih_trigger[i] == 0 ) {
      ih_trigger[i] = 1;               // track 1
      car_rf_time[0] = millis();
      break;
    }
  }
}
digitalWrite( ih_leds[0], LOW );
}

What is an IH pin?

What resistor value are you using with the LDR to make a voltage divider? I presume each LDR has its own resistor.

How, exactly, are the LDRs connected to the Mega - make a simple pencil drawing showing all the connections and post a photo of the drawing. See this Simple Image Guide

How is your wiring routed? For example do the signal wires pass close to the track power wires? Post a photo that shows the routing.

...R

I don't know what's wrong but here's snippets and partial wiring?

Bye.

I've just noticed that there is information about the LDRs mixed in with the program snippet. IMHO 15k is a very low resistor value. I have some that are working nicely with 220k resistors - admittedly using ambient light rather than a laser.

Using INPUT_PULLUP will put the internal resistor in parallel with the LDR. This is a case for NOT using INPUT_PULLUP.

I don't think it makes any difference but my LDRs are connected to GND and the external resistor is connected to 5v. I did that because it makes my wiring more convenient.

...R

Point of information: ferrite cores will do nothing if the material mix is not right for the frequency of the interference you are trying to control.

Paul

+5v (mega) .--- ldr ---.--- 15k ---. GND
|
|
IH pin

If the IH pin is the INPUT_PULLUP sense pin then that circuit should work so that when the LDR is dark the IH pin gets pulled LOW. Wellll, if the connect to IH is from the LDR and not before the LDR which would be stupid and likely just a formatting problem in the editor.

BTW, LDR's are S L O W

The Light Dependent Resistor
a light dependant resistor

Typical LDR

As its name implies, the Light Dependent Resistor (LDR) is made from a piece of exposed semiconductor material such as cadmium sulphide that changes its electrical resistance from several thousand Ohms in the dark to only a few hundred Ohms when light falls upon it by creating hole-electron pairs in the material.

The net effect is an improvement in its conductivity with a decrease in resistance for an increase in illumination. Also, photoresistive cells have a long response time requiring many seconds to respond to a change in the light intensity.

The LDR datasheets I have seen have reaction times closer to 100 ms or less. The GL55 series is supposed to respond to increased light in 20 ms and decreased light in 30 ms. How total that response is, I don't know but 30 ms for any dark change might not be enough time for a fast moving slot car to be dark-detected with your LDR. That might be fast enough for what you did with your pencil, did you wave it fast over the holes?

I am for no reason whatsoever sure that your track with the holes does not move at all, not even a slight wobble. You could save yourself 3 lasers and the trouble of aiming them (not to mention having the beam reflect off a car into someone's eye -- they might get vision back in a couple hours or so, my buddy did when he caught a curved mirror with his laser pointer, hey just lucky it wasn't a flat mirror!) by placing leds above the holes --or--

There are industrial proximity sensors that have an IR led pointing about 90 degrees across the view of an IR detector, the two packaged close so that something must be within a finger thickness to reflect the IR to the detector. They run about 50 cents each in boxes so > $1 to get 1 is reasonable but -- shop around, try eBay or,,, just MAKE YOUR OWN but don't use LDR's, get > 100x faster phototransistors instead.

Posted by Robin2 (reply to)

--- sorry for my shorthand. Interrupt Handler pin. there are only certain pins, on certain boards, that support the attachment and invocation of interrupt handlers. here's some examples:

// -- Board Digital Pins Usable For Interrupts
// ----- ----------------------------------
// -- Uno, Nano, Mini, other 328-based 2, 3
// -- Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
// -- Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
// -- Zero all digital pins, except 4
// - Due all digital pins

--- i've tried many different resistor values. all the way up to connecting 3 -1M's in series. maybe i just didn't hit on that magic number. suggestions?

--- i attached pics to show how ldr's are connected to mega (btw, it's a 2560 if that matters). 1 is the actual harness that lives under the slot track. the other is a breadboard version that i use for testing. much easier to swap resistors on the breadboard (note the 3 - 1M's). when using this version i simply place it on the track, connect it to the mega, and simulate car movement by placing the car on the track, rear wheels up, and pulling the trigger to see if i get interference. always do.

--- in this smaller test setup my signal wires are about 2 feet away from the track power wires. that is, if by power wires you mean the wires that are attached to the triggers via 17v adapter. the "dining room table" version placed them much farther apart. there are, of course, also the "power wires" that run the entire length of the track (for each track) in the form of the metal strips. in this case, the metal strips run smack dab over the top of the signal wires (the harness) - track thickness apart (1/16" maybe).

Posted by Paul_KD7HB (reply to)
--- thanks for the comment on the ferrite's. will google when i get a chance.

Posted by GoForSmoke (reply to)
--- yes, formatting problem in the editor

--- will need more time to absorb and research your other comments. will likely get back. i can say that i did move the pencil as slow as i could, as well as as fast to insure that i only counted exactly 1 lap in either case. it worked consistently.

thanks to all for the information and interest.
lee

Images from Reply #6 so we don't have to download them. See this Simple Image Guide

...R

The arrangement of the LDRs as voltage dividers seems fine. The fixed resistor needs to be chosen so that the voltage going to the Mega's I/O pin is clearly HIGH or LOW depending on whether the LDR is illuminated or in shadow. (Which is which depends on which side of the divider the LDR is placed).

Have you changed your code to NOT use INPUT_PULLUP?

I can't visualize what you describe for your "smaller test". And I don't know if that causes the same problems as the "harness under the track". If you are getting the problem with the "smaller test" then I suggest that you stick with that until it is eliminated.

If you are not getting the problem with the "smaller test" then it is the difference between it and the "harness under the track" that is the cause of the problem.

My own system is for detecting model trains so there is no need for precise timing and I am not using interrupts - but interrupts should work fine. However it may be useful to debounce the interrupts - if a car triggers an LDR it will be several seconds before it will complete a lap and trigger it again.

...R

With the pin moded as INPUT, that resistor could be 4.7K to 20K and work but that could be narrowed down. You want dark to show up as quickly as possible on the sense pin, use less resistor to drain charge in the wire to the pin quicker but see if 20K will do, don't go straight to the low since the less resistance means more current lost when the sensor is lit up.

Robin2

--- i have not used INPUT_PULLUP for a long time. notice that those calls are commented out in my code snippets. i left them in the code (commented out), just in case. i will not use.

--- the smaller test is simply the smaller version of the track layout - not the 4x8 plywood version. layout is identical to the pic i posted. the harness under the track was used for both track sizes. and, produced the same interference. i will continue to test on the small track, especially since i'm not allowed in the dining room any more.

--- p.s. will try simple image if there's a next time. i didn't know you had to download. when i click on the attachment the pic simply comes up in picasa. sorry.

GoForSmoke

--- thanks for the advise on resistor sizes and what i should expect them to do. will try.

thanks,
lee

As I recall (from about 30 years ago), slotcar motors are simple permanent magnet motors, fed by two strips of copper braid rubbing two powered brass rails imbedded in the track. This is a very, very, electrically noisy environment. I hope that you are NOT powering the arduino from the track supply. You might want to try paralleling a .1uf and a 100(or larger)uf capacitor across the arduino supply. if you have access to all the slotcars that will be running on the track, you might want to try putting a .1uf across the motor contacts. Also, don't let anybody use a 'dyna-can' controller (very noisy power stealer).

123Splat

--- i, as well, go back that far. probably farther. and, not much has changed since the old days. same permanent magnet motors. the copper braid has been replaced with metal and the brass rails also replaced with metal, but still very noisy. you can almost feel the magnetic field generated on your neck hair.

--- no, i am not powering the board with track power. track runs off a 17v adapter. boards are powered via 5v usb ports off the laptop. when all this is working an i do my final build/installation i'll switch to 9-12v adapters to power the arduino's. someday ...

--- "You might want to try paralleling a .1uf and a 100(or larger)uf capacitor across the arduino supply."
ok, but, sorry to be a non-electrical engineer type, but if this suggestion is going to do me any good i'm going to need it in english. and, a pencil drawing of exactly how to wire it (not a schematic - can't read one with any reliability). in terms of placement of the caps, i assume that by "arduino supply" you don't mean the 5v from the laptop, but, instead the supply to the mega IH pins. if that's true it implies that i modify what i call my ldr harness. see previous posts to view harness.
i'm excited about trying this because i do believe it's the cars themselves that are, in fact, the cause of the problem. the problem only surfaces when a car is fired up. i alluded to this in my original post. i believe that my ldr harness is acting like an antenna sucking up the car noise and passing it to the mega pins.
having never used caps before i have a few general questions:

  1. are we, in fact, building what's known as a noise filtering circuit? just curious.
  2. i did some window shopping, noticed that as well as cap "size", they also come with voltage ratings. would the general rule be to by a voltage rating higher than you would ever expect to use?

--- you might want to try putting a .1uf across the motor contacts.
would have no problem trying this if i could find a way to put it across without actually touching the contacts themselves. the contacts require swap outs often. all that friction, wear, and carbon build up.

--- googled 'dyna-can' controller. couldn't find a thing. my controllers are your basic wound wire rheostat type with a trigger.

looking forward to instruction on how/where the install the caps.
thank you,
lee

leepalmer:
--- you might want to try putting a .1uf across the motor contacts.
would have no problem trying this if i could find a way to put it across without actually touching the contacts themselves. the contacts require swap outs often. all that friction, wear, and carbon build up.

Put it across the wires going to the motor as close as possible to the motor. I have seen situations where a motor has 3 x 0.1 µF caps. One from each power wire to the motor body and one across the motor wires.

However, if the power to track is a high-frequency PWM signal the capacitors may absorb some of it and affect performance.

Before fitting capacitors I think I would try wiring each LDR with a twisted-pair wire. That just means the two wires wrapped round each other. And take that wire well clear of the track before connectiing to anything else. Also run the twisted-pair at right-angles to the track so it has the minimum length close to the power tracks.

And, in the context of using twisted pair I would connect GND directly to the LDR so that the twisted pair is carrying GND and signal rather than 5v and signal.

...R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you got any bypass capacitors on the input circuit?

How are you powering your project?

Tom.. :slight_smile:

If you fit the track with proximity sensors that only light leds and detect those from a distance, the noise would make much less of an effect. Long unshielded wires in LOW state get most affected by electrical noise.

I get the feeling that when you run the cars there's enough shake that one or more lasers occasionally misses.
You don't need lasers, only illumination that's easy to keep on the detectors.
Laser adds a precision requirement between track on table and ceiling above.

TomGeorge

--- no-go on the CAD picture. the hand drawn pic i posted is in .jpg format.

--- no capacitors on the circuit yet. don't have any - have to get some. thanks for the wiring placement on the circuit. have a few questions.

  1. do i need only 1 0.1uF? is placement on the 5v/gnd wires arbitrary? i see that you placed it in a position that "wraps" the first resister/ldr.
  2. also, robin2 suggests that i connect gnd directly to ldr (opposite of my connection). instead of rewiring the circuit, the easiest way for me to do that would be to simply connect gnd to 5v, and 5v to gnd on the mega. question is, will that affect the order i connect the 0.1uF. that is, will the connection always go from 5v to ground, (as in your example) or doesn't it matter.
  3. 123Splat recommended 100uf or larger for the other 4. agree?

--- from a previous post:
no, i am not powering the board with track power. track runs off a 17v adapter. boards are powered via 5v usb ports off the laptop. when all this is working an i do my final build/installation i'll switch to 9-12v adapters to power the arduino's. someday ...

robin2

--- some info and questions on twisted pairs.
did some research on them and if i learned correctly it seems that they work when the current is "opposing", as in my 5v/gnd wire to the arduino. and, since the 5v/gnd look like a "pair" those are the ones i twisted.
the ldr wires are single wires. can i simply "add" a wire and twist them such that both ends are singularity connected at each end?
or are you suggesting that i pair the 4 ldr wires into 2 pair and twist them? if so, why not twist all 4 together?
please educate and advise.

--- status
given your suggestion to move all but the ldr's as far away from the track as possible, i set aside my harness for now (probably permanently) and built new to that end.
i'm happy to report that with that, and the 5v/gnd twisted i still get some interference, but it's a lot lot less.
my current problem is that i can no longer detect laps. i've triple checked my wiring. there has to be something simple/stupid that i overlooked. i'll deal with all that.

thank you all so much for the assistance and education,
i think we're getting closer,
lee

Long unshielded wires have another name. Antenna.

leepalmer:
did some research on them and if i learned correctly it seems that they work when the current is "opposing", as in my 5v/gnd wire to the arduino. and, since the 5v/gnd look like a "pair" those are the ones i twisted.
the ldr wires are single wires.

The idea with a twisted-pair is that the signal and its GND are paired. I can't envisage how the 5v and GND wires would need to be near each other (assuming that the LDRs are connected to GND rather than the 5v). However I am very far from being an expert on that stuff.

On my model railway system the 5v and the resistor is close to my Mega and then two wires (GND and Signal) go to the LDR at some distance away.

...R

When charges move dopwn a wire it makes a field around the wire which takes time and to change, any change to the local field is resisted, Faraday said that. If you have another close wire with current running opposite the field effects of both will combine. The twist part is because EM fields have torque, twisted pair signals support each other and give faster data rates.

Cat3 and CAT5 cables have twisted pairs and shielding that works when grounded.