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;
}
#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;
}