Maker Pro
Maker Pro

Temperature sensor over I2C not working using STM32nucleo64 microcontroller

lrclarke90

Mar 6, 2024
1
Joined
Mar 6, 2024
Messages
1
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.

#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 6
// Time to wait between measurements in milliseconds
#define WAIT_TIME_MS 2000
// Function prototypes
bool readSHTC3(float& temperature, float& humidity);
int main() {
// Initialize UART baud rate
pc.set_baud(9600);
// Initialize I2C communication
i2c.frequency(100000);
while (1) {
float temperature, humidity;
// Read temperature and humidity from SHTC3 sensor
if (readSHTC3(temperature, humidity)) {
// Print temperature and humidity to terminal
printf("Temperature: %.2f°C, Humidity: %.2f%%\n", temperature, humidity);
} else {
printf("Error reading SHTC3 sensor!\n");
}
// Wait for some time before next reading
thread_sleep_for(WAIT_TIME_MS);
}
}
bool readSHTC3(float& temperature, float& humidity) {
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 and humidity data
int rawTemp = (data[0] << 8) | data[1];
int rawHumidity = (data[3] << 8) | data[4];
// Convert raw temperature data to degrees Celsius
temperature = (175.0 * rawTemp / 65535.0) - 45.0;
// Convert raw humidity data to percentage
humidity = (100.0 * rawHumidity / 65535.0);
return true;
}
 

Maglatron

Jul 12, 2023
1,644
Joined
Jul 12, 2023
Messages
1,644
put it like this but in regards to your question I got no idea:
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 6
// Time to wait between measurements in milliseconds
#define WAIT_TIME_MS 2000
// Function prototypes
bool readSHTC3(float& temperature, float& humidity);
int main() {
// Initialize UART baud rate
pc.set_baud(9600);
// Initialize I2C communication
i2c.frequency(100000);
while (1) {
float temperature, humidity;
// Read temperature and humidity from SHTC3 sensor
if (readSHTC3(temperature, humidity)) {
// Print temperature and humidity to terminal
printf("Temperature: %.2f°C, Humidity: %.2f%%\n", temperature, humidity);
} else {
printf("Error reading SHTC3 sensor!\n");
}
// Wait for some time before next reading
thread_sleep_for(WAIT_TIME_MS);
}
}
bool readSHTC3(float& temperature, float& humidity) {
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 and humidity data
int rawTemp = (data[0] << 8) | data[1];
int rawHumidity = (data[3] << 8) | data[4];
// Convert raw temperature data to degrees Celsius
temperature = (175.0 * rawTemp / 65535.0) - 45.0;
// Convert raw humidity data to percentage
humidity = (100.0 * rawHumidity / 65535.0);
return true;
}
 

Maglatron

Jul 12, 2023
1,644
Joined
Jul 12, 2023
Messages
1,644
oh yeah and welcome to the forum Mr Irclarke90!!! I know it's cheating but if you use chatgpt (free to sign up) tell it what components you have and what microcontroller you using and the sensors and everything and ask it to write the code for you! it's very good! I use arduinos and it's very helpfull!
 
Last edited:

Delta Prime

Jul 29, 2020
2,125
Joined
Jul 29, 2020
Messages
2,125
I know it's cheating but if you use chatgpt (free to sign up) tell it what components you have and what microcontroller you using and the sensors and everything and ask it to write the code for you!
That's not cheating!
That's like saying manipulating an naturally occurring phenomenon, like electromagnetic fields is cheating; because we do not understand it fully.
Human being manipulate everything to their advantage we influence it's behavior it's a good feeling to help someone else... isn't it? Bravo!
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,769
Joined
Nov 17, 2011
Messages
13,769
There is a conceptual error in your code (call by value instead of call by reference) which prevents you from getting temperature and humidity, see my annotation in the code.

For debugging you may also want to add an errocode which can be printed to locate the source of the problem, see also my annotations.

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 6
// Time to wait between measurements in milliseconds
#define WAIT_TIME_MS 2000

// Function prototypes
bool readSHTC3(float& temperature, float& humidity);

// global variable to store an errorcode
#define MAXERRCHAR 30
char errorcode[MAXERRCHAR];

int main() {
// Initialize UART baud rate
    pc.set_baud(9600);
    // Initialize I2C communication
    i2c.frequency(100000);
    while (1) {
        /* this will not work. The variables are submitted as "call by value" (https://www.geeksforgeeks.org/c-function-argument-return-values/)
        Any change to the variables within the subroutine will not be seen outside.
        You need to use call by reference or use global variables (less recommended).
        */
        float temperature, humidity;
        // Read temperature and humidity from SHTC3 sensor
        if (readSHTC3(temperature, humidity)) {  // <- will not work, see above.
            // Print temperature and humidity to terminal
            printf("Temperature: %.2f°C, Humidity: %.2f%%\n", temperature, humidity);
        } else {
            printf("error accessing sensor. Errorcode = %03d\n", errorcode);
        }
        // Wait for some time before next reading
        thread_sleep_for(WAIT_TIME_MS);
    }
}

bool readSHTC3(float& temperature, float& humidity) {
    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) {
        strncpy(str_error, "Error writing to sensor", MAXERRCHAR);
        return false;
    }
    // Read measurement data from SHTC3 sensor
    if (i2c.read(SHTC3_ADDR | 0x01, data, SHTC3_MEASURE_SIZE) != 0) {
        strncpy(str_error, "Error reading from sensor", MAXERRCHAR);
        return false;
    }

    // Parse temperature and humidity data
    int rawTemp = (data[0] << 8) | data[1];
    int rawHumidity = (data[3] << 8) | data[4];
    // Convert raw temperature data to degrees Celsius
    temperature = (175.0 * rawTemp / 65535.0) - 45.0;
    // Convert raw humidity data to percentage
    humidity = (100.0 * rawHumidity / 65535.0);
    return true;
}
 
Top