I2C Bus Problem

I am using an arduino mega with a 20 x 4 LCD module which is hanging/freezing while running the lcd.init() or lcd.begin() function. The SDA and SCL are connected to pin 20 and 21 respectively.

For this reason I used a basic I2C address scanner code without connecting the LCD module which too hangs and cannot scan any addresses.

I also used a hd44780 diagnostic tool on which with/without any LCD connections gives the following result:

********************************************************************
Serial Initialized
--------------------------------------------------------------------
I2CexpDiag - i2c LCD i/o expander backpack diagnostic tool
--------------------------------------------------------------------
hd44780 lib version: 1.1.1
--------------------------------------------------------------------
Reported Arduino Revision: 1.8.12
CPU ARCH: AVR - F_CPU: 16000000
--------------------------------------------------------------------
SDA digital pin: 20
SCL digital pin: 21
--------------------------------------------------------------------
Checking for required external I2C pull-up on SDA - YES
Checking for required external I2C pull-up on SCL - STUCK LOW
********************************************************************
ERROR: SDA or SCL stuck pin
********************************************************************
I2C bus not usable

While this code runs successfully with another spare arduino mega which displays on LCD and reads out addresses.

Is there any problem with my previous mega? Please help!

Try the following sketch:

#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4);  //I2C addr, 20-chac, 4-line

void setup() 
{
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 1);  //DPos (0-19), LPos (0-3)
  lcd.print("Forum");   //Forum should appear on Bottom Line
}

void loop() 
{
 
}

If the above sketch is not working, then change the address from 0x3F to 0x27. If there is still no response, then check the continuity of the jumper wires and then replace the LCD if needed. The Library File is attached in case you don't have it.

LiquidCrystal_I2C-master.zip (8.98 KB)

So it is not about using another library. This works perfectly with another arduino mega.

The I2C bus is not able to get any addresses upon scanning even without any connections made to it.

I have used this code for scanning without connections. It hangs while scanning. And this gives output on another arduino mega.

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial)
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
 
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Serial.print("Testing Wire Transmission ");
    Serial.println(i);
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

Is your board damaged ? Then you should buy a new one.

You can do test with a Wire.begin() in setup() and keep the loop() empty. Let it run and measure the voltage of SDA and SCL. It should be 5V or close to 5V.

If one of them is stuck LOW and nothing is connected to it, then your board is damaged.

If you have a damaged board, then don't be tempted to use it with other pins. When a pin is damaged there might be other parts inside the microcontroller that are damaged as well.

Disconnect all wires from A4 and A5; upload the following sketch to get 8 on the Serial Monitor to be sure that I2C Logic/Interface of the Arduino is good.

void setup()
{
  Serial.begin(9600);
  TWCR = 0b10100100;  //TWI bus is formed,  START condition is asserted, TWINT-bit is cleared;
  while(bitRead(TWCR, 7) != HIGH) //check if process is done; HIGH at TWINT-bit
    ;     //wait until TWINT-bit becomes HIGH
  Serial.print((TWSR & 0b11111000), HEX);//Serial Monitor should show: 08 (8)
}

void loop() 
{
  
}

GolamMostafa:
Disconnect all wires from A4 and A5; upload the following sketch to get 8 on the Serial Monitor to be sure that I2C Logic/Interface of the Arduino is good.

void setup()

{
  Serial.begin(9600);
  TWCR = 0b10100100;  //TWI bus is formed,  START condition is asserted, TWINT-bit is cleared;
  while(bitRead(TWCR, 7) != HIGH) //check if process is done; HIGH at TWINT-bit
    ;    //wait until TWINT-bit becomes HIGH
  Serial.print((TWSR & 0b11111000), HEX);//Serial Monitor should show: 08 (8)
}

void loop()
{
 
}

No it does not print anything.

Koepel:
Is your board damaged ? Then you should buy a new one.

You can do test with a Wire.begin() in setup() and keep the loop() empty. Let it run and measure the voltage of SDA and SCL. It should be 5V or close to 5V.

If one of them is stuck LOW and nothing is connected to it, then your board is damaged.

If you have a damaged board, then don't be tempted to use it with other pins. When a pin is damaged there might be other parts inside the microcontroller that are damaged as well.

I did as you mentioned. I am getting SDA and SCL both close to 5V.

sanketpatel87:
No it does not print anything.

My one does print 8. So, try by changing the Arduino Board.