I2C arduino program code for HTS221 & LIS2DE12 sensor for communication.

Hi there,

I am pretty new to use I2C protocol. I want to read the data of HTS221 (temperature & Humidity sensor) and LIS2DE12 (Accelerometer) using my arduino AT Mega 2560 via I2C protocol.

Kindly share some ideas about how to write the code.

Thanks in advance.

--

Cyclops

HTS221 : http://www.st.com/web/en/catalog/sense_power/FM89/SC1718/PF260067
3.3V sensor, I2C/SPI, a number of control, status and temperature and humidity registers.

LIS2DE12 : http://www.st.com/web/catalog/sense_power/FM89/SC444/PF261135
3.3V sensor, I2C/SPI, many registers, with FIFO and interrupt.

They are both 3.3V sensors, but the Arduino Mega 2560 is the only Arduino board that has pullup resistors for the I2C bus and has therefor a 5V I2C bus. You may not connect the sensors to the Arduino Mega 2560. You need a level shifter between the Arduino Mega 2560 and the sensors.

I think it is easier to try an Arduino Uno to test the sensors. For a 5V Arduino board, be sure to add pullup resistors of 4k7 to 3.3V.
A 3.3V Arduino board is even better.
Start by writing function to read and write registes. Perhaps the HTS221 can be done. The LIS2DE12 could take months to develop good code.
There is code for the HTS221 on Github for the Arduino, but I can't find code for the LIS2DE12.

Thanks Koepel,

I figure out that later that Arduino Mega 2560 is bit challenging task. I would go with Arduino Uno. Thanks for your advice.

If possible can you provide me the link for the code of HTS221 so that I can get some Idea. I need to read the Temperature & Humidity values form the sensor and hoping that code will help me.

Thanks,

Cyclops

I did not check the source code of one of them. Perhaps you can look into a few of those, they are probably almost the same.

Thanks Koepel,

It helps me to understand the code logic flow and how it works.

What if I want to use any other microcontroller and want to read the values from the HTS221.

Should I want to read or write the individual register values of HTS221.


Cyclops

That is the most simple bottom layer of code. A few functions to read and write a byte or two bytes.

Could you please let me know which are the to main register that master needs to read the data from, to display Temperature and Humidity.

Sorry, it is not that easy. Perhaps the chips needs to be turned on, and there is a lot calculation and shifting involved.

Use a library or buy a sensor that comes with a library (for example Adafruit.com provides all information and write a library for the sensors they sell).

This library might work, although I spotted the wrong use of the Wire library : sme-hts221-library/HTS221.cpp at master · ameltech/sme-hts221-library · GitHub

I like this code more, it is more straightforware and uses the Wire library in the right way. Let's call this the 'bq' version : diwo/HTS221.cpp at master · bq/diwo · GitHub

This has more than one device in a single file, and the usage of the Wire library is not okay : M41T62_HTS221_MS5637_M24M02/M41T62_HTS221_BMP280_M24M02.ino at master · kriswiner/M41T62_HTS221_MS5637_M24M02 · GitHub

This is simple and seems okay : https://github.com/FaBoPlatform/FaBoHumidity-HTS221-Library/blob/master/src/fabo-hts221.cpp

Can you try the 'bq' version. You can use it as a library, or copy the code into a sketch. The libraries have example sketches, you need those examples as well.

Thanks again !!!

Could please explain me the following function in detail.

void readI2c (byte register_addr, int num, byte * buf)

{
Wire.beginTransmission (HTS221_SLAVE_ADDRESS);
Wire.write (register_addr);
Wire.endTransmission (false);

//Wire.beginTransmission(DEVICE_ADDR);
Wire.requestFrom (HTS221_SLAVE_ADDRESS, num);

int i = 0;
while (Wire. available ())
{
. buf = Wire read ();

  • i ++; *
  • }*

//Wire.endTransmission ();
}
readI2c (HTS221_WHO_AM_I, 1, & device);
if (device == HTS221_DEVICE)
{

  • return true;*
  • }*
    Else
    {
  • return false;*
  • }*
    Thanks in advance....

Please show your code between code tags. It is explained in number 7 on this page : http://forum.arduino.cc/index.php/topic,148850.0.html

Also add a link to the code that you found on the internet.
You probably found this: http://qiita.com/akira-sasaki/items/90aa0f74fc292621087a
Using code from the internet, is like eating a cookie you picked up from the street.

Every sensor has internal registers (not every sensor, perhaps 99%).
If the Arduino want to read a register, then it first writes the register-address. After the Arduino reads the data from that register.

Suppose register 5 needs to be read. The Arduino uses: "Wire.beginTransmission", "Wire.write(5)", Wire.endTransmission", only to write register-address '5' into the sensor.

After that the Arduino does a "Wire.requestFrom" to read the contents of register 5.
The "Wire.available" tells how many bytes are waiting in the buffer, and "Wire.read" reads data from the buffer.

The reference is online : Wire - Arduino Reference

Here is the code that I am stuck at:

bool SearchDevice ()
 {
   byte device = 0x00;
   readI2c (HTS221_WHO_AM_I, 1, & device);

   if (device == HTS221_DEVICE) {
       return true;
   } Else {
       return false;
   }
 }

If possible kindly explain me in detail how this thing works.
I get the code from the below link:

That is the same link as I posted.
The readI2c() is a function in that code. It reads a register (one or more bytes).

void readI2c(byte register_addr, int num, byte *buf)

The "HTS221_WHO_AM_I" is the "who am i" register inside the sensor. The value is probably declared in a *.h file.

If that registers has the value "HTS221_DEVICE" the function returns true, or else it returns false.