Speeding up readings of MULTIPLE SHT1x or SHT7x sensors

Hi,

I was wondering if there is some way of reducing the time to get all the relevant measurementsf from multiple SHT7x sensors.

I am using the SHT1x library and reading 2 sensors but want to read more.

Just to do the following takes at least 5 seconds on an UNO. and this is only 2 sensors.

temperatureA = tempSensorA.readTemperatureC();
humidityA = tempSensorA.readHumidity();

temperatureB = tempSensorB.readTemperatureC();
humidityB = tempSensorB.readHumidity();

The rest of the code around works very fast. If I // these lines out, my other sensors are working very fast.

Thanks

Peter

How long does the data sheet say it takes to get a reading?

The datasheet suggests not taking more than 1 reading per second to reduce the chance of self heating. But In reality I think it is quite a bit less. The point is that each time it reads a different sensor, it starts from the beginning.

I was wondering if there was a way with the same CLK signals etc read both / all sensors at the same time for example. So I could read them all in 1 second as opposed to 1 second or more for each?

I know that digitalwrites are fairly slow on the arduino and looking at the SHT1x code, it needs a lot of them,so I doubt without rewriting the library in C, it could be sped up much.

BUT there might be a way to read themall as I said at the same time. Unfortunately my programming skills and understanding all the datsheets isn't up to it.

If you are using the library at SHT1x/SHT1x.cpp at master · practicalarduino/SHT1x · GitHub, you will see that there is a delay(10) call at line 140 of the .cpp file. Try using a much smaller delay such as delayMicroseconds(10), or even no delay.

You might also want to reduce the delay(10) at line 193 to delay(1), and increase the loop limit in line 191 from 100 to 1000.

I had a similar problem with the DS18B20 long ago and I implemented an asynchronous call to the Dallas Temperature lib that allows me to do the following

...
void setup()
{
  Serial.begin(9600);
  if (!sensors1.getAddress(Thermometer1, 0)) Serial.println(F("Temp1 failure"));

  sensors1.setResolution(Thermometer1, 9);
  sensors1.setWaitForConversion(false);  // set async mode
  sensors1.requestTemperatures(); // request a new temperature reading
}

void loop()
{
  
  do some things

  if (millis() - lastTemp >=  94)
  {
    temp = sensors1.getTempC(Thermometer1);  // read the temperature 
    sensors1.requestTemperatures();  // request a new measurement
    lastTemp = millis();
  }

   do other things here
}

The trick is that I decoupled the start of a new measurement and the reading of the result. By decoupling them I could save almost the complete conversion delay.

I don't know the SHT lib but a similar code addition might solve your performance issue.

A quick look at the lib dc42 pointed to.

there is a line that states : waitForResultSHT(_dataPin);

That is just a blocking wait call, you need to get rid off, so lets hack a bit

add these commands to the library: cannot test as I have no such sensor
You might do the same trick for humidity.

do not forget to add the protos in the .h file too

void SHT1x::requestTemp()
{
  // Command to send to the SHT1x to request Temperature
  int _gTempCmd  = 0b00000011;
  sendCommandSHT(_gTempCmd, _dataPin, _clockPin);
}

bool SHT1x::dataReady()
{
  pinMode(_dataPin, INPUT);
  return digitalRead(_dataPin) == LOW;
}

float SHT1x::asyncReadTempC()
{
  const float D1 = -40.0;  // for 14 Bit @ 5V
  const float D2 =   0.01; // for 14 Bit DEGC
  
  int _val = getData16SHT(_dataPin, _clockPin);
  skipCrcSHT(_dataPin, _clockPin);

  return (_val * D2) + D1;
}
void setup()
{
  ...
  SHT.requestTemp();
}

void loop()
{
  do some here
  
  if (SHT.dataReady())
  {
    temp = SHT.asyncReadTempC();
	SHT.requestTemp();
  }
  
  do the rest here
}

as said not tested, so please post the lib if you got it working.

and please change this in the lib, makes it more maintainable...

float SHT1x::readTemperatureF()
{
  return readTemperatureC() * 1.8 + 32;
}

Lets post a mail to jon.oxer (maintainer of the lib)

update - mailed jon - time for a cold beer :wink:

Humidity in asuync mode is a bit more complicated as it needs the temperature.

tip:
first read the temperature and give this as a param to asyncREadHumidity(float tempC);

so it can correct the raw humidity.