Problem with software i2c for SHT21 sensor

Hello. I run this code for reading temperature and humidity using SHT21 sensor:

#include <Wire.h>
#include <Ports.h>
#include “PortsSHT21.h”
#include <SHT2x.h>

//Define soft i2c channels for a sensor
SHT21 hsensor2 (2); // pins A1 and D5 - Sensor 2

//define variables for temp data
float h, t;

void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{
// Get data from sensor soft I2C
hsensor2.measure(SHT21::HUMI);
hsensor2.measure(SHT21::TEMP);
hsensor2.calculate(h, t);
float hum2 = (h);
float temp2 = (t);
Serial.println(“temp2”);
Serial.println(temp2);

delay(500);
}

But always it returns -46.85 degree. here is PortsSHT21.cpp code and source code of PortsSHT21.h can be found here

It seems that this part of PortsSHT21.cpp file (line 114) has always a zero value for “meas[TEMP]” variable.

t_C = (175.72 / 16384.0) * meas[TEMP] - 46.85; //T= -46.85 + 175.72 * ST/2^16

Would you please help me about fixing this?

excuse me, I should run this code and have a short deadline for it. Would you please help?

excuse me, I should run this code and have a short deadline for it. Would you please help?

The deadline is your problem. If you want help please follow the forum rules. Edit your first post and put code tags around the code!

Why don't you use the I2C interface but some software emulation? You still include the Wire library although you don't use it.

Please post a wiring diagram and a link to the sensor board you're using!

hello, thanks for your answer, I tried to do that, but I couldnt write the code in its format.

I should communicate with more than one sht21 sensor, and since it gives a unique address (even if more than one sht21 sensor are connected to a single two wire i2c bus) i should use software i2c approachs.

I think that portssht21.cpp is defined in a wrong manner, but I have no knowledge of C++ programming (and a little in c programming) Would you please help me?

wiring diagram (as a documentation suggests):

Arduino UNo SHT21 A1 pin -----------------> SCL D5 pin -----------------> SDA 5V pin -----------------> Vin GND pin----------------> GND

This sensor has only one I2C address.

Use an I2C muxer if you want to use more sensors.

https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview Leo..

Wawa: This sensor has only one I2C address.

Use an I2C muxer if you want to use more sensors.

https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview Leo..

Thanks for your guidance. but it seems that some people has done this without any external hardware, using (as I mentioned in first post) Jeelib library. But it always gives me -46.85 degree for temperature.

Would you please take a look at the submitted code in first post and source code of portssht21.cpp & portssht21.h code and guide me?

I suppose you have tried one sensor the normal way, and that works.

Let's wait for an answer from the other forum, before we put any effort into it.

http://stackoverflow.com/questions/43117205/wrong-tempreture-value-when-using-arduino-software-i2c-for-sht21-sensor

Arduino UNo SHT21 A1 pin -----------------> SCL D5 pin -----------------> SDA 5V pin -----------------> Vin GND pin----------------> GND

The SHT21 sensor is a 3V3 device. To use it on an UNO you need a bidirectional level converter. As you connected it to 5V you probably need to buy a new sensor. If you don't connect the sensor directly, then post the link to the board you're using as I requested earlier!

It's probably not a problem of the code although the code is quite an overkill for the task (it contains a lot of code you don't need and the pretty nontransparent usage of the digital and analog pins to form so called ports is at least unusual. But it seems to do what it claims to, at least I found no obvious bug.

Please explain, why an I2C muxer is not feasible for you. The software solution will fail if the chip uses clock stretching while the hardware acts correctly.

If you don't need more than 4 sensors take a look at the SHT31 alternative. It runs on 5V and has two address lines to select the I2C address (and it's more precise but slightly more expensive). That way you can eliminate the level converter and you don't need the clumsy software emulation or the bus muxer.

pylon: The SHT21 sensor is a 3V3 device. To use it on an UNO you need a bidirectional level converter. As you connected it to 5V you probably need to buy a new sensor. If you don't connect the sensor directly, then post the link to the board you're using as I requested earlier!

The SHT21 module has a 3.3V regulator, so you can connect 5V to its Vin pin.

Thanks for your other comments.

The SHT21 module has a 3.3V regulator, so you can connect 5V to its Vin pin.

You told us that you use the SHT21 sensor. If you use a breakout board you should provide a link to that product or at least a link to the schematics!

If you module doesn't include a level converter you still need one even if you can power it with 5V as the signal lines (SDA and SCL) are not 5V save.