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.
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.
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.
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).
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.
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.