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;
}

{
unsigned int runningValue = 0;

for(int x = 0 ; x < numberOfReadings ; x++)

delay(100);

return(runningValue);
}

void setup() {
Serial.begin(9600);
}

void loop() {

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: 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()

• It 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
Jun 29 '17 at 20:29
• 5 volt is stable at 5.01, any ideas as to what may be wrong with the analog circuit? Jun 29 '17 at 21:19
• What is the x-axis scale? (Eg, is it reading number, time, etc)? [Please edit reply into your question] Jun 29 '17 at 21:34

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.