0

I am using Si7021 silicon lab's temperature and humidity sensor and APD300 light sensor together in my PCB board, I am also using STM32F103 Arduino compatible microcontroller which communicates the sensors through the I2C protocol, I have tested it before and the sensors were reading the correct sensor data in the Arduino Serial monitor, but now my humidity and temperature sensor is reading way too many values like "219982828282 C" even though I am using the correct pull-up resistors, I was wondering if there is something wrong with the code I am using (Found it from the internet). I have attached the source code I used and the serial monitor output of my sensors.

#include <Wire.h>

// SI7021 I2C address is 0x40(64)
#define Addr 0x40

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);
}

void loop()
{
  unsigned int data[2];

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send humidity measurement command, NO HOLD MASTER
 Wire.write(0xF5);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // humidity msb, humidity lsb 
  if(Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }

  // Convert the data
  float humidity  = ((data[0] * 256.0) + data[1]);
  humidity = ((125 * humidity) / 65536.0) - 6;

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Send temperature measurement command, NO HOLD MASTER
  Wire.write(0xF3);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(500);

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // temp msb, temp lsb
  if(Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }

  // Convert the data
  float temp  = ((data[0] * 256.0) + data[1]);
  float cTemp = ((175.72 * temp) / 65536.0) - 46.85;
  float fTemp = cTemp * 1.8 + 32;

  // Output data to serial monitor
  Serial.print("Relative humidity : ");
  Serial.print(humidity);
  Serial.println(" % RH");
  Serial.print("Temperature in Celsius : ");
  Serial.print(cTemp);
  Serial.println(" C");
  Serial.print("Temperature in Fahrenheit : ");
  Serial.print(fTemp);
  Serial.println(" F");
  delay(500);
}

Arduino Serial Monitor Output:

Illumination: 101 Relative humidity : 65802760.00 % RH Temperature in Celsius : 92502848.00 C Temperature in Fahrenheit : 166505152.00 F

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
A.Ali
  • 13
  • 4
  • you should use `byte data[2]`, avoid those float calculations to combine two bytes, and check the raw data with your data sheet. – datafiddler Mar 04 '19 at 11:09
  • If there's no response from your device, you do not get an error but unpredictable nonsense. – datafiddler Mar 04 '19 at 11:17
  • I used this code before and it was working, there was no problem with the float calculations. I also did the calculations according to the datasheet – A.Ali Mar 04 '19 at 11:27
  • OK, remains the error handling issue ;) – datafiddler Mar 04 '19 at 11:28
  • is there any chance that my sensor is broken or not working even though it's sending these large numbers? – A.Ali Mar 04 '19 at 11:30
  • If it's not connected at all, `data[]` has any undefined value, which is propagated to your float variables. Perhaps you check your `if(Wire.available()) { }` structure and/or the return value of `Wire.requestFrom()` – datafiddler Mar 05 '19 at 11:13

0 Answers0