Analog input gets noise from power supply

Hi,
I'm trying to add an optical encoder to my project. Arduino Mega is connected to USB and 12V PSU. Arduino ground is connected with PSU ground additionally, though this probably has no effect.
Phototransistor collector is connected to arduino 5V pin, emitter to analog input and through diode/10K resistor to ground. (there are two phototransistors, but since there's diode on each I assume they can't interfere)
When PSU is off I get readings of 7-12, which is logical, as photoresistor picks some ambient lighting. When PSU is on I get the following:

It is probably not the analog input that is getting the PSU noise, but the analog reference.

Try adding a big fat capacitor across the +5V and GND connections to reduce noise and fluctuations on the Vcc rail.

Either that or use an external precision voltage reference source to the Aref pin.


Apparently noise frequency is close to 50Hz, which is no surprise.
But how is that possible? I mean - ok - input voltage is jumping, but point of reference is jumping with it then, and readings should be consistent anyway. No?

majenko:
It is probably not the analog input that is getting the PSU noise, but the analog reference.

Try adding a big fat capacitor across the +5V and GND connections to reduce noise and fluctuations on the Vcc rail.

Either that or use an external precision voltage reference source to the Aref pin.

Thanks for fast reply. Fattest I have at hand is 1000uf. BTW - is it possible to damage voltage regulator when turning on the board with too large capacitor?

Thanks for fast reply. Fattest I have at hand is 1000uf. BTW - is it possible to damage voltage regulator when turning on the board with too large capacitor?

Theoretically, yes, but you'd need a very very large capacitor. You're more likely to exceed the USB inrush specifications when powering solely from USB.

I have an ADC board with a 1000uF capacitor (and others) on it to give more accurate readings, and it doesn't cause any inrush problems.

Result with 1000uf:

Much better. Thanks!
Guess I need to try even bigger cap, though it might not fit in the box physically :slight_smile:

Why do you need analog inputs for reading encoder? Why not digital?
And

Arduino ground is connected with PSU ground additionally, though this probably has no effect.

I believe it could have effect, though it's not clear why do you need this PSU connected to arduino.

Magician:
Why do you need analog inputs for reading encoder? Why not digital?
And

Arduino ground is connected with PSU ground additionally, though this probably has no effect.

I believe it could have effect, though it's not clear why do you need this PSU connected to arduino.

Output from phototransistor needs some filtering. It's quite low so it'd need amplification, it's noise induced spikiness may trigger digital input multiple times, and treshold is not constant as environment lighting is shifting it. So I thought filtering analog input digitally would make things simpler.
I tried to disconnect ground and it seems making things noisier or no difference. But you're right - probably I'll have to put arduino on another PSU. I tried to put a load on the PSU (outrunner motor, ammeter shows about 7A) and the cap isn't helping anymore - analog input goes just crazy. I tried AREF external, which made things a bit better, but still below getting distinct treshold.

BTW -is there any difference between GND/5V pins Mega has on the 22-53 pin end and ones marked Power?

and treshold is not constant as environment lighting is shifting it.

O'k, I see. Still think digital inputs better for "pulse" wave form. What is pulse rate you expecting, if you aware that analogRead is pretty slow (110 usec), plus if you gonna implement flltering, let say 8 running average it slow down to ~1 msec, limiting your signal to 500 pulses/per second or so.
Filtering digital is not big deal, there are some debounce library that design for this.
Changing lighting conditions could be fixed setting cap (in series)/ resistor (pull down) before digital in, cutting off DC.
Amplification / scaling probably would require additional hardware, but two OPA in one package should be dirty cheap.
BTW,

When PSU is off I get readings of 7-12, which is logical, as photoresistor picks some ambient lighting.

Do you mean phototransistors?

Phototransistor collector is connected to arduino 5V pin, emitter to analog input and through diode/10K resistor to ground.

That is not right. Connect the emitter directly to the analogue input and connect the resistor from the analogue input to ground. You might want to increases this to 100K. It looks like the input was half floating the way you had it and that is why you are picking up noise.

Thank you very much for replies!

The part I didn't say, considering irrelevant - I have a 1.5x1m steel frame, that everything is mounted on. Arduino is inside plastic box, bolted on nylon bolts, with wires going from it to sensors. PSU is lying on the frame near by.

I started to experiment with another PSU and noticed some inconsistency in noise results. Annoyed I removed all connections to the PSU and the correlation between turning on the PSU and noise stayed there! Only logical assumption is induction, so I moved the PSU away from all the wires, but noise was still there. Noise disappeared only when I moved PSU away from the frame. So it seems that the frame is picking up noise from the PSU and inducing it again onto wires going around it.
This seems strange, considering the PSU has aluminum case not connected to any of in/out +/-. A bit annoying is that I have to move PSU at least 5cm from any part of the frame to get "noise" in range of 10-20 units. If things are as they seem to be, it looks I'll have to separate the PSU from main thing at all or practice woodworking :slight_smile:

On other hand - computer has powerful PSU and lots of sensitive electronics. How come that works?

Do you mean phototransistors?

Yes

Magician:
What is pulse rate you expecting, if you aware that analogRead is pretty slow (110 usec), plus if you gonna implement flltering, let say 8 running average it slow down to ~1 msec, limiting your signal to 500 pulses/per second or so.
Filtering digital is not big deal, there are some debounce library that design for this.

I have motors running 500RPM max, with 8 "light holes" per revolution, so I'm expecting 67 pulses/sec. Should survive severe filtering :slight_smile:

Grumpy_Mike:
That is not right. Connect the emitter directly to the analogue input and connect the resistor from the analogue input to ground. You might want to increases this to 100K. It looks like the input was half floating the way you had it and that is why you are picking up noise.

Guess I wrote it badly. It is as you say, except I added diode between input and ground to avoid interfering with other inputs.

I added diode between input and ground to avoid interfering with other inputs.

That doesn't make sense, adding a diode like that will either short out the pull down resistor or do nothing depending on what way round it is.

If one analogue input is interfering with another input then you have got something wrong.

On other hand - computer has powerful PSU and lots of sensitive electronics. How come that works?

Because you have got something wrong perhaps?
It sounds for all the world like you have a floating input.

Grumpy_Mike:
That doesn't make sense, adding a diode like that will either short out the pull down resistor or do nothing depending on what way round it is.

This is the circuit I have:

Grumpy_Mike:
Because you have got something wrong perhaps?
It sounds for all the world like you have a floating input.

I'm 95% sure I'm doing something wrong and I tried it this way:

The result is:

On the left there is a led at the sensor to rise input from 0. Correlation between values and lighting is stable and proportional. PSU 1m away from the frame.
On the right - didn't touch anything, but put the PSU on the frame 1m away from Arduino.

This is the circuit I have:

I had imagined that the diode was in series with the resistor. Anyway it is not right as it limits the low voltage you can achieve.

If it were me I would take the second circuit and swap it around with the emitter to ground and the collector to the input with the resistor pulling up to 5V. That is the way I always do it.

Your power supply should not be having such an effect and normally when this happens is because the impedance of the input to the analogue inputs is too high. When this happens you also find that one input appears to affect the others.

How long wire to sensors? Shielded cable? May be it would make a difference to put a cap close to inputs.

This might be irrelevant but while trying to put multimeter to anything I could imagine I got slight electric shock touching Arduino ground and PSU case. I tried measuring voltage across ground and case - probes produce sparks from time to time, the voltage jumps for a second and backs to 0V, but a few times, probably having bad contact, I was able to get stable 86V reading. Only thing arduino is connected to is PC through USB and there seems to be no voltage measuring PSU case to USB cable shell or any of it's leads directly.
Voltage seems to jump for a split second if measuring AC mains to PSU case, no sparks. Absolutely nothing between PSU output +/- and it's case.

Magician:
How long wire to sensors? Shielded cable? May be it would make a difference to put a cap close to inputs.

Cables are about 20cm, non-shielded. How big cap could be of use? On the sensor or Arduino side?

Grumpy_Mike:
Your power supply should not be having such an effect and normally when this happens is because the impedance of the input to the analogue inputs is too high. When this happens you also find that one input appears to affect the others.

Actually I didn't see any interference between inputs, it just seemed logical that current could flow back through another input's resistor and affect it, not much of course.