Hello,
I just started at NXP. I picked the LPCXpresso860-MAX as my Evaluation Board and I'm learning how to develop applications with it. Right now, I'm working on connecting the LPC860-MAX microcontroller to the SHT-31 humidity and temperature sensor using I2C.
I attempted as shown below.
Hardware Setup:
LPC860-MAX connections:
SCL (P0_10)
SDA (P0_11)
SHT-31 connections:
VCC → 3.3V
GND → Ground
SCL → LPC860-MAX SCL
SDA → LPC860-MAX SDA
Software Setup:
/*
* Program for SHT-31 (Temperature & Humidity sensor)
*/
#include <stdio.h>
#include "pin_mux.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "i2c.h"
#include "wait.h"
#define SHT31_ADDR 0x44 // SHT-31 I2C address
enum registers {
Temp,
Conf,
T_LOW,
T_HIGH
};
void mcu_init( void );
// Function to send command to SHT-31
void SHT31_SendCommand(uint8_t commandHigh, uint8_t commandLow) {
uint8_t data[2];
data[0] = commandHigh;
data[1] = commandLow;
// Send command to SHT-31 over I2C
i2c_reg_w16( SHT31_ADDR, Temp, data );
}
// Function to read data from SHT-31
void SHT31_ReadData(uint8_t *buffer, uint8_t length) {
// Read 6 bytes from SHT-31 (Temperature + Humidity)
i2c_reg_r16( SHT31_ADDR, buffer );
}
int main( void )
{
mcu_init();
i2c_init( 100 * 1000 ); // setting I2C clock frequency in "Hz"
uint8_t data[6];
int16_t rawTemperature, rawHumidity;
float temperature, humidity;
// Send command to read temperature and humidity
SHT31_SendCommand(0x2C, 0x06); // Command for temperature & humidity
PRINTF( "\r\nLPC860-MAX with SHT-31\r\n\r\n" );
while ( 1 )
{
// Read the response (6 bytes)
SHT31_ReadData(data, 6);
// Calculate temperature from raw data
rawTemperature = (data[0] << | data[1];
temperature = -45 + (175 * rawTemperature / 65535.0);
// Calculate humidity from raw data
rawHumidity = (data[3] << | data[4];
humidity = 100 * rawHumidity / 65535.0;
// Output the results (for example, via UART or LED)
printf("Temperature: %.2f °C\n", temperature);
printf("Humidity: %.2f Percentage\n", humidity);
wait_ms( 1000 );
}
return 0;
}
void mcu_init( void )
{
CLOCK_Select( BOARD_DEBUG_USART_CLK_ATTACH );
CLOCK_Select( kI2C0_Clk_From_MainClk );
BOARD_InitBootPins();
BOARD_BootClockFRO60M();
BOARD_InitDebugConsole();
if ( SysTick_Config( SystemCoreClock / 1000U ) )
{
PRINTF( "Fail: SysTick_Config()\r\n" );
while ( 1 );
}
}
status_t i2c_reg_w16( const uint8_t addr, const uint8_t reg, const uint16_t data )
{
uint8_t b[ 3 ] = { reg, (data >> 8), data & 0xFF };
return i2c_send( addr, b, sizeof( b ), I2C_STOP );
}
uint16_t i2c_reg_r16( const uint8_t addr, const uint8_t reg )
{
uint8_t b[ 2 ] = { reg, 0 };
i2c_send( addr, b, 1, I2C_REPEATED_START );
i2c_receive( addr, b, 2 );
return (b[ 0 ] << | b[ 1 ];
}
I received the following output:
Humidity: 0.20 Percentage
Temperature: 11.23 °C
Humidity: 0.20 Percentage
Temperature: 11.23 °C
Humidity: 0.20 Percentage
Temperature: 11.23 °C
Humidity: 0.20 Percentage
It looks like the value isn't being read correctly. Could you please help fix this by sharing a sample code that can be used for development?