6 years, 7 months ago.

Connectig NUCLEO-L432KC to SHT25 using I2C

Hi im new using mbed, Recentrly I bought a L432KC and start rightaway experimenting with it, but now Im having trouble trying to get conected to a SHT25, I'm already using the pullup resistors, I'm using the Nucleo_i2c_master by arostm, and adaped it to my needs, I'm not sure if something is missing but from what I read from de Datasheet, SHT25 doesnt have a start register, so I'm using only the address (0x40) and the temp register(0xE3), here is the code:

include the mbed library with this snippet

#include "mbed.h"
 
#define LM75_REG_TEMP (0xE3) // Temperature Register
//#define LM75_REG_CONF (0x01) // Configuration Register
#define LM75_ADDR     (0x40) // LM75 address
 
I2C i2c(PB_7, PB_6);
 
DigitalOut myled(LED2);
 
Serial pc(USBTX,USBRX);
 
volatile char TempCelsiusDisplay[] = "+abc.d C";
 
int main()
{
 
    //i2c.start();
    i2c.frequency(125000);
    char data_write[] = {0};
    char data_read[2];
 
    /* Configure the Temperature sensor device STLM75:
    - Thermostat mode Interrupt
    - Fault tolerance: 0
    */
    data_write[0] = LM75_REG_TEMP;
    int status = i2c.write(LM75_ADDR, data_write, 1, 0);
    //pc.printf("status: %d\n",status);
    if (status != 0) { // Error
        while (1) {
            myled = !myled;
            wait(1);
            pc.printf("ERROR!\n\n");    
        }
    }
 
    while (1) {
        // Read temperature register
        data_write[0] = LM75_REG_TEMP;
        i2c.write(LM75_ADDR, data_write, 1, 1); // no stop
        i2c.read(LM75_ADDR, data_read, 2, 0);
 
        // Calculate temperature value in Celcius.
        int tempval = (int)((int)data_read[0] << 8) | data_read[1];
        tempval >>= 7;
        if (tempval <= 256) {
            TempCelsiusDisplay[0] = '+';
        } else {
            TempCelsiusDisplay[0] = '-';
            tempval = 512 - tempval;
        }
 
        // Decimal part (0.5°C precision)
        if (tempval & 0x01) {
            TempCelsiusDisplay[5] = 0x05 + 0x30;
        } else {
            TempCelsiusDisplay[5] = 0x00 + 0x30;
        }
 
        // Integer part
        tempval >>= 1;
        TempCelsiusDisplay[1] = (tempval / 100) + 0x30;
        TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30;
        TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30;
 
        // Display result
        pc.printf("temp = %s\n", TempCelsiusDisplay);
        myled = !myled;
        i2c.stop();
        wait(1.0);
    }
 
}
 

I know Im not using the proper formula for obtaining the temperature, but doesnt matter because it never gets passed the if status condition, it always returns 1, I havent guessed what's wrong.

Thanks for the help!

1 Answer

6 years, 7 months ago.

Hello Agustín,
Because SHT25 is almost identical to SHT21, to get started I'd suggest to import the sht_21_test program (e.g. by clicking on the [Import program] button). Then modify line #5 as follows:

SHT2x sht21(PB_7, PB_6);

If that program does not work because of unsupported I2C byte write and read operations then try the following:

  • Create a new project and edit main.cpp as follows:

#include "mbed.h"
#include "SHT2X.h"
 
Serial pc(USBTX, USBRX);
SHT2X  sht25(PB_7, PB_6);  // I2C SDA pin name, I2C SCL pin name
float  temperature;
float  humidity;
 
int main()
{          
    while(1) {
        sht25.readData(&temperature, &humidity);
        pc.printf("temperature = %5.2f%cC\r\n", temperature, 176);
        pc.printf("humidity    = %5.2f%%RH\r\n\r\n", humidity);
        wait(1);
    }   
}
  • Save main.cpp and import the SHT2X library into your project (by clicking on the [Import into Compiler] button located in the top-right corner of the library's home page).
  • In the SHT2X folder of your project open the SHT2X.h file and modify the declaration of SHT2X class as follows:

class SHT2X {
    public:
        //SHT2X(I2C& i2c);
        SHT2X(PinName sda, PinName scl);  // new line
        int readData(float *tempC, float *relHumidty);
        int measureTemperature(float *tempC);
        int measureHumidty(float *relHumidty);
        int setPrecision(char precision);
        bool softRest();
    protected:
        I2C m_i2c;
        //Serial pc;
    private:
    
};
  • In the SHT2X folder of your project open the SHT2X.cpp file and modify the definition of the SHT2X constructor as follows:

//SHT2X::SHT2X(I2C& i2c) : m_i2c(i2c), pc(p5,p4) {
//}

SHT2X::SHT2X(PinName sda, PinName scl) : m_i2c(sda, scl) {}

Then compile.

Zoltan

Accepted Answer

Thank's It worked with the second method, the first one returned me an error of a library called cmsis.h "CMSIS Target not recognised", but on the second method of creating a new project and import the sht2x.h lib and changed a few things, worked like a charm.

posted by Agustín Castañeda 28 Aug 2017