5

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()
{
  TinyWireM.begin();
  mySerial.begin(9600);
  mySerial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;
  mySerial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    TinyWireM.beginTransmission(address);
    error = TinyWireM.endTransmission();

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

      nDevices++;
    }
    else if (error==4) 
    {
      mySerial.print("Unknow error at address 0x");
      if (address<16) 
        mySerial.print("0");
      mySerial.println(address,HEX);
    }  
  }
  if (nDevices == 0)
    mySerial.println("No I2C devices found\n");
  else
    mySerial.println("done\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.

Omer
  • 1,370
  • 1
  • 9
  • 15
  • 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
  • I am using the internal resonator. – Omer Jun 12 '14 at 14:46
  • Try using an external crystal or calibrating the internal resonator maybe - https://www.google.com/search?q=calibrate+attiny+internal+resonator – imjosh Jun 12 '14 at 15:32
  • thanks, but currently I'm looking for a solution that does not require external crystal. Also I could not find any reference on how to calibrate the internal oscillator (and not resonator, according to the spec). In addition I find it hard to believe that SoftwareSerial timing is right if the internal oscillator is not accurate enough. – Omer Jun 12 '14 at 17:03
  • Did you manage to successfully run I²C on ATTiny85 without external oscillator? – Omer Jun 12 '14 at 17:04
  • You have the Pull up resisters in place... both of them? – Chip Estrada Sep 24 '14 at 19:47
  • Yes. Both of them. – Omer Sep 24 '14 at 20:20
  • Any luck with this issue? I am probably experiencing very similar. My logic analyser says the first byte sent (the slave address) is correct, the rest is nonsense. I tried it on 1Mhz and 8Mhz, correct fuses, fiddling with the T2_TWI/T4_TWI but I am running out of ideas. – oujesky Dec 26 '14 at 22:40
  • Unfortunately no. I also checked with logic analyzer and oscilloscope- and I can confirm output is garbage. – Omer Dec 26 '14 at 23:08
  • @Omer Looking at this picture from your post, are'nt SDA and SCL signals reversed? SDA looks like clock and SCL looks like data. – Chetan Bhargava Feb 23 '16 at 16:19
  • I have the same problem as you with a DHTC3 probe. The I2scanner code does not respond OK to the probe address with an Attiny85 while it works with an Arduino Uno. However, I changed the frequency to 8 Mhz and the 2 timers T2_TWI and T4_TWI. And you, have you solved your problem please? – MikeBee59 Feb 04 '21 at 13:24

2 Answers2

1

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.

Anonymous Penguin
  • 6,285
  • 10
  • 32
  • 62
Jon
  • 466
  • 2
  • 5
  • 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 07: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
  • uploaded the screenshot. – Omer Jan 31 '15 at 14:10
  • I know it's an old post, buy my ATTiny85 talked successfully to the BMP280 sensor via I2C using TinyWireM. And no, the SYS_CLK 8Mhz or 1Mhz doesn't make any difference. It works with any T2 and T4 for me - the 5 and 4 default (faster) or 40 and 32 (just slower). My issue was that I've bit-banged the LCD screen and then initialized via TinyWireM the BMP280 sensor... that mix doesn't work. – Volodymyr Kotylo Apr 01 '20 at 21:19
0

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 :)

Avamander
  • 624
  • 2
  • 11
  • 35