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?


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;



void setup() {

void loop() {

   float pinRead0 = averageAnalogRead(0);
   float pVolt0 = pinRead0 / 1024.0 * 5.0;

   // In mW/m^2
   //  Serial.println(mapfloat(pVolt0, 0.0, 5.0, 0.0, 15.0));



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

  • 2
    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, 2017 at 20:29
  • 5 volt is stable at 5.01, any ideas as to what may be wrong with the analog circuit? 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] 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.