How to run I2C Adxl345 and LCD05 together

Good day everyone!

I am a mechanical engineering student using the Arduino for my project (Very low skill in programming)

For my project, I am using Arduino Uno to run an analog thermocouple, accelerometer(I2C-ADXL345), humidity sensor(DHT22) and LCD (I2C-LCD05). I need help on setting up the I2C aspect. I have wired all the SCL/SCA wires together. But nothing seems to happen when i run the program, not even on the serial monitor.

Q1. The lazy method?
If i am not wrong, there is two I2C ports? Is it possible if i use and set 1 for acclerometer and 1 for the LCD??

Q2. The standard procedure?
When running individual components they seems fine, the problems seems to be the two I2C components? I believe i need to do something for them to link up?

Basically, I just need them to run all together so any other method is fine. Appreciate any help! Thank you!

Here is my codes:

/*-----( Import needed libraries )-----*/
#include <LCD03.h>
#include <Wire.h>
#include "DHT.h"
#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
#define THRESH_ACT  (0x24)
#define THRESH_INACT  (0x25)
#define DHTPIN 2
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
LCD03 lcd;    // Create new LCD03 instance

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];          //string buffer to transform data before sending it to the serial port


// Declare Variable
    float voltage; //Analog input voltage from AD8495 thermocouple
    float temp; //Temperature variable thermocoupple
    float tem[10]; //Temperature variable thermocoupple
    float x, y, z; //Acceleration variable
    float x1, y1, z1; //Acceleration variable
    int tpin = A0; //thermocouple to A0

    void thermocouple() {
          int voltage = analogRead(tpin); //Read voltage coming from AD8495
          float vout = (voltage * 5)/ 1023.0; //Calculate vout
          float temp = (vout - 1.25) / 0.005 ; //Convert to temperature
          Serial.print("Thermocouple : "); Serial.print("temp"); Serial.print((char)186); Serial.print(" C");
          lcd.print("Thermocouple: ");lcd.print(temp);lcd.print("C");  
        }

    void writeTo(int device, byte address, byte val) {
      Wire.beginTransmission(device); //start transmission to device
      Wire.write(address);        // send register address
      Wire.write(val);        // send value to write
      Wire.endTransmission(); //end transmission
    }
    
    void readFrom(int device, byte address, int num, byte buff[]) 
    {
      Wire.beginTransmission(device); //start transmission to device 
      Wire.write(address);        //sends address to read from
      Wire.endTransmission(); //end transmission
      
      Wire.beginTransmission(device); //start transmission to device (initiate again)
      Wire.requestFrom(device, num);    // request 6 bytes from device
      
      int i = 0;
      while(Wire.available())    //device may send less than requested (abnormal)
      { 
        buff[i] = Wire.read(); // receive a byte
        i++;
      }
      Wire.endTransmission(); //end transmission
    }
    
    void accelerometer()
    {
        int accelerometer = 0x32;    //first axis-acceleration-data register on the ADXL345
        readFrom(DEVICE, accelerometer, TO_READ, buff); //read the acceleration data from the ADXL345
        
         //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
         //thus we are converting both bytes in to one int
        x = (((int)buff[1]) << 8) | buff[0];   
        y = (((int)buff[3])<< 8) | buff[2];
        z = (((int)buff[5]) << 8) | buff[4];
        
        x1 = x/256;
        y1 = y/256;
        z1 = z/256;

  Serial.print("X: ");Serial.print(x1); Serial.println(" LSB/mg");
  Serial.print("Y: ");Serial.print(y1); Serial.println(" LSB/mg");
  Serial.print("Z: ");Serial.print(z1); Serial.println(" LSB/mg");
  lcd.print("VibrationZ:");lcd.print(x1);lcd.print("mm/s2");
     }
       void humidity() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // Read temperature as Celsius (the default)

  if (isnan(h) || isnan(t)) { // Check if any reads failed and exit early (to try again).
    lcd.println("Failed to read from DHT sensor!");
    return;
  }    
  float hic = dht.computeHeatIndex(t, h); // Compute heat index in Celsius
  lcd.print("Humidity: ");lcd.print(h);lcd.print("%    ");
  lcd.print("System_Temp:");lcd.print(t);lcd.print("C  ");
  //lcd.print("Heat index: ");lcd.print(hic);lcd.print("*C    "); // What is heat index
  
  Serial.print("Humidity: ");Serial.print(h);Serial.println(" %    ");
  Serial.print("Temperature: ");Serial.print(t);Serial.println(" ");
  //Serial.print("Heat index: ");Serial.print(hic);Serial.print("*C  ");
  
}
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  dht.begin();         //start humidity sensor
  lcd.begin(20, 4);    //start lcd
  lcd.backlight();     // Turn on the backlight
  Serial.println("The SenMon");
  delay(2000); // Wait a few seconds between measurements.
  writeTo(DEVICE, 0x2D, 0);   //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 16);  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 8);   //Turning on the ADXL345

}    
void loop()
{
  accelerometer();
  thermocouple();
  humidity();
  
 // Serial.write(10);
  delay(1000); //It appears that delay is needed in order not to clog the port
}

What about voltages of the I2C bus ?
Can you tell which Arduino board you use, which modules (with links to the products please) for the I2C bus.
If you have a 3.3V ADXL345 connected to a 5V Arduino board with a 5V LCD on the same I2C bus, that might not work. Do you use a level shifter ? Perhaps the level shifter is already on the ADXL345 module ?

If the voltages on the I2C bus are okay, then use the i2c_scanner : Arduino Playground - I2cScanner
It is possible to keep that sketch running and add and remove devices from the I2C bus.

This is a large buffer : char str [ 512 ] ;
Do you really need 512 bytes ?

The way you use the Wire library is not okay. The Wire.requestFrom() should not be encapsulate by Wire.beginTransmission() and Wire.endTransmission(). Remove those please, and show us your new sketch.

To show a sketch, use code tags. It is number 7 on this page : http://forum.arduino.cc/index.php/topic,148850.0.html

Hi,

I am using the Arduino Uno R3, ADXL345, LCD.

If you have a 3.3V ADXL345 connected to a 5V Arduino board with a 5V LCD on the same I2C bus, that might not work. Do you use a level shifter ? Perhaps the level shifter is already on the ADXL345 module ?

Yes, according to the data sheet, there is a level shifter inside ADXL345.

*Attached is the I2C scanner result, when I connect the ADXL345 via the 3.3V port, LCD via the 5V port.

This is a large buffer : char str [ 512 ] ;
Do you really need 512 bytes ?

The way you use the Wire library is not okay. The Wire.requestFrom() should not be encapsulate by Wire.beginTransmission() and Wire.endTransmission(). Remove those please, and show us your new sketch.

To be honest, I got the ADXL345 code online. I did try to decipher but I am quite confused at the wire commands. And also, everything seems to working as it should, So i did not went go to edit the codes.
I will read up the wire command again, and hopefully, I will be able to show you the new sketch.

** Update **
I just tried powering the ADXL345 through 3.3V port and the LCD through 5V port and everything is working now, even though I did not change to the coding.

Thanks for all the information :slight_smile:
Are those prices in singapore dollars ?
1 american dollar = 1.43 singapore dollars ?
Then those prices are still not cheap.
Adafruit uses good components, and they have normal prices. On Ebay you find the cheapest prices. For example a ADXL345 module for 1.30 (american) dollars.

The ADXL345 is a 3.3V sensor and may only connect to a 3.3V I2C bus.
This is the manufacturer's page : http://www.analog.com/en/products/mems/mems-accelerometers/adxl345.html
It does not allow 5V on any of its pins. In the datasheet they keep repeating that the voltage on any pin may not exceed VDD-IO + 0.3V.

The Uno is a 5V Arduino board, but in some cases it is possible to connect a 3.3V I2C chip to it, when 4k7 pullup resistors are used to 3.3V. A combination of a 5V I2C chip and a 3.3V I2C is still dangerous.

The LCD is a 5V module.

Do you have pullup resistors for the I2C bus ?

I know it will work, but the code is wrong and the I2C bus can't mix those voltage levels. The code might get you into trouble, and the wrong voltage levels could damage the ADXL345.

This is the Adafruit ADXL345 module, which is made compatible with 3.3V and 5V : ADXL345 - Triple-Axis Accelerometer (+-2g/4g/8g/16g) w/ I2C/SPI [STEMMA QT / Qwiic] : ID 1231 : $17.50 : Adafruit Industries, Unique & fun DIY electronics and kits

I suggest to buy a I2C level shifter and split the I2C bus into a 5V part and a 3.3V part. The level shifter also contains pullup resistors, so you don't have to add your own.

Don't just take some code from the internet. Also write in the sketch where you found that code. There is a lot bad code out there, especially on www.instructables.com

Did you know that ADXL345 code is in the Library Manager in the Arduino IDE ?
Go to: Sketch / Inlcude Library / Manage Libraries...
Type in the search entry: adxl
Install the ADXL345 Unified.
Also install the: "Adafruit Unified Sensor".
After that, examples have been added to the menu of the Arduino IDE.