Using I2C on ATMEGA328P-PU not working

Hi all,

I got the ATMEGA328P-PU chip and bootloaded it. I used this package to burn the bootloader:

The bootloader seems to work as I was able to write different sketches and upload them to the chip (like blinking LED, etc). However, when I tried to use the I2C with a component (Real Time Clock or LCD backpack), it would not work. It would seemingly give garbage results. Both the Real Time Clock or the LCD backpack would not work, however they would work with my Arduino Leonardo.

I have two 10k Ohm pull up resistors at the 5V for both SDA and SCL. I’ve tested this issue with two different ATMEGA chips I got from Digikey (both of which I bootloaded as explained above). I am at a lost why I can’t get the I2C to work. Any suggestions are welcomed and appreciated.

Thanks,
MathPi

Edit 1:

I’ve uploaded the schematic. Some more details:

  • Blue wires are the SCL and SDA
  • Crystal clock is 16 MHz with two 22 pF caps to ground
  • Real Time Clock is “RTC DS1307” … not exact as shown in schematic. However, as stated, I’ve tried this with other I2C components with similar bad results.
  • FTDI breakout is “FT232R Breakout” … again it is not exactly as shown in schematic. The one I have is not available in Fritzing.
  • The capacitor to reset is 0.1 uF. I actually had lots of trouble with this. Without this cap, I couldn’t upload sketches. But it was in this forum that I found the answer.

Lastly - I’ve tried this with and without the 10k Ohm pull up resistors. Similar results either way.

(deleted)

Clearly you have been able to upload sketches so it is probably not serious a timing issue. Are you using an external 16Mhz crystal with 2 x 22pf capacitors ?

spycatcher2k:
What happens if you remove the 10K pull ups?
Can you give a wiring schematic, or a FULL description of your wiring!

See my reply in Edit of original post. Cheers!

(deleted)

spycatcher2k:
DON'T DO THAT! Post new info inline.

I am not sure how to upload a picture when doing the "Quick Reply" - so I just edited my original post.

Here is my reply (but please see the image I attached above):

I've uploaded the schematic. Some more details:

  • Blue wires are the SCL and SDA
  • Crystal clock is 16 MHz with two 22 pF caps to ground
  • Real Time Clock is "RTC DS1307" .. not exact as shown in schematic. However, as stated, I've tried this with other I2C components with similar bad results.
  • FTDI breakout is "FT232R Breakout" ... again it is not exactly as shown in schematic. The one I have is not available in Fritzing.
  • The capacitor to reset is 0.1 uF. I actually had lots of trouble with this. Without this cap, I couldn't upload sketches. But it was in this forum that I found the answer.

Lastly - I've tried this with and without the 10k Ohm pull up resistors. Similar results either way.

Please remove the wire to AREF.
Also add a 100nF (or a few) decoupling capacitors between VCC and GND.

Can you give more information ? You can't get I2C to work, but what does not work ? Did you try a I2C scanner ?

If you say it does not work, or has bad results or gives garbadge, then we don't know what to do with it. You have to investigate it and tell us more.

Hi,

I've removed the ARef and also added some 100nF caps as you suggested. It appears that there is not much difference.

I did run I2C scanner and am getting the following:

I2C Scanner
Scanning...
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at addt address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device foun⸮
I2C Scanner
Scanning...
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !
I2C device found at address 0xE8 !
I2C device found at address 0x68 !

I should be more particular with my results. When I tried to print out anything from the RTC (say time or date), I get stuff like "⸮譭⸮⸮⸮⸮b⸮0b⸮r,".

That's nasty. It seems that it is barely running.
Is there something at 0xE8 or is that the wrong address ?
Perhaps the voltage is too low. Is it above 4.5 V ? Or the breadboard has bad contacts. Or you need more decoupling capacitors. Or there is a ground wire missing somewhere. Or a shortcut with RX and TX.
Start with the serial communication. Remove everything else. Make a sketch that outputs some text in the loop and add a delay() and see if it can output a nice stream of text.

void loop()
{
  Serial.println( "Hello World");
  delay( 500);
}

Hi,

I've done the following:

  • Checked the voltage with my multimeter - it is indeed 5.12 VDC (Just to be save - I checked the battery voltage for the RTC and it is ok as well).
  • Removed everything and made a sketch to print "Hello World". It prints as expected. No issue there.
  • I've looked to see if all grounds are correct - it appears so.
  • RX and TX don't appear to be shortcut anywhere. .... if this were the case, then I would not be able to run the simple LED blinking sketch. But I can run that sketch just fine.
  • I've checked the contacts and they seem fine too.

There is no other device plugged in that uses I2C communication - So I am not sure what 0XE8 is...?

It is working now. ... I took everything out of the breadboard and replaced it (on a different breadboard). Now I2C, along with everything else, is working as expected. Therefore, it was likely a bad connection somewhere, maybe to ground.

Thanks everyone for your input. Debugging these cases can be quite time consuming.

Cheers,
MathPi

I'm glad it is working :smiley: It took some time to get to know your project, but when you showed the Serial output, that was an indication that is was barely running.