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