AtMega32u4 I2c not working properly

hi,first of all, i apologies if this question has been asked multiple times, but i cant seem to find the answer, or the answers that i found did not help solve my problem. So let me start from the beginning

So i built a stand alone arduno leonardo almost exactly the same as the one linked in bellow, with the exception that Uvcc is connected to VCC and i put a 1uF capacitor on reset and ground to prevent auto reset.

http://murchlabs.com/monday-experiment-bootloading-an-atmega32u4-with-arduino/

I can upload the basic blink code and it works fine, i can increase the pulse rates no problem. But it was when i tried interfacing with the I2C interfaces, problems started occurring. I used a DS3231MZ+ as an RTC, simple connection, its a two wire interface, SDA and SCL both with a 10k pull up resistor. On an arduino Uno, i could read the time no problem, but when i used the breakout board i created, upon opening up the serial monitor, i only get about 5 readings before it stops giving me anymore readings.

this is the code i used if anyon is wondering

/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
	// Start the I2C interface
	Wire.begin();
       // Clock.setSecond(50);//Set the second 
        Clock.setMinute(03);//Set the minute 
        Clock.setHour(14);  //Set the hour 
        //Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(2);  //Set the date of the month
        Clock.setMonth(7);  //Set the month of the year
        Clock.setYear(15);  //Set the year (Last two digits of the year)
	// Start the serial interface
	Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);
	// send what's going on to the serial monitor.
	// Start with the year
/*	Serial.print("2");
	if (Century) {			// Won't need this for 89 years.
		Serial.print("1");
	} else {
		Serial.print("0");
	}
	Serial.print(Clock.getYear(), DEC);
	Serial.print('-');
	// then the month
	Serial.print(Clock.getMonth(Century), DEC);
	Serial.print('-');
	// then the date
	Serial.print(Clock.getDate(), DEC);
	Serial.print(' ');*/
	// and the day of the week
	/*Serial.print(Clock.getDoW(), DEC);
	Serial.print(' ');*/
	// Finally the hour, minute, and second
	/*Serial.print(Clock.getHour(h12, PM), DEC);
	Serial.print(':');
	Serial.print(Clock.getMinute(), DEC);
	Serial.print(':');
	Serial.print(Clock.getSecond(), DEC);
	// Add AM/PM indicator
	if (h12) {
		if (PM) {
			Serial.print(" PM ");
		} else {
			Serial.print(" AM ");
		}
	} else {
		Serial.print(" 24h ");
	}
	// Display the temperature
	Serial.print("T=");
	Serial.print(Clock.getTemperature(), 2);
	// Tell whether the time is (likely to be) valid
	if (Clock.oscillatorCheck()) {
		Serial.print(" O+");
	} else {
		Serial.print(" O-");
	}*/
	// Indicate whether an alarm went off
	/*if (Clock.checkIfAlarm(1)) {
		Serial.print(" A1!");
	}
	if (Clock.checkIfAlarm(2)) {
		Serial.print(" A2!");
	}*/
	// New line on display
	//Serial.print('\n');
       // delay(1000);
	// Display Alarm 1 information
/*	Serial.print("Alarm 1: ");
	Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	Serial.print(ASecond, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm ');
		} else {
			Serial.print('am ');
		}
	}
	if (Clock.checkAlarmEnabled(1)) {
		Serial.print("enabled");
	}
	Serial.print('\n');
	// Display Alarm 2 information
	Serial.print("Alarm 2: ");
	Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);
	Serial.print(ADay, DEC);
	if (ADy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(AHour, DEC);
	Serial.print(' ');
	Serial.print(AMinute, DEC);
	Serial.print(' ');
	if (A12h) {
		if (Apm) {
			Serial.print('pm');
		} else {
			Serial.print('am');
		}
	}
	if (Clock.checkAlarmEnabled(2)) {
		Serial.print("enabled");
	}*/
	/* display alarm bits
	Serial.print('\n');
	Serial.print('Alarm bits: ');
	Serial.print(ABits, DEC);
	*/
/*
	Serial.print('\n');
	Serial.print('\n');
	delay(1000);

	// Display the time once more as a test of the getTime() function
	Clock.getTime(year, month, date, DoW, hour, minute, second);
	
        Serial.print(year, DEC);
        Serial.print("/");
	Serial.print(month, DEC);
        Serial.print("/");
	Serial.print(date, DEC);
        Serial.print("day of the week :");
	Serial.println(DoW, DEC);
	Serial.print(hour, DEC);
        Serial.print(":");
	Serial.print(minute, DEC);
        Serial.print(":");
	Serial.println(second, DEC);*/
}

some people who had similar problems said that there is a clash between USB and I2C and the way they solved it is by dropping the voltage to 3.3v. I have done that but the same problem still occurs.

IT couldn’t be the capacitor i have added, because even if i remove it, the problem persists.

I also tried using different I2C devices , i used an OLED display with TWI as well. and it does not display text perfectly. I have used the oled displays many times already, and i ensured the correct drivers and addresses has been used.

IF anyone has any suggestions or similar experiences, it would be of great help!

Thanks!

This is the pinmapping : Arduino - PinMapping32u4

A capacitor to prevent auto-reset ? I don’t understand. When does this auto-reset happen ?

I have not heard before of such problems with the ATmega32U4.
The ATmega32U4 has different voltage levels, but for the I2C the voltage levels should be the same as the ATmega328P. Perhaps there is a small difference, or perhaps a timing difference.

Do you use the newest Arduino IDE ?

Is the RTC powered with 5V ?

How long are the I2C wires ?
Do you use flat ribbon cable for the I2C (you should not do that).

What happens if you let the i2c_scanner run for a while ? Is it 100% okay ?
http://playground.arduino.cc/Main/I2cScanner

You could try without the pullup resistors or with 4k7 pullup resistors.

That link doesn't have the decoupling caps done right. Should be a 0.1uf ceramic cap, one on each vcc pin as close to the chip as possible, he's got 1uf electrolytic way in the corner. That could cause unreliable or unpredictable operation. That could be the problem?

Edit: Wait, is that 1uf cap for UCap pin? So he forgot the decoupling caps entirely, or thought that that replaced them...

hi, thanks for the replies,

1.maybe the term is not auto reset, but when uploading to the board, i have to pull it down to ground and then pull it tho high just before the code loads into the arduino. so reading

http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection

i know that it is for serial auto reset, but i realise i can upload a code without pulling it to down, and it works for all different codes, just not for i2c situations so i doubt that the problem is with the capacitor

  1. this guy had a similar problem,

http://forum.arduino.cc/index.php?topic=259094.0

but he said by using 3.3v he solved it, i tried it but it does not work.

  1. Yeap, im using the 1.6 IDe

  2. yes the RTC is powered by 5v, the DS3231 data sheet says it goes all the way to 5.5v but even when i ran it at 3.3v it did not work.

  3. the length of the wires are approximately 5cm both SDA and SCl. But, i have used the same wire on my UNO and it works fine.

6.i tried running the scanner, but not even the "scanning..." text came up

regarding the decoupling capacitors, i am using a 1uF electrolytic as i dont have a 0.1uF

BTW in the code bellow, when i dont uncomment the ReadDS3231(); i cant get the "test" in the serial monitor, but when i uncomment, i get a "test: every second. So what can you deduce from this?

void loop() {
ReadDS3231();
Serial.print("test");
delay(1000);

it is bugging me so much haha thanks in advance again!

Is there a way i can use another arduino to read my breakouts I2c? or if i were to use an oscilloscope, what should i be looking out for?

thanks

void setup()
{
 // Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}

when i uncomment this line, the seiral monitor gave me this output

Scanning...
I2C device found at address 0x01  !
I2C device found at address 0x03  !
I2C device found at address 0x05  !
I2C device found at address 0x07  !
I2C device found at address 0x09  !
I2C device found at address 0x0B  !
I2C device found at address 0x0D  !
I2C device found at address 0x0F  !
I2C device found at address 0x11  !
I2C device found at address 0x13  !
I2C device found at address 0x15  !
I2C device found at address 0x17  !
I2C device found at address 0x19  !
I2C device found at address 0x1B  !
I2C device found at address 0x1D  !
I2C device found at address 0x1F  !
I2C device found at address 0x21  !
I2C device found at address 0x23  !
I2C device found at address 0x25  !
I2C device found at address 0x27  !
I2C device found at address 0x29  !
I2C device found at address 0x2B  !
I2C device found at address 0x2D  !
I2C device found at address 0x2F  !
I2C device found at address 0x31  !
I2C device found at address 0x33  !
I2C device found at address 0x35  !
I2C device found at address 0x37  !
I2C device found at address 0x39  !
I2C device found at address 0x3B  !
I2C device found at address 0x3D  !
I2C device found at address 0x3F  !
I2C device found at address 0x41  !
I2C device found at address 0x43  !
I2C device found at address 0x45  !
I2C device found at address 0x47  !
I2C device found at address 0x49  !
I2C device found at address 0x4B  !
I2C device found at address 0x4D  !
I2C device found at address 0x4F  !
I2C device found at address 0x51  !
I2C device found at address 0x53  !
I2C device found at address 0x55  !
I2C device found at address 0x57  !
I2C device found at address 0x59  !
I2C device found at address 0x5B  !
I2C device found at address 0x5D  !
I2C device found at address 0x5F  !
I2C device found at address 0x61  !
I2C device found at address 0x63  !
I2C device found at address 0x65  !
I2C device found at address 0x67  !
I2C device found at address 0x69  !
I2C device found at address 0x6B  !
I2C device found at address 0x6D  !
I2C device found at address 0x6F  !
I2C device found at address 0x71  !
I2C device found at address 0x73  !
I2C device found at address 0x75  !
I2C device found at address 0x77  !
I2C device found at address 0x79  !
I2C device found at address 0x7B  !
I2C device found at address 0x7D  !
done

Note this is a slightly different I2c scanner

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
 // Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;
delay(1000);
  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

You need 0.1uf ceramic cap on each Vcc pin, as close to the chip as possible, to ensure reliable operation. Since you're seeing really spooky behavior, "well it kinda works" sort of thing, which is the sort of thing you see when you don't put in the decoupling caps, you should try putting appropriate decoupling caps in.

0.1uf ceramic caps are dirt cheap (search ebay - 1-5 cents/unit shipped, depending on quantity) and extremely abundant. I'm pretty sure they're the most frequently used capacitor value. They certainly are among hobbyists.

thanks, ill pop in the labs tomorrow to pick some up and i will report my findings here . Cheers!

hi, i havent changed the capacitor, but i hooked the sda and scl pins to the oscilloscope and i can see that SCL pulses when i initially plug the board in, but stops after a few seconds. does this mean anything?

thanks

:edit i found out that the RTC actually pulses just fine, it is just that the serial monitor cannot detect it.

You should never disable Wire.begin(). It must be called when you want to use the I2C bus. When you don’t see the text “Scanning…”, I think you should add the waiting for the serial port for the Leonardo.

  Serial.begin();
  while(!Serial);   // wait for Leonardo

  Serial.println("\nI2C Scanner");
  Serial.print("calling Wire.begin... ");
  Wire.begin();
  Serial.println("done");

OK, so i got the RTC to work reliably, but only when i connect the oscilloscope to SCL..the second i unplug it, it fails, whats going on??

Is there a way i can simmulate an oscilloscope probing? haha

There is something very wrong. A bad connecting, a missing ground wire, something like that.

There is something very wrong. A bad connecting, a missing ground wire, something like that.

i agree…i bet its something to do with capacitance. ill try out some decoupling in the morning

Are the grounds connected? They should be.

Are pullups in place on the bus?

Is it possible that one of the connections is loose and the act of connecting the probe physically moves it and causes it to make contact?

That you got it to work like that suggests that it's probably not a lack of decoupling caps (not that you don't need to add them - if not now, you'll have problems later) at fault here.

hi, So i have a question, whats the difference between a PC and a Mac?

Seriously, yesterday when i was writing this post, i used my windows machine and all the problem persist, ie only works with an oscilloscope.

So i went to the labs right, told my friends about the problem, and i plugged it into my mac, and BAM, it works fine, ( made a fool out of myself). works with or without probing the oscilloscope. So, i went home, plugged it into my windows again, and it doesnt work! exact same setup. also i have added decoupling capacitors. So there must be something odd going on here, the USB ports perhaps? i have a mac and a PC and i keep interchanging between these two and the same result occurs !

This stuff is spooky. Could this still be decoupling?

edit: Ok, this is strange, on my mac, when i use a different USB port, it behaves strange as well, it only works on one USB port on my mac. wraaaaa!! why do i do electronics!

Wait i have a question, in my breadboard, i braided the USB wires. It should reduce noise right? or doesnt it?

edit: just a quick update, i have decoupled the (excuse my language) C**p out of that board and still occurs..

Could you show us a photo of what you have ? I would like to that see that *** decoupling myself. At this moment your project is unstable, and the solution is not in sight.

Everything is pointing towards: bad decoupling, electric noise, long wires, bad grounding, bad contacts, mismatch of voltage levels, power trouble, and so on.

The best solution could be to stop this. Perhaps your electronic skills are not yet for a bare ATmega32U4 chip. Buy an Arduino Leonardo or Micro and use that.

hi,

i feel like such a noob as i dont know how to insert an image =( the insert image button is for URL. any ways, i just added 3 0.1uF on VCC to GND along the bread board , decoupling on every VCC,AVCC, UVCC.

when i figure out how to post a pic, i will post of the setup i have. I agree, that it could be all that you have mentioned and i am slowly trying to debug it.

And yes, i agree that the BEST solution is to not continue with this, but its more like i dont have a choice haha. it needs to be an integrated low profile PCB. Plus i have time, and yes, this is my first attempt at bare ATmega programming, so i still have lots to learn and this seems like a good opportunity.

I will continue debugging and report my findings here

cheers

Upload your image somewhere else (There are oodles of free image hosts) and use the image link, or attach the image to your post

This is pretty spooky

If you use the "REPLY" button instead of the "Quick Reply" text field, there are "Attachments and other options" below the text field on the left.