Hello fellow Arduino enthusiasts, I am trying to make the ATTiny85 talk I²C with a MMA7660 breakout board I have (it's an accelerometer, this is a follow up question to my previous one).

I would state in advance that the I²C chip works perfectly with Arduino Nano, that I am using the Arduino IDE v1.5.6-r2, using the arduino-tiny core (from here) and that until now I had no special issues working at 8MHz.

The official documentation for the TinyWireM library (at the Arduino playground website) states that:

By default the I2C master library (TinyWireM) is set to run at 1MHz. To run at 8MHz, #defines in USI_TWI_Master.h / .cpp must be changed.

I have made the following changes in USI_TWI_Master.cpp:

#define F_CPU 8000000UL // was 1000000UL

I have made the following changes in USI_TWI_Master.h:

#define SYS_CLK   8000.0 // was 1000.0
#define T2_TWI    1.3 // was 5 
#define T4_TWI    0.6 // was 4 

My guess is that the T2_TWI & the T4_TWI values are wrong. I also tried T2_TWI 40 and T4_TWI 32 (as stated here), and T2_TWI 2 & T4_TWI 1 (as stated here) but no luck. I also tried some other values in between that I thought might make sense but it won't work.

The code I am running is the I²C scanner from here (which as I mentioned works perfectly with the Nano and finds the device), the blow is a modified version for interfacing the TinyWireM library instead of the Wire library:

#include <TinyWireM.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(-1,4);

void setup()
  mySerial.println("\nI2C Scanner");

void loop()
  byte error, address;
  int nDevices;
  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
    error = TinyWireM.endTransmission();

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

    else if (error==4) 
      mySerial.print("Unknow error at address 0x");
      if (address<16) 
  if (nDevices == 0)
    mySerial.println("No I2C devices found\n");

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

As a side note I would say that my logic analyzer was broken recently and I'm sure it would have helped here... Also I am aware of the sound-card hack to be used as logic analyzer, but would prefer to avoid adding more error-prone variables to the system, if possible, for now.

Here is a picture of probing with Saleae logic analyzer of the two channels. The result of the code above is "No I2C devices found":

Logic sampling of ATTiny I2C channels

(Full size picture)

Would appreciate the insights of anyone with experience using ATTiny85 at 8Mhz with I²C successfully.

  • 1
    Did you burn the fuses (upload bootloader) to enable the ATtiny to run at 8MHz? – imjosh Jun 11 '14 at 14:51
  • @imjosh: yes. thanks for bringing it up. I am always using the same ATTiny for prototyping and from time to time I burn the fuses just to be on the safe side :) – Omer Jun 11 '14 at 18:23
  • I took a look at USI_TWI_Master.h, and I don't think T2_TWI and T4_TWI should be changed at all. Just SYS_CLK from 1000.0 to 8000.0. Have you tried it like that? – imjosh Jun 11 '14 at 20:46
  • Yes. I also tried with the original values. no go. – Omer Jun 12 '14 at 11:14
  • Are you using an external crystal or resonator? – imjosh Jun 12 '14 at 14:35

You really just need to scope the signals, even if it's with a DSO Nano-type scope.

Without downloading the datasheets:

  • I would guess your pull-up is probably too small, but possibly too large (scope it)
  • Slew rate may be either insufficient or too steep (scope it)
  • Be aware that the ATTiny runs at "8MHz", and not 8.00MHz. The timing error can be off enough to cause communication issues at high speeds while working well at lower speeds (scope it, and/or start at default and incrementally increase speed by 0.5MHz until it stops working)
  • MMA may not support a typical protocol feature being used, i.e. 'repeated start', burst transfers, etc. (datasheet register/programming section)

Edit: Both the ATTiny and the MMA should have bypass capacitors near the power pins.

  • It's kind of easy to miss, but on my last comment to the question I mentioned that I did use logic analyzer and that the results are garbage. It happened with other I2C devices as well, not just MMA. Did you ever got the I2C working with the ATtiny85? – Omer Jan 28 '15 at 7:33
  • The Tiny family supports both "I2C" and "SPI", although they are not implemented as separate hardware peripherals. Instead, the Tiny has a generic hardware peripheral called the USI, that can emulate both. Of course, both protocols can always be bit-banged for virtually 100% guaranteed communication with anything you want. – Jon Jan 30 '15 at 12:10
  • If you feel like it, could you hook up any I2C device up to one and post a capture from your scope? – Jon Jan 30 '15 at 12:13
  • I discovered that I have an MMA8451Q on my KL25Z. I will try to run it with the Tiny tomorrow and post back the results. – Jon Jan 30 '15 at 12:39
  • The USI part from Atmel documentation is what giving me hope. Unfortunately I never heard of anyone succeeding with it. I'll update my question with a screenshot of the probing. – Omer Jan 30 '15 at 17:08

Just an idea. On adafruit.com there's a product called Trinket which is an ATtiny85 microcontroller board. It can communicate over I2C using their TinyWireM library. Maybe you should check it out for reference. I hope it helps :)

  • The owner of the question has already mentioned they are using the library. – Avamander Feb 12 '16 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.