Temperature sensor over I2C not working using STM32nucleo64 microcontroller

Thread Starter

lrclarke90

Joined Mar 6, 2024
5
I have been trying to program the SparkFun SHTC3 Humidity sensor to an STM32nucleo64 L476RG board using Mbed studio but I cannot get any readings. I am trying to communicate through I2C but have not had any luck. I wondered if anyone out there can offer any advice? Or give advice on my code please.

C:
#include "mbed.h"

// I2C pins
#define SDA_PIN PB_9
#define SCL_PIN PB_8

// UART pins
#define TX_PIN PA_2
#define RX_PIN PA_3

// Define BufferedSerial object
BufferedSerial pc(TX_PIN, RX_PIN);

// Define I2C object
I2C i2c(SDA_PIN, SCL_PIN);

// SHTC3 sensor I2C address
#define SHTC3_ADDR 0x70

// Register addresses
#define SHTC3_MEASURE_CMD 0x7CA2
#define SHTC3_MEASURE_SIZE 3

// Time to wait between measurements in milliseconds
#define WAIT_TIME_MS 2000

// Function prototypes
bool readTemperature(float& temperature);

int main() {
    // Initialize UART baud rate
    pc.set_baud(9600);

    // Initialize I2C communication
    i2c.frequency(100000);

    while (1) {
        float temperature;

        // Read temperature from SHTC3 sensor
        if (readTemperature(temperature)) {
            // Print temperature to terminal
            printf("Temperature: %.2f°C\n", temperature);
        } else {
            printf("Error reading temperature from SHTC3 sensor!\n");
        }

        // Wait for some time before next reading
        thread_sleep_for(WAIT_TIME_MS);
    }
}

bool readTemperature(float& temperature) {
    char data[SHTC3_MEASURE_SIZE];

    // Send measurement command to SHTC3 sensor
    char cmd[2] = {SHTC3_MEASURE_CMD >> 8, SHTC3_MEASURE_CMD & 0xFF};
    if (i2c.write(SHTC3_ADDR, cmd, 2, true) != 0) {
        return false;
    }

    // Read measurement data from SHTC3 sensor
    if (i2c.read(SHTC3_ADDR | 0x01, data, SHTC3_MEASURE_SIZE) != 0) {
        return false;
    }

    // Parse temperature data
    int rawTemp = (data[0] << 8) | data[1];

    // Convert raw temperature data to degrees Celsius
    temperature = (175.0 * rawTemp / 65535.0) - 45.0;

    return true;
}
C:
 

MrChips

Joined Oct 2, 2009
34,627
Welcome to AAC!

You need to break the problem down into smaller steps. For example, you need to verify that you have I2C communications.
Do you have access to an oscilloscope?
 

schmitt trigger

Joined Jul 12, 2010
2,027
One of the most common “gotchas” for beginners dealing with I2C is confusing the 7 bit address with the 8 bit address.
Other than that, is checking with an oscilloscope that the pulses actually appear. A lack of pull-up resistors will lead to an unresponsive bus.
 

Thread Starter

lrclarke90

Joined Mar 6, 2024
5
Welcome to AAC!

You need to break the problem down into smaller steps. For example, you need to verify that you have I2C communications.
Do you have access to an oscilloscope?
I do yes, I will check the signals on the SDA and SCL lines and let you know what I see
 

schmitt trigger

Joined Jul 12, 2010
2,027
Lastly, I ignore the depth of your involvement with digital electronics in general and serial busses in particular.

If you plan to grow down this path, I would advise that you invest in an USB logic analyzer. There are some basic yet quite capable analyzers for less than US$50.
 

MrChips

Joined Oct 2, 2009
34,627

Thread Starter

lrclarke90

Joined Mar 6, 2024
5
Ok, now you need to break in down to basics. You need to pare down your code to the bare minimum.
Create a single I2C transaction with the device.
Get the SHTC3 datasheet and confirm the SDA and SCL signals are correct.
Do you receive an ACK signal from the device?

https://sensirion.com/media/documents/643F9C8E/63A5A436/Datasheet_SHTC3.pdf
I have sent a read command to the device using the register address 0xEFC8 as per the data sheet, using the following code, presenting the oscilloscope results. The results from the SDA and SDL display no change.

I'm not sure whether I need to create an initialisation process. According to the datasheet on page 7, it would seem there is a process to take in order to read the signals. I just find it odd how the SDA and SCL lines are remaining high and not changing at all.

What would you recommend? or anyone else reading this please feel free to advise.

C:
#include "mbed.h"

// I2C pins
#define SDA_PIN PB_9
#define SCL_PIN PB_8

// Define I2C object
I2C i2c(SDA_PIN, SCL_PIN);

// SHTC3 sensor I2C address
#define SHTC3_ADDR 0x70

// Register address for reading sensor ID
#define SHTC3_SENSOR_ID_REG 0xEFC8

int main() {
    // Initialize I2C communication
    i2c.frequency(100000);  // Set I2C frequency to 100 kHz

    // Buffer for storing sensor ID
    char sensorID[2];

    // Perform a single I2C read transaction to read sensor ID
    int result = i2c.read(SHTC3_ADDR, sensorID, sizeof(sensorID), false);

    if (result == 0) {
        // Communication successful
        printf("Sensor ID: 0x%02X%02X\n", sensorID[0], sensorID[1]);
    } else {
        // Communication failed
        printf("Error reading sensor ID!\n");
    }

    // End of test
    return 0;
}
Screenshot 2024-03-06 210759.png

Screenshot 2024-03-06 205505.png
Screenshot 2024-03-06 210051.png
 
Last edited:

ronsimpson

Joined Oct 7, 2019
4,645
The O-scope, looks like you are not outputting anything. I think you have the I/O setup wrong. It looks like the pins are in input mode and never get set to output.
 

MrChips

Joined Oct 2, 2009
34,627
Strip down your test code. Remove any and everything that uses printf( ).
Here is a sample oscilloscope output for a different device.
Your device address is 0x70.

1709770450706.png

Notice that SDA is HIGH and then goes LOW before SCL starts clocking.
You are looking to see ACK = LOW after the address 0x70 is received.
 

geekoftheweek

Joined Oct 6, 2013
1,429
I did some poking around, but couldn't come up with any example code to compare with. Generally with any I2C you have to define your micro as a master or slave.

Do you have any example code from the mbed studio we can use to compare? Usually there is a folder with examples with pretty much any IDE for anything.
 
Top