I am using a JIC 139 UV Sensor (datasheet) with an Arduino Mega 2560 and should be reading smooth curves as I bring a UV source to towards the sensor. Instead, my curves are increasing and oscillating. How can I remove the oscillations and what is the cause of these oscillations?
Code:
float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;
for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;
delay(100);
return(runningValue);
}
void setup() {
Serial.begin(9600);
}
void loop() {
float pinRead0 = averageAnalogRead(0);
float pVolt0 = pinRead0 / 1024.0 * 5.0;
// In mW/m^2
Serial.println(pVolt0);
// Serial.println(mapfloat(pVolt0, 0.0, 5.0, 0.0, 15.0));
delay(100);
}
`
Here is a sample screenshot from the Serial Plotter at 3 different distances from the UV Source: enter image description here
Would post cicuit but don't have enough reputation. Will post as response. edit: I added a very large capacitor(3300 uf) between the output pin and ground which seemed to smooth the values a little bit but the noise still exists
edit: The x-axis is number of readings and the y-axis is voltage(0-5v) calculated from analogRead()
-
2It is not your sketch, the sketch is okay. How stable is the 5V of the Arduino Uno ? It is most likely the analog circuit.Jot– Jot2017年06月29日 20:29:44 +00:00Commented Jun 29, 2017 at 20:29
-
5 volt is stable at 5.01, any ideas as to what may be wrong with the analog circuit?Andrew Walker– Andrew Walker2017年06月29日 21:19:34 +00:00Commented Jun 29, 2017 at 21:19
-
What is the x-axis scale? (Eg, is it reading number, time, etc)? [Please edit reply into your question]James Waldby - jwpat7– James Waldby - jwpat72017年06月29日 21:34:37 +00:00Commented Jun 29, 2017 at 21:34
1 Answer 1
Without seeing circuit and construction details and oscilloscope traces, it's difficult or impossible to say what the problem is. It may be due to op amp instability due to a large capacitive load. It may reflect a beat frequency between some sensor and MCU signals, or aliasing between 60-Hz noise and your data-conversion rate.
Regarding op amp instability, see for example the article Op Amps Driving Capacitive Loads, by Grayson King. Here is a quote from that article:
Q. How does capacitive loading affect op amp performance?
A. To put it simply, it can turn your amplifier into an oscillator. Here's how: [...]
Rather than just connecting that big 3300 μF capacitor to the output pin, perhaps instead try attaching an Active Low Pass Filter to the output, or a unity-gain op amp followed by a Passive Low Pass Filter; etc. Put big capacitors across power into the UV source and power into the sensor, instead of on the sensor output.
Regarding beat frequencies and aliasing, low-frequency oscillation like that seen in the trace may occur as a difference of two higher-frequency signals, or as a sampling artifact. To check this out, attach an oscilloscope to the UV source and see if its power consumption oscillates; and/or try a more-rational sampling scheme than that shown in your code.
For example, instead of taking 8 readings within one millisecond in averageAnalogRead()
and then waiting 100 ms, perhaps add a delay(1)
inside the loop so that readings are spread out slightly more. Remove the delay(100)
from within averageAnalogRead()
. Compensate by changing the other delay(100)
in loop()
to, for example, delay(191)
or delay(193)
. Try various delay amounts to see if they have an effect on the waveform.
Explore related questions
See similar questions with these tags.