I am trying to set up a pressure sensor (this model) using an MKR Wifi 1010. Everything has been working ok, but all of the sudden, I start getting a LOT of extra noise from the sensor reading. Here is the code I am using and my setup:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float average;
float pressureValue;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
const int pressureInput = A1; // select the analog input pin for the pressure transducer
const int pressureZero = 102.4; // analog reading of pressure transducer at 0psi
const int pressureMax = 1023; // analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 76; // psi value of transducer being used
const int sensorreadDelay = 1000; // constant integer to set the sensor read delay in milliseconds
float analogValue = 0; // variable to store the value coming from the analog input
//float pressureValue = 0; // variable to store the value coming from the pressure transducer
const int numReadings = 10;
int readingIndex = 0;
float total = 0;
float readings[numReadings];
//float average = 0;
void setup() {
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
Serial.begin(9600);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
analogValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
pressureValue = ((analogValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
total = total - readings[readingIndex]; // Remove the oldest entry from the total
readings[readingIndex] = pressureValue; // Add the newest reading to the window
total = total + pressureValue; // Add the newest reading to the total number of readings
readingIndex = (readingIndex+1) % numReadings; // Increment the index, and wrap to 0 if it exceeds the window size
average = total / numReadings; // Divide the total of the window by the window size for the result
Serial.print("Reading: ");
Serial.println(pressureValue);
Serial.print("Current Avg: ");
Serial.println(average);
Serial.println("***************************");
delay(sensorreadDelay); //delay in milliseconds between read values
}
Here is a chart showing the readings. You can see there is a small amount of noise, which I am averaging out, but then, the noise goes crazy. I was able to get the noise to go away by unhooking the pressure sensor from the 5v and re-attaching it. When I did that, the noise went away.
Any ideas on what is causing this? This has been happening frequently.
A couple other bits of info:
- The sensor is plugged into a standard compressor and I know the pressure is stable.
- The board is being powered through the usb.