使用arduino软件i2c进行SHT21传感器时的温度值错误

时间:2017-03-30 11:56:05

标签: arduino

我使用SHT21传感器运行此代码以读取温度和湿度:

#include <Wire.h>
#include <Ports.h>
#include "PortsSHT21.h"
#include <SHT2x.h>

//Define soft i2c channels for a sensor
  SHT21 hsensor2 (2); // pins A1 and D5 - Sensor 2


//define variables for temp data
  float h, t;

void setup() 
{
    Wire.begin();
   Serial.begin(9600);
}

void loop()
{
  // Get data from  sensor soft I2C
  hsensor2.measure(SHT21::HUMI);
  hsensor2.measure(SHT21::TEMP);
  hsensor2.calculate(h, t);
    float hum2 = (h);
   float temp2 = (t);
    Serial.println("temp2");
    Serial.println(temp2);     

    delay(500);
}

但总是回归-46.85度。可以找到here is PortsSHT21.cpp code和PortsSHT21.h的源代码here

似乎PortsSHT21.cpp文件的这一部分(第114行)总是为#34; meas [TEMP]&#34;变量

t_C = (175.72 / 16384.0) * meas[TEMP] - 46.85; //T= -46.85 + 175.72 * ST/2^16

请你帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

该传感器看起来与HTU21D和Si7021单元完全相同。地址和转换常量是相同的。确认硬件设置后,我建议,作为调试步骤,尝试其中一个库。我已经成功地使用Si7021替代(没有软件更改)烧毁HTU21D。看起来SHT21芯片也使用相同的I2C接口。

HTU21D Tutorial

SHT21 Datasheet

HTU21D Datasheet

答案 1 :(得分:-1)

使用此温度读取功能。这还会读取多个测量值并获得平均值。

bool Air_Temperature(int Read_Count, int AVG_Type, float &Value_, double &Deviation_) {

    /******************************************************************************
     *  Project     : SHT21 Temperature Read Function
     *  Developer   : Mehmet Gunce Akkoyun (gunce.akkoyun@noxcorp.org)
     *  Revision    : 03.13.00
     *  Relase      : 19.12.2018
     *  AVG Type    : 1-AVG, 2-RMS, 3-EXRMS, 4-MEDIAN, 5-Sigma1RMS
     ******************************************************************************/

    // Set Sensor Definations
    struct Sensor_Settings {
        int     Resolution;
        bool    EoB;
        bool    OCH;
        bool    OTP;
        int     Range_Min;
        int     Range_Max;

    };
    Sensor_Settings SHT21[] {

        {
            14,         // Measurement Resolution
            false,      // Sensor End of Battery Setting
            false,      // On Chip Heater Setting
            true,       // OTP Read
            -40,        // Sensor Range Minimum
            100         // Sensor Range Maximum

        }

    };

    // Control Read Count
    if (Read_Count < 01) Read_Count = 01;
    if (Read_Count > 50) Read_Count = 50;

    // ************************************************************
    // Set Sensor Configuration Byte
    // ************************************************************

    // User Register Bit Definations
    bool User_Reg_Bits_[8] = {false, false, false, false, false, false, false, false};

    // Set Resolution Bits
    if (SHT21[0].Resolution == 14)  { User_Reg_Bits_[7] = false ; User_Reg_Bits_[0] = false ;} // T: 14 bit
    if (SHT21[0].Resolution == 12)  { User_Reg_Bits_[7] = false ; User_Reg_Bits_[0] = true  ;} // T: 12 bit
    if (SHT21[0].Resolution == 13)  { User_Reg_Bits_[7] = true  ; User_Reg_Bits_[0] = false ;} // T: 13 bit
    if (SHT21[0].Resolution == 11)  { User_Reg_Bits_[7] = true  ; User_Reg_Bits_[0] = true  ;} // T: 11 bit

    // Set End of Battery Bits
    if (SHT21[0].EoB == true) {User_Reg_Bits_[6] = true;} else {User_Reg_Bits_[6] = false;}

    // On Chip Heater Bits
    if (SHT21[0].OCH == true) {User_Reg_Bits_[2] = true;} else {User_Reg_Bits_[2] = false;}

    // OTP Reload Bits
    if (SHT21[0].OTP == true) {User_Reg_Bits_[1] = true;} else {User_Reg_Bits_[1] = false;}

    // User Register Defination
    uint8_t User_Reg_ = 0x00;

    // Set Config Register
    if (User_Reg_Bits_[0] == true) {User_Reg_ |= 0b00000001;} else {User_Reg_ &= 0b11111110;}   // User Register Bit 0
    if (User_Reg_Bits_[1] == true) {User_Reg_ |= 0b00000010;} else {User_Reg_ &= 0b11111101;}   // User Register Bit 1
    if (User_Reg_Bits_[2] == true) {User_Reg_ |= 0b00000100;} else {User_Reg_ &= 0b11111011;}   // User Register Bit 2
    if (User_Reg_Bits_[3] == true) {User_Reg_ |= 0b00001000;} else {User_Reg_ &= 0b11110111;}   // User Register Bit 3
    if (User_Reg_Bits_[4] == true) {User_Reg_ |= 0b00010000;} else {User_Reg_ &= 0b11101111;}   // User Register Bit 4
    if (User_Reg_Bits_[5] == true) {User_Reg_ |= 0b00100000;} else {User_Reg_ &= 0b11011111;}   // User Register Bit 5
    if (User_Reg_Bits_[6] == true) {User_Reg_ |= 0b01000000;} else {User_Reg_ &= 0b10111111;}   // User Register Bit 6
    if (User_Reg_Bits_[7] == true) {User_Reg_ |= 0b10000000;} else {User_Reg_ &= 0b01111111;}   // User Register Bit 7

    // ************************************************************
    // Reset Sensor
    // ************************************************************

    // Send Soft Reset Command to SHT21
    Wire.beginTransmission(0b01000000);
    Wire.write(0b11111110);

    // Close I2C Connection
    int SHT21_Reset = Wire.endTransmission(false);

    // Control For Reset Success
    if (SHT21_Reset != 0) {

        // Set Error Code
        Value_ = -101;

        // End Function
        return(false);

    }

    // Software Reset Delay
    delay(15);

    // ************************************************************
    // Read Current Sensor Settings
    // ************************************************************

    // Read User Register of SHT21
    Wire.beginTransmission(0b01000000);
    Wire.write(0b11100110);
    Wire.requestFrom(0b01000000, 1);
    uint8_t SHT21_Config_Read = Wire.read();

    // ************************************************************
    // Write New Settings if Different
    // ************************************************************

    // Control for Config Read Register
    if (SHT21_Config_Read != User_Reg_) {

        // Write User Register of SHT21
        Wire.beginTransmission(0b01000000);
        Wire.write(0b11100110);
        Wire.write(User_Reg_);

        // Close I2C Connection
        int SHT21_Config = Wire.endTransmission(false);

        // Control For Command Success
        if (SHT21_Config != 0) {

            // Set Error Code
            Value_ = -102;

            // End Function
            return(false);

        }

    }

    // ************************************************************
    // Read Sensor Datas
    // ************************************************************

    // Define Measurement Read Array
    float Measurement_Array[Read_Count];

    // Read Loop For Read Count
    for (int Read_ID = 0; Read_ID < Read_Count; Read_ID++) {

        // Define Variables
        uint16_t Measurement_Raw = 0;

        // Send Read Command to SHT21
        Wire.beginTransmission(0b01000000);
        Wire.write(0b11100011);

        // Close I2C Connection
        int SHT21_Read = Wire.endTransmission(false);

        // Control For Read Success
        if (SHT21_Read != 0) {

            // Set Error Code
            Value_ = -103;

            // End Function
            return(false);

        }

        // Read Data Command to SHT21
        Wire.requestFrom(0b01000000, 3);

        // Define Data Variable
        uint8_t SHT21_Data[3];

        // Read I2C Bytes
        SHT21_Data[0] = Wire.read(); // MSB
        SHT21_Data[1] = Wire.read(); // LSB
        SHT21_Data[2] = Wire.read(); // CRC

        // Combine Read Bytes
        Measurement_Raw = ((SHT21_Data[0] << 8) | SHT21_Data[1]);

        // Clear 2 Low Status Bit
        if (SHT21[0].Resolution == 11) Measurement_Raw &= ~0x0006;
        if (SHT21[0].Resolution == 12) Measurement_Raw &= ~0x0005;
        if (SHT21[0].Resolution == 13) Measurement_Raw &= ~0x0004;
        if (SHT21[0].Resolution == 14) Measurement_Raw &= ~0x0003;

        // Define CRC Variables
        uint8_t CRC, Control_Byte;

        // Calculate CRC
        for (Control_Byte = 0; Control_Byte < 3; Control_Byte++) {
            CRC ^= SHT21_Data[Control_Byte];
            for (uint8_t bit = 8; bit > 0; bit--) {
                if (CRC & 0x80) {CRC = (CRC << 1) ^ 0x131;}
                else { CRC = (CRC << 1);}
            }
        }

        // Control and Calculate Measurement
        if (CRC == 0) {

            // Calculate Measurement
            Measurement_Array[Read_ID] = -46.85 + ((175.72 * float(Measurement_Raw)) / pow(2,16));

        } else {

            // Set Error Code
            Value_ = -104;

            // End Function
            return(false);

        }

        // Read Delay
        if (SHT21[0].Resolution == 14) delay(85);   // T: 14 bit
        if (SHT21[0].Resolution == 12) delay(22);   // T: 12 bit
        if (SHT21[0].Resolution == 13) delay(43);   // T: 13 bit
        if (SHT21[0].Resolution == 11) delay(11);   // T: 11 bit

    }

    // ************************************************************
    // Calculate Value for Average Type
    // ************************************************************

    // Control Read Count for Average Type
    if (Read_Count < 3 and AVG_Type == 3) AVG_Type = 2;

    // Calculate Average
    if (AVG_Type == 1) {

        // ************************************************************
        // Calculate Aritmetic Average With Measurement Array
        // ************************************************************

        // Calculate Average
        double Average_Value_Sum = 0, Average = 0;
        for (int i = 0; i < Read_Count; i++) Average_Value_Sum += Measurement_Array[i];
        Average = Average_Value_Sum / Read_Count;

        // Calculate Average
        Value_ = Average;

        // Set Deviation Variable
        Deviation_ = 0;

    }   // Aritmetic Average
    if (AVG_Type == 2) {

        // ************************************************************
        // Calculate RMS Average With Measurement Array
        // ************************************************************

        // Calculate Average
        double Average_Value_Sum = 0, Average = 0;
        for (int i = 0; i < Read_Count; i++) Average_Value_Sum += sq(Measurement_Array[i]);
        Average = sqrt(Average_Value_Sum / Read_Count);

        // Calculate Average
        Value_ = Average;

        // Set Deviation Variable
        Deviation_ = 0;

    }   // RMS Average
    if (AVG_Type == 3) {

        // ************************************************************
        // Calculate Max & Min values and Extract From Measurement
        // Calculate RMS Average With Filtered Measurement Array
        // ************************************************************


    }   // Filtered RMS Average
    if (AVG_Type == 4) {

        // ************************************************************
        // Calculate Median Point of Measurement Array
        // ************************************************************


    }   // Median Average
    if (AVG_Type == 5) {

        // ************************************************************
        // Calculate Average and Standart Deviation With Measurement Array
        // Select x Sigma Valid Data and Calculate Aritmetic Average
        // ************************************************************

        // Define Sigma
        float Sigma = 1;

        // Define Variables
        int Valid_Data_Count = 0;
        double Value_Sum = 0;

        // Calculate Average
        double Average_Value_Sum = 0, Average = 0;
        for (int i = 0; i < Read_Count; i++) Average_Value_Sum += Measurement_Array[i];
        Average = Average_Value_Sum / Read_Count;

        // Calculate Standart Deviation
        double Deviation_Value_Sum = 0, Deviation = 0;
        for (int i = 0; i < Read_Count; i++) Deviation_Value_Sum += ((Measurement_Array[i] - Average) * (Measurement_Array[i] - Average));
        Deviation = sqrt(Deviation_Value_Sum / Read_Count);

        // Set Sigma Min/Max Values
        float Sigma_1_Max = Average + (Sigma * Deviation);
        float Sigma_1_Min = Average - (Sigma * Deviation);

        // Handle Array Values
        for (int Calculation_ID = 0; Calculation_ID < Read_Count; Calculation_ID++) {

            // Control for 1 Sigma Data
            if (Measurement_Array[Calculation_ID] >= Sigma_1_Min and Measurement_Array[Calculation_ID] <= Sigma_1_Max) {

                // Calculate Sum
                Value_Sum += Measurement_Array[Calculation_ID];

                // Calculate Valid Data Count
                Valid_Data_Count++;

            }

        }

        // Control for Valid Data
        if (Valid_Data_Count < 1) {

            // Set Error Code
            Value_ = -105;

            // End Function
            return(false);

        }

        // Calculate Average
        Value_ = (Value_Sum / Valid_Data_Count);

        // Set Deviation Variable
        Deviation_ = Deviation;

    }   // 1 Sigma Average

    // ************************************************************
    // Control For Sensor Range
    // ************************************************************
    if (Value_ < SHT21[0].Range_Min or Value_ > SHT21[0].Range_Max) {

        // Set Error Code
        Value_ = -106;

        // End Function
        return(false);

    }

    // End Function
    return(true);

}