0
votes

I have this D6T Omron temperature sensor (1x8 array) and trying to get temperature readings from it. However, after debugging and making sure there's no error, I can't get anything outputted but simply 0's. Here's the code, basically modified from an example that is contained in the SoftI2CMaster library from github, more information here http://playground.arduino.cc/Main/SoftwareI2CLibrary

// Simple sketch to read out BMA020 using SoftI2C
// Readout BMA020 chip
// use low processor speed (you have to change the baud rate to 2400!) 
// #define I2C_CPUFREQ (F_CPU/8)
#define NO_INTERRUPT 1
#define I2C_TIMEOUT 1000

#define SDA_PORT PORTC
#define SDA_PIN 4
#define SCL_PORT PORTC
#define SCL_PIN 5
#include <SoftI2CMaster.h>
#include <avr/io.h>


#define BMAADDR 0x14

float PTAT, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, PEC;

void CPUSlowDown(void) {
  // slow down processor by a factor of 8
  CLKPR = _BV(CLKPCE);
  CLKPR = _BV(CLKPS1) | _BV(CLKPS0);
}


boolean setControlBits(uint8_t cntr)
{
  Serial.println(F("Soft reset"));
  if (!i2c_start(BMAADDR | I2C_WRITE)) {
    Serial.println("failed at write start");
    return false;
  }
  if (!i2c_write(0x4C)) {
    Serial.println("failed at write");
    return false;
  }
  i2c_stop();
  return true;
}

boolean initBma(void)
{
  if (!setControlBits(B00000010)) return false;;
  delay(100);
  return true;
}

int readOneVal(boolean last)
{
  uint8_t msb, lsb;
  lsb = i2c_read(false);
  msb = i2c_read(last);
  if (last) i2c_stop();
  return (float)((msb<<8)|lsb)/64;
}

boolean readBma(void)
{
  PTAT = 0xFFFF;
  P0 = 0xFFFF;
  P1 = 0xFFFF;
  P2 = 0xFFFF;
  P3 = 0xFFFF;
  P4 = 0xFFFF;
  P5 = 0xFFFF;
  P6 = 0xFFFF;
  P7 = 0xFFFF;
  PEC= 0xFFFF;
  if (!i2c_start(BMAADDR | I2C_WRITE)) return false;
  if (!i2c_write(0x01)) return false;
  if (!i2c_rep_start(BMAADDR | I2C_READ)) return false;
  PTAT = readOneVal(false);
  P0 = readOneVal(false);
  P1 = readOneVal(false);
  P2 = readOneVal(false);
  P3 = readOneVal(false);
  P4 = readOneVal(false);
  P5 = readOneVal(false);
  P6 = readOneVal(false);
  P7 = readOneVal(false);
  PEC = readOneVal(true);
  return true;
}



//------------------------------------------------------------------------------
void setup(void) {
#if I2C_CPUFREQ == (F_CPU/8)
  CPUSlowDown();
#endif
  Serial.begin(19200); // in case of CPU slow down, change to baud rate / 8!
  if (!initBma()) {
    Serial.println(F("INIT ERROR"));
  }

}

void loop(void){
  if (!readBma()) Serial.println(F("READ ERROR"));
  Serial.print(F("PTAT="));
  Serial.println(PTAT);
  Serial.print(F("  P0="));
  Serial.println(P0);
  Serial.print(F("  P1="));
  Serial.println(P1);
  Serial.print(F("  P2="));
  Serial.println(P2);
  Serial.print(F("  P3="));
  Serial.println(P3);
  Serial.print(F("  P4="));
  Serial.println(P4);
  Serial.print(F("  P5="));
  Serial.println(P5);
  Serial.print(F("  P6="));
  Serial.println(P6);
  Serial.print(F("  P7="));
  Serial.println(P7);
  Serial.print(F("  PEC="));
  Serial.println(PEC);
  delay(300);
}

Please any help would be appreciated :(

1
Why not use the Wire library? arduino.cc/en/reference/wireMartin Thompson

1 Answers

0
votes

I have a couple of recommendations.

First, the data that is returned from your temperature device is not sent to you as a floating point value - it is a 16-bit signed integer. For example, if the device senses that it is 25.8 degrees, you will get the value 258 (0x0102) from the device during I2C communications. Also, a quick scan of your code shows that in your readOneVal() routine, you typecast your return as a float, but your function is defined to return an int. The typecast doesn't help much anyway as the data isn't in the format you expect. You will have to convert it yourself.

You really need to take the time to understand your temperature sensor and how I2C works. I would recommend you read the app note the Mouser has here. It is specific to your device and has examples in the C language that are fairly straightforward. That should help to get you on the right track.