MIT-i Programming Help with Arduino I2C & Sensirion Temp Sensor

Thread Starter

Ryan Jones

Joined Aug 27, 2016
35
@Raymond Genovese @djsfantasi

Thank you! I will give it a shot and see how it comes out.

The next step is setting a threshold value so that actions can be taken if it is above/below a certain temperature, but that part is a piece of cake, I am very capable of the rest, but sharing just in case you all were curious of the next goal!
 
@Raymond Genovese @djsfantasi

Thank you! I will give it a shot and see how it comes out.

The next step is setting a threshold value so that actions can be taken if it is above/below a certain temperature, but that part is a piece of cake, I am very capable of the rest, but sharing just in case you all were curious of the next goal!

So, calculating the checksum is a lot easier for me than fully understanding the procedure and I know that can be dangerous. If you look here http://www.phanderson.com/PIC/16C84/crc.html you can get an idea of what is going on. Although that page is describing the situation for a DS1820 temperature sensor, the procedure for your chip is similar. If you look here https://users.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf you can get even more information, but I felt it was melting my neurons and I have precious few to spare.*

The bottom line is that Sensiron has code in an application note to calculate the checksum which you can then match to the one that was transmitted https://www.sensirion.com/fileadmin...ity_Sensors_Sample_Code_SHTC1_SHTW1_STSC1.pdf - while not specifically for your chip, it was easily modified using information in Table 17 of the data sheet for the chip that you are using. The result is a simple procedure that you can call sending byte1, byte2 and byte3. It will return a byte of '1' if the checksum is good or '0' if it is bad. Then you can handle what to do if the checksum was bad in your program. If you are not having transmission problems or operating in a noisy environment, you will rarely get a bad checksum I would think. Still, why not make use of it since it is there.

I tested the example code below with their example data and several values from your early post and it calculates fine.

Hope this helps and now I have no excuse to avoid begining my taxes.

Regards,

Ray
C:
void setup() {
  Serial.begin(9600);
}

void loop() {

  byte byte1, byte2, byte3;
/*
  byte1 = 102;
  byte2 = 223;
  byte3 = 164;

  byte1=95;
  byte2=70;
  byte3=100;
*/
  byte1=0xBE;
  byte2=0xEF;
  byte3=0x92;


  if (CheckCrc(byte1, byte2, byte3)) {
  Serial.println("CRC is good");
  }
  else {
  Serial.println("CRC is bad");
  }
here: goto here;

}
//==============================================================================
// adapted from Sensiron application note titled
// "Sample Code For the SHTC1, SHTW1 and STSC1 Humidity and Temperature Sensors"
byte CheckCrc(byte byte1, byte byte2, byte byte3) {
  byte b;  // bit mask
  byte crc = 0xFF; // initial value as per Table 17

  // do byte1
  crc ^= (byte1);
  for (b = 8; b > 0; --b)
  {
  if (crc & 0x80) crc = (crc << 1) ^ 0x31;
  else  crc = (crc << 1);
  }
  // do byte2
  crc ^= (byte2);
  for (b = 8; b > 0; --b)
  {
  if (crc & 0x80) crc = (crc << 1) ^ 0x31; // polynomial from Table 17
  else  crc = (crc << 1);
  }
  Serial.print("byte1 (msb)=");
  Serial.print(byte1);
  Serial.print(" byte2 (lsb)=");
  Serial.print(byte2);
  Serial.print(" byte3 (crc)=");
  Serial.println(byte3);
  Serial.print("Calculated checksum=");
  Serial.println(crc);
  // verify checksum
  if (crc != byte3) return 0; // checksum error
  else  return 1;  // all is well
}
*edited to add - for example, I have been using checksum and CRC as though they are interchangeable and they are not. My appreciation for the complexity of this is currently limited.

Mod edit: code tags. Nice job!
 
Last edited by a moderator:

Thread Starter

Ryan Jones

Joined Aug 27, 2016
35
Thank you for everything, @Raymond Genovese ! I couldn't have gotten this completed without your generosity. I have definitely learned something from all of this, including calculating with a checksum! Some of it is still over my head but I've come a long way in understanding this sensor. The MIT-i series appreciates your help!
 
Thank you for everything, @Raymond Genovese ! I couldn't have gotten this completed without your generosity. I have definitely learned something from all of this, including calculating with a checksum! Some of it is still over my head but I've come a long way in understanding this sensor. The MIT-i series appreciates your help!
You are welcome and I am glad to pitch in along with the others. - Ray
 
Top