Pages: [1]   Go Down
Author Topic: Speeding up readings of MULTIPLE SHT1x or SHT7x sensors  (Read 1303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using the library at https://github.com/practicalarduino/SHT1x/blob/master/SHT1x.cpp, 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.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13905
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
...
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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13905
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

<hack hack hack>

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
Code:
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;
}


Code:
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...
Code:
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 smiley-wink
« Last Edit: February 12, 2013, 01:35:42 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13905
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: