Search code examples
arduinoarduino-unoandroid-sensorsproximitysensor

Abrupt jumps in the Arduino UNO sensor data output


I am using Arduino UNO to record CNY70 sensor data. The code that I have used is given below. This code is written to record values for 10 seconds and stop after that.

#include <EEPROM.h>
const int chipSelect = 2;
unsigned int y1 = 0;
unsigned long y = 0;

String data = "";
void setup()
{
  Serial.begin(230400);
  delay(10);
}

void loop()
{
  while (y < 10000000)
  {
    if (y1 > 372) //start recording only after 372 microseconds
    {
      y = micros();
      data += y;
      data += ",";
      data += analogRead(A0);
      Serial.println(data);
      data = " "; //clear data after printing the value
    }
    y1 = micros() - y;
  }
}

The values are copied from the Serial Monitor. Some recordings show a sudden jump in one or two of the recorded values, and because of that, the graph of the data looks like it has been 'struck' out. The figures are: Jump in value of the sensor data Zoomed image of the sensor data

These abrupt values do not appear every time the code is run but they do appear once every 7-12 runs. What is the cause of these abrupt jumps? There are no abrupt changes in the experimental conditions. How to prevent the Arduino from recording and storing such abrupt values?


Solution

  • Your values don't suddenly jump, they drop to 0. Normally you can only measure 0 if you have a short to ground. So maybe you have a broken cable and due to vibrations.... I don't know your test setup.

    Test 1) measure a constant voltage

    Test 2) try another input pin

    If you cannot fix it, just check the read values. If they don't make sense, skip them or replace them by an extrapolation.