Multiple LDRs detection erratic

Hello all,

This is my first Arduino project, I'm a software developer at heart! :slight_smile:

I discussed my project initially on this board on another thread Which Arduino to buy for this project? - Project Guidance - Arduino Forum... basically controlling 8x relays with the digital out pins, while monitoring the signals in from 4x LDRs on the analog pins. This is all controlled from a PC application. To be frank, I have been amazed at how easy it has been so far: all the relays are now being controlled, messages are flowing back and forward from the Uno to my app on the PC and I can get signal sent back from the LDRs. However the LDRs are giving me a bit of an issue.

I have modified a sample program for using one LDR to light a single LED when the sensor is in low light to try and monitor 4 - as that is what I will need to be doing on my app. The sensors themselves will located one per groove of the track, sharing a constant light source above. When the car passes I need to detect it and send the signal back to the PC.

The problems I'm getting are that the sensors seem - well - flaky! I have tried various sensors, so I think its to do with my config or circuit. Is there something I'm missing? Has anyone got any advice?

I have included my test program code below, as well as a schematic of the test build project as it stands.

Hope someone can assist.

Mark

int iLightPins[4] = {0,1,2,3};
int iLEDPins[4] = {13,12,11,10};

void setup() {
  Serial.begin(9600);
  Serial.println("SETUP");
  for (int i=0; i<4; i++) {
    pinMode(iLEDPins[i], OUTPUT);
  }
}

void loop() {
  int iLightPin = -1;
  int iLEDPin = -1;
  int iRead = -1;
  int iWrite =  -1;
  for (int i=0; i<4; i++) {
    iLightPin = iLightPins[i];
    iLEDPin = iLEDPins[i];
    iRead = analogRead(iLightPin);
    Serial.println(iLightPin + " - " + iRead);
    iWrite = iRead / 4;
    analogWrite(iLEDPin, iWrite);
  }
  delay(100);
}

SlotBox.fzz (10.9 KB)

You are sharing ground and supply wiring between sensors and relays?

You mustn't do that, you'll superimpose IR voltage losses in the high current
wiring onto your sensor voltages.

Run separate sets of wiring to the sensors, direct from the Arduino, ditto for the
ground/power to the output relays.

Ideally the relays are separately powered so the only the ground has to be commoned,

Ah ha!

So I need to get a separate supply for the relays then!

Do you think that might explain the erratic nature of the LDR detection?

Would you be able to show me by modifying the Fritzing I attached to the original post?

Many thanks for helping so far!

Mark

MarkT:
You are sharing ground and supply wiring between sensors and relays?

You mustn't do that, you'll superimpose IR voltage losses in the high current
wiring onto your sensor voltages.

Run separate sets of wiring to the sensors, direct from the Arduino, ditto for the
ground/power to the output relays.

Ideally the relays are separately powered so the only the ground has to be commoned,

Hi I have attached a modified schematic. Does this look better now?

SlotBox2.fzz (11.1 KB)

Does this look better now?

Hard to tell. Stupidly large pictures are very difficult to see.
Have you read the how to use the forum sticky?

And don't go thinking that is a schematic, it is a physical layout diagram and normally next to useless for discussing circuits. But in your case when we are talking about poor physical layout it will do.

Edit
No your grounds are not connected together.

Grumpy_Mike:
Hard to tell. Stupidly large pictures are very difficult to see.

Well, inconvenient and tedious, but - right click on enlarged version, "View image"

All,

I apologise for the large images. I hadn't realised they were quite so large!

And just so you know - I DID read the how to use sticky, but as I said previously I hadn't realised the images were so large.

And just for you Grumpy_Mike, sorry for using the incorrect terminology. I am a new user here, and new to working with Arduino/electronics in general. But hey, now I'm suitably informed!

I will change some of the layout on my "physical layout diagram" and post it at lunch today. The forum is proving very helpful, so I hope it continues to be!

Thanks,
Mark

Paul__B:

Grumpy_Mike:
Hard to tell. Stupidly large pictures are very difficult to see.

Well, inconvenient and tedious, but - right click on enlarged version, "View image"

Hard to right click on an iPad!

Grumpy_Mike:
Hard to right click on an iPad!

I don't think iPads were intended for use by engineers.

Dunno about that, keep mine next to pc , really useful for datasheet lookup.
Safari seems not to suffer from google or internet delays.

Grumpy_Mike:

Does this look better now?

Edit
No your grounds are not connected together.

Right, I have amended the layout diagram and attached a new, suitibly resized, image. Im pretty sure the grounds are connected now.

As for the power source for the LDRs, I show a 9v battery on here - but I will need something more permanent that I can plug in to the wall, any suggestions on what component(s) I should buy?

Thanks again for all your help! It IS appreciated!

Mark

SlotBox2.fzz (11.4 KB)

Thanks for that, it is a whole lot easier to see.

You still have the same layout problem though.

The bit where the 9V battery negative lead joins all the resistors, and then is bridged to the negative line on the bread board is wrong. Take that point and do not connect it to the breadboard ground but connect it to the ground on the arduino next to the 5V line.

This stops the current from your relays making the "ground bounce" for your sensors.

Having said that about the grounds, why are the LDRs connected to Vcc?

Why not connect them to ground - back on the Arduino module - and use pull-ups to Vcc. This makes for safer wiring when you have them connected at a distance, trivial difference in the code. Also I believe (needs verification) you can still use the internal pull-ups on the Analog pins instead of 10k resistors, which they approximate. Linearity of response will be different but hey - that is hardly of relevance when using LDRs!

Also I believe (needs verification) you can still use the internal pull-ups on the Analog pins instead of 10k resistors

Verified, yes you can.

However using the higher internal pull ups will make it more prone to picking up interference which is what the problem is here.

Paul__B:
Having said that about the grounds, why are the LDRs connected to Vcc?

Why not connect them to ground - back on the Arduino module - and use pull-ups to Vcc. This makes for safer wiring when you have them connected at a distance, trivial difference in the code. Also I believe (needs verification) you can still use the internal pull-ups on the Analog pins instead of 10k resistors, which they approximate. Linearity of response will be different but hey - that is hardly of relevance when using LDRs!

OK.... I'm a bit lost here! :frowning:

What's the best way to proceed here? I need this to be consistent. It is going to be installed in my fathers Slot Car race track, where they currently have a working (Parallel port -> Win95) solution.

I am not sure what you mean Paul? Mike, I get the bit you mentioned about the grounds, and I have amended as shown in the image attached.

I am looking to build this fully over the weekend, but will be wanting to have all the bits I need. So if I need a new power supply (the 9v on here is just for demonstration) for the LDRs and advise on what I am needing to do would be appreciated! :slight_smile:

Or is there a better way to connect these LDRs?

Thanks,
Mark

tip2tail:
What's the best way to proceed here? I need this to be consistent. It is going to be installed in my fathers Slot Car race track, where they currently have a working (Parallel port -> Win95) solution.

Noted. This is a somewhat adverse environment, so you do need to get it right.

tip2tail:
I am not sure what you mean Paul? Mike, I get the bit you mentioned about the grounds, and I have amended as shown in the image attached.

Sorry, Still got it wrong!

OK, my first point is that you should not be running your 5V line from the Arduino across your race track board to LDRs (or anything else if you can avoid it). The LDRs should be connected to ground so that only ground wires (and the inputs themselves) are running around the board. Now if you use the resistors as pull-ups they should be going to Vcc. Mike feels that you are better to use 10k resistors, but I am suggesting that the internal pull-up function in the Arduino for each pin (by writing them HIGH using digitalWrite even though they are defined as inputs) should equate to a 10k pull-up. It may simply have a different linearity - it is not actually a resistor, but for your purpose, it should be OK. In fact, there is no reason why you should actually need to read them as analog - if they are presently being sensed by a parallel port then they are already being read as digital inputs - you presumably only wish to know whether are shadowed or not.

As to the grounds themselves, the ground from the power supply goes to the Arduino, not somewhere else. The ground to the LDRs goes to the Arduino. The ground for your output devices goes to the Arduino (unless you were to use opto-isolators in which case only the grounds from the inputs to the opto-isolators goes to the Arduino).

tip2tail:
I am looking to build this fully over the weekend, but will be wanting to have all the bits I need. So if I need a new power supply (the 9v on here is just for demonstration) for the LDRs

The LDRs consume negligible power. Your concern may be for how much power the output devices use.

Wow! Now I am even more confused! I am totally struggling here!

Is there any chance you could show me what you mean in a diagram?

Let me clarify some things here:

  • The Parallel Port/Win95 system is being retired.
  • A new PC, using the Arduino and USB will be installed to run the new software I have written.
  • The 4x tracks & 4x lights are powered (via the the relays) by a seperate 12v DC supply.
  • The Arduino is not plugged into a wall socket, just the USB.

I really don't know what to do next, I just need to be able to detect when a car passes by the LDRs and breaks the beam so I can send a signal back to the PC. There is plenty of scope for me to plug in another power source if needed, but if someone can show me what you guys mean I would be most appreciative! :slight_smile:

Mark

Wow! Now I am even more confused! I am totally struggling here!

What you need to appreciate is that there is no 100% sure fire way of predicting if you will or will not get some interference in any given situation.

What you can do is to take as many precautions as you can and test it. If you get trouble then you modify and test again.

That last thing you posted was right - ish as far as I could tell although you will not physically have the layout like that will you? The LDRs will be on long wires over to various parts of the track. You reduce the chances if your pull ups are small, 10K is a good start but you might have to go smaller. Then if there is trouble you can put capacitors across the analogue inputs. You have to test first nothing like this works first time.

You started the post with a specific problem and we gave you a solution. What you have not done is to test what we told you to do and report back.

Grumpy_Mike:
That last thing you posted was right - ish as far as I could tell although you will not physically have the layout like that will you? The LDRs will be on long wires over to various parts of the track. You reduce the chances if your pull ups are small, 10K is a good start but you might have to go smaller. Then if there is trouble you can put capacitors across the analogue inputs. You have to test first nothing like this works first time.

You started the post with a specific problem and we gave you a solution. What you have not done is to test what we told you to do and report back.

Fair comments Mike. I plan on setting up a test over the weekend.

No, my physical layout will be quite far removed from what you see. Perhaps I would be better drawing a proper electrical schematic of the circuit. Need to try and regress to 2002 and Higher (Scottish A Level) Physics. Once I was out of there that was memory banked never to be used again or so I thought!!!

I do appreciate the help! The one thing, before I go out testing this on my test track etc that I'm going to build, is the power for the LDRs.

I THINK that I need something like a pin adapter similar to the one on the Arduino itself (pic attached), then an appropriate wall plug for that. Although Ive not been able to find such a thing on eBay - perhaps my search terms have been wrong?

Thanks (again)!

Mark

I do appreciate the help! The one thing, before I go out testing this on my test track etc that I'm going to build, is the power for the LDRs.

I would just run 5V from the arduino for that. It takes very little current so there is no need for a separate supply for them.