Hello everyone! I am working on improving a temperature/sensor package I have been working on. I am using a 1V input HMP60 temp/humidity sensor read into a adafruit 16bit ADC using 4x gain. I am having a hard time wrapping my head how to do a certain action though. I have written my code (included below to smooth out my readings, which is working quite well) but I am really confused why it is taking so long to get readings. The data sheet on the ADC claimed up to 860 samples per second, but my code must be doing something wrong because it takes FAR FAR longer than 1 second to get through my for loop. My guess is I'm missing something pretty simple, but am having a hard time figuring out where to go. The plan is to use a real-time clock and using post-processing syncing the arduino datalogger info with the data logger from the flight controller being used on a drone. Basically what I want to do is to be able to log my arduino data to a CSV in 1 second increments. So basically, take samples, smooth, and record data in the time frame of 1 second. If you have any more questions I have left out please let me know!
#include <Wire.h>
#include <Adafruit_ADS1015.h>
//Use Adadfruit library for ADS
Adafruit_ADS1115 ads;
void setup(void)
{
Serial.begin(9600);
analogReference(INTERNAL); //use internal reference for voltage
Serial.println("Hello!");
Serial.println("Getting single-ended readings from HMP60");
// The ADC input range (or gain) can be changed via the following
// function. Since our HMP60 has an input voltage of 1.0V we can set the gain to 4x to help
// improve resoultion
//
ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.03125mV
ads.begin();
}
void loop(void)
{
int16_t adc0, adc1, adc2, adc3;
float tempSensorVal = 0;
float smoothedTempVal = 0;
float humiditySensorVal = 0;
float smoothedHumidityVal = 0;
int samples = 860;
for (int i = 0; i < samples; i++){
adc0 = ads.readADC_SingleEnded(0);
float humiditySensorVal = (adc0 / 32768.0) * 100;
adc1 = ads.readADC_SingleEnded(1);
float tempSensorVal = (adc1 / 32768.0) * 100.0 - 40.0;
smoothedHumidityVal = humiditySensorVal + ((humiditySensorVal - smoothedHumidityVal) / samples);
smoothedTempVal = tempSensorVal + ((tempSensorVal - smoothedTempVal) / samples);
}
Serial.print("Humidity: "); Serial.println(smoothedHumidityVal);
Serial.print("Temperature: "); Serial.println(smoothedTempVal);
Serial.println(" ");
delay(10);
}
860 samples per second, but I see you are doing 2 samples per iteration, and a lot of other things in the loop too. Those take time too, especially divisions
try this, stripped the loop a bit, still taking the average of the readings.
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads;
const int samples = 860;
void setup(void)
{
Serial.begin(9600);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from HMP60");
analogReference(INTERNAL); //use internal reference for voltage
// The ADC input range (or gain) can be changed via the following
// function. Since our HMP60 has an input voltage of 1.0V we can
// set the gain to 4x to help improve resolution
// 4x gain +/- 1.024V 1 bit = 0.03125mV
ads.setGain(GAIN_FOUR);
ads.begin();
}
void loop(void)
{
int32_t adc0, adc1, adc2, adc3; //
float temperature = 0;
float humidity = 0;
adc0 = 0;
adc1 = 0;
for (int i = 0; i < samples; i++)
{
adc0 += ads.readADC_SingleEnded(0);
adc1 += ads.readADC_SingleEnded(1);
}
adc0 /= samples;
adc1 /= samples;
float humidity = adc0 * 0.003051758;
float temperature = adc1 * 0.003051758 - 40.0;
Serial.println( millis() );
Serial.print("Humidity: "); Serial.println(humidity);
Serial.print("Temperature: "); Serial.println(temperature);
Serial.println(" ");
}
can youpost some output ?
the millis() was added to see how much time the samples take
Write a short sketch that just reads the sensor and ignores the results. Use that to work out how long each sample takes.
I came across another Thread (reading a weight sensor IIRC) in which the read took some time but it was possible to initiate the sample and do other stuff until the value was ready. A bit like buttering the toast while waiting for the kettle to boil.
You're using a baud rate of just 9600. That can only send 1200 bytes per second. I estimate you're Serial.printing about 30 chars per loop. So even if you strip out everything else, it can only manage about 40 iterations of the loop per second.