LDR screws with serial

Hi

Turning off the light shone on an LDR in a potential divider screws with my USB serial monitor.

I have a light dependant resistor (5K to 20M ohm) that I want to use as a qualitative sensor. I thought it would be perfectly good to have a potential divider from +5V to GND; the LDR between +5 and A0, and a 1.3M ohm resistor between A0 and GND. My program is basically the analog input example but printing to the serial monitor as well, and it requires an input from the serial to start.

The problem is, when I shine a bright light on the LDR, I receive the level '1023'... Then I turn the light off and then the trouble starts. It's quite tricky to pin down but the following is very repeatable;
Immediately serial connection is lost by my computer. (Serial port /dev/ttyUSB0 not found). Resetting the arduino doesn't seem to work, and neither does selecting /dev/ttyUSB1. I restart my computer and everything is fine.

I thought it might be shorting the arduino and resetting it but the LED keeps blinking, whereas when it is working properly and is reset the LED remains off whilst it waits for an input from the serial monitor. So either it isn't resetting, or is resetting and immediately receiving something through serial.

I tried with an additional 47K ohm resistor in series with the LDR. This lead to the range of values I could get being 820 (dark) to 860 (bright), rather than what I would expect; 62 (dark), 861 (bright) but that could just be some light leaking in. Again, turning the light off loses serial connection and mucks everything up again.

Using a different pin makes no difference, and removing the LDR from the circuit to just having a 47K / 1M3 potential divider leaves it sitting around 860 to 862 - as expected. Having a 1M3 / 1M3 potential divider leaves it going between 508 and 516 - as expected. Shorting the pin and +5 reads 1023 - as expected. And suddenly going from +5 to GND, GND to +5V and combinations of resistors all work as expected.

Anyone got any ideas what might be going to screw with the serial?

Thanks,

Andrew

Could the light switch be causing a glitch? When the LDR was out of the circuit did you still try turning the light on and off?

Note: the analog inputs are designed for an input impedance of 100k or less. You might want to change your 1.3M resistor to 100k. With the much higher resistor the sampling capacitor doesn't change value fast enough and part of the charge from the previous sample is detected. Shouldn't cause a problem with your serial port.

The lamp is plugged into a separate wall socket from my computer's 'special' 4-way block, my arduino is being powered from the USB. I was 99% sure that the lamp could have no effect. But, with no other option, I tried anyway... The lamp ruins the arduino-computer serial connection. :astonished:
Every time... I've done it about 10 times because I just couldn't believe it. Wow.

Now, as long as I don't switch the lamp, it works great with +5V > LDR > A0 > 23K5 > GND

Thanks!

Now, how can I prevent this from being a problem in the future? The datasheet says the Duemilanove switches between USB and external power automatically, but with preference given to which? Even then though, I would have thought my PC PSU would have protected the arduino, and as it doesn't, would another power supply? How do I protect my serial connection?

Ideally I guess would do something to the lamp to stop it affecting anything else, but also to the arduino and/or my PC to protect them... what should I be googling for each at this stage?

If you replace the LDR with a potmeter, and you switch the light does it still happen? Would indicate that the LDR is OK.

Yes, it isn't a problem with the LDR, it is due to the lamp being turned on or off. So now I'm wondering whether it's earthborne or airborne EMI. I'll try moving them as far away from each other and see if it still happens.