View previous topic :: View next topic |
Author |
Message |
sandyw
Joined: 30 Jun 2023 Posts: 7
|
SHT41 and I2C |
Posted: Thu Feb 13, 2025 5:15 am |
|
|
Hello,
Can anybody help with this.
I'm currently trying to get data from a Senserion SHT41 temp/humid sensor. The spec sheet says the is I2C compatible. When I use I2C to talk to the module all I keep getting back is 0xFF in all six packets.
I'm using the pic18F8722 development board with an in house build board with the sht41 fitted. When asking for any type of data I get 0xFF.
I then bought a Adafruit sht41 board with the sensor fitted and get the same result.
Compiler version is 5.118.
Code used is :
#include <main.h>
#define I2C_AADDRESS 0x44
#define SOFT_RESET 0x94
#define READ_SERIAL_NO 0x89
#define READ_TEMP_HUMID_LP 0xE0
void main()
{
int8 wData[1];
int8 rData[6];
int8 loop;
// use_i2c command should set this but do to make sure.
set_tris_c(0b00011000);
// turn of the spi to make sure not trying to do anything stupid
setup_spi(SPI_DISABLED)
setup_spi2(SPI_DISABLED);
#use i2c(Master,fast,i2c1,force_hw)
setup_adc_ports(NO_ANALOGS, VSS_VDD);
delay_ms(10);
while(TRUE)
{
// Give soft reset command
// Tell IC I want to read the temp
i2c_start();
i2c_write(I2C_AADDRESS);
i2c_write(SOFT_RESET);
i2c_stop();
//Allow 0.1sec plus margin
delay_ms(5);
// Tell IC I want to read the temp
i2c_start();
i2c_write(I2C_AADDRESS);
i2c_write(READ_SERIAL_NO);
i2c_stop();
//Allow 0.01sec plus margin
delay_ms(15);
// Now read the S/N
i2c_start();
i2c_write(I2C_AADDRESS | 0x01);
rData[0] = i2c_read();
rData[1] = i2c_read();
rData[2] = i2c_read();
rData[3] = I2c_read();
rData[4] = I2c_read();
rData[5] = i2c_read(0);
i2c_stop();
// Just in so that I can see that the code is actully runninig
output_toggle(pin_b5);
delay_ms(1000);
}
}
When looking on the scope all the pulses( start, write data , stop etc) all seem to be OK.
Any help much appreciated.
SandyW |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19729
|
|
Posted: Thu Feb 13, 2025 6:49 am |
|
|
It is _3.3v_ I2C. Your PIC is a 5v device.
Read the sticky at the top of the forum about 3.3v versus 5v.
You either need to switch to using a 3.3v PIC and 3.3v for everything
else, or have 3.3v to 5v level translation between the devices.
However big issue is its address is not 0x44. 0x44 is it's _7bit_ address.
To talk to this with a PIC you need to use 0x88/0x89 for read/write.
The 7bit address is shifted left by one bit before sending with the read/write
bit added afterwards. |
|
 |
sandyw
Joined: 30 Jun 2023 Posts: 7
|
|
Posted: Thu Feb 13, 2025 7:48 am |
|
|
Hi Ttelmah,
Thanks for the reply.
You are correct in saying that the data sheets say it is 3V3 but you can get 5V versions and the AdaFruit board is configurable as 3V3 or 5V and the ones I have are 5V but that's just for info for anybody else.
The problem was the wrong ID. It's obvious when you say it. I read that data sheet umpteen times from front to back and it just didn't click.
Thanks for the help, you have saved me a lot of time.
SandyW |
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1948 Location: Norman, OK
|
|
Posted: Thu Feb 13, 2025 9:23 am |
|
|
PCMs I2C Scanner in the Code Library would have helped you in this case.
When working with I2C I always use that first to confirm I can talk to the
devices (in case I have interface issues like pull-up resistors) and confirm the
device addresses. _________________ Google and Forum Search are some of your best tools!!!! |
|
 |
sandyw
Joined: 30 Jun 2023 Posts: 7
|
|
Posted: Thu Feb 13, 2025 9:27 am |
|
|
Hi Dyeatman,
Something handy to remember for next time.
Thanks
SandyW |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19729
|
|
Posted: Thu Feb 13, 2025 10:19 am |
|
|
There is a very big caveat about the 5/3.3v 'versions'.
Most devices like this being used on things like the AdaFruit, are actually
3.3v devices. What they do is have a 3.3v regulator on the board, have the
I2C pullups going to this, and run the chip at 3.3v.
Now this will run OK on the Adafruit, but not on the PIC.
Reason is the actual I2C hardware on the PIC requires it's inputs to
go up to 4v to see a .1., The Adafruit only requires 2.4v.
Now there are two solutions.
First you can select 'SMBUS' in the I2C setup. This turns down the
voltage required. Works on most PIC's.
Second use software I2C, and connect to pins that have TTL compatible
inputs. |
|
 |
sandyw
Joined: 30 Jun 2023 Posts: 7
|
|
Posted: Thu Feb 13, 2025 10:29 am |
|
|
Hi Ttelmah,
All you said about the Adafruit is right. They use a regulator and pull ups etc. through some fets as a level shifter.
There is a 5V version of the sht41 IC (or so my compatriot on site tells me) and that is what we are using with the PIC.
I will however go and check the info on the 5V version.
sandyw |
|
 |
|