I2C on the ATtiny85

I like this new place. It's like moving into a new office! :)

Just to say that I made a Playground article that provides the libs for I2C (master and slave) on the ATtiny85. It's here . . . http://www.arduino.cc/playground/Code/USIi2c

Hope some of you find it useful.

First off, thank you for writing this!

I gave it a whirl talking (or not, heh) to a AD5171 digital cap, I didn't get any output from pin5 or pin7 on the attiny, though I discovered when i was hooking it up that the closest to 4.7k resistors I had was 3.9k.

I'm going to spend some more time with it later on today hopefully, I'd be very, very pleased if I can get it to work as I was going to have to work out a way to bitbang it, which is rather over my head!

I think 3.9K is close enough. (They are pull [u]ups[/u] right?) So I'll assume . . . - your using the I2C master lib (TinyWireM) - it compiles and loads to the ATtiny fine - you have an LED on one of the other pins showing the sketch started - you put a scope on pin 5 (SDA) and pin 7 (SCL) and saw nothing

Humph, as you can see in the examples that come with the lib, as a master, I had it talking to an I2C temp sensor, RTC and a GPIO. The AD5171 looks like a straight forward chip to interface to.

What ATtiny core files are you using? I've only been using those from "high-low tech", and loading with the ArduinoISP or the USBtinyISP.

I haven't done the scope yet, I'm going to take it to work on monday and do that. Got some 4.7k resistors for good measure. The attiny core files are one generation old now, I need to update. That might be the problem right there.

I'll update the core files and check it out on the scope and post back after that.

Bob, Could confirm my assumptions - i.e.

  • your using the I2C master lib (TinyWireM) - YES ?
  • it compiles and loads to the ATtiny fine - YES ?
  • you have an LED on one of the other pins showing the sketch started - "NO BUT GOOD IDEA . . . YES IT DOES" ?
  • you put a scope on pin 5 (SDA) and pin 7 (SCL) and saw nothing - NO, BUT I KNOW I'M NOT GETTING OUTPUT BECAUSE . . .

That would help me help you.

What ATtiny core files are you using? I've only been using those from "high-low tech", and loading with the ArduinoISP or the USBtinyISP.

I'm now guessing you are using the core files Coding Badly is involved with. I haven't tested with those. Try the cores from here http://hlt.media.mit.edu/wiki/pmwiki.php?n=Main.ArduinoATtiny4585

You can change boards.txt if your using a programmer other than ArduinoISP. For example:

attiny852.name=ATtiny85 (Load w/ USBtinyISP)
attiny852.upload.using=arduino:usbtinyisp
attiny852.upload.maximum_size=8192
attiny852.build.mcu=attiny85
attiny852.build.f_cpu=1000000L
attiny852.build.core=attiny45_85

(What programmer are you using?)

The "hlt" core works fine and it's simpler in some ways - a lot less going on in boards.txt. If you don't like that core for some reason, I still suggest you try it first - you can always go back to the other after you see everything working.

Humph, Looks like I scored a -karma for my efforts! Great system. :~

@Bobnova Since I figured you were using the cores from here . .. http://code.google.com/p/arduino-tiny/ I tested those cores with my libs.

After changing boards.txt to upload with the USBtinyISP I found that the TinyWireM lib worked with those cores.

However, the sketch was running 10X slower - even the Blink sketch. :. So I changed the speed in boards.txt from f_cpu=800000L to f_cpu=1000000L and now it works fine. I don't know why one set of cores uses an 8MHz clock and another set uses a 10MHz clock.

BroHogan: However, the sketch was running 10X slower - even the Blink sketch. :.

Was it 8X?

So I changed the speed in boards.txt from f_cpu=800000L

800K is nowhere to be found in boards.txt. Was it an 8M entry you changed?

to f_cpu=1000000L and now it works fine.

Are the target processor's fuses set to run at 1 MHz? Or 8 MHz?

I don't know why one set of cores uses an 8MHz clock and another set uses a 10MHz clock.

The HIT core uses a 10 MHz clock?

Kinda busting my chops on this aren't you?

Was it 8X?

Could have been 8X could have been 10X I didn't put a stopwatch on it. It was at least 8X. It is a critical difference for making a determination?

800K is nowhere to be found in boards.txt. Was it an 8M entry you changed?

Ya got me! I dropped a 0 during the post. I changed it from 8MHz to 10MHz as I stated. As you know boards.txt in google code core is set for 8MHz.

Are the target processor's fuses set to run at 1 MHz? Or 8 MHz?

I suggest you look at the google code boards.txt and answer that yourself. I made no changes.

The HIT core uses a 10 MHz clock?

That's what I said. You can download the HIT core and verify that if you wish.

In the google code site for the core you asked if someone would verify if these libraries would work with that core. This is what I've done, and this is, I guess, my thanks.

Kinda busting my chops on this aren't you? This is what I've done, and this is, I guess, my thanks.

I do apologize for upsetting you. That was not my intention. I'm simply trying to determine if there is a problem with either core or if there is a misunderstanding on how either of them should work. I simply don't want another user to have the same problems that you've just had. Both cores should be simple to install and easy to use. I promise to refrain from any further communication.

For what it's worth, I appreciate your efforts and thank you for your time.

Well, I was wrong! Adamant, but wrong. I was posting instead of sleeping, and between cut & paste and counting zeros, I screwed up.

  • boards.txt for HIT core is set for 1MHz.
  • boards.txt for "google code" core is set for 8MHz. (It also sets fuses which sets the internal clock speed.)

Nonetheless, when I used the "google code" core, Blink ran 8X slower. When I changed it from f_cpu=8000000L to f_cpu=1000000L it ran normally. Reloading again with the HIT core it continued to run normally.

It looks like the fuse settings in boards.txt for "google code" aren't getting set for some reason.

That's all I know. Sorry about the confusion.

In any case, the ATtiny I2C libs work with either core.

BroHogan: - boards.txt for "google code" core is set for 8MHz. (It also sets fuses which sets the internal clock speed.)

It looks like the fuse settings in boards.txt for "google code" aren't getting set for some reason.

Ah! In order to change the fuse settings you have to select "ATtiny85 @ 8 MHz" then execute "Burn Bootloader". "Burn Bootloader" is also a nice way to clean the slate when switching to another project or putting a processor into storage. In addition to changing the fuse settings an empty do nothing Sketch is uploaded.

Well, I was wrong! Adamant, but wrong. I was posting instead of sleeping, and between cut & paste and counting zeros, I screwed up.

The moral of the story is ... friends don't let friends play with Arduinos when sleep deprived? ;)

  • boards.txt for HIT core is set for 1MHz.

The "google code" should also have a "ATtiny85 @ 1 MHz" option. It should be almost identical in behaviour to the HIT core.

In any case, the ATtiny I2C libs work with either core.

Excellent news! Thanks!

select "ATtiny85 @ 8 MHz" then execute "Burn Bootloader".

Ive tried that, but I get errors. (Perhaps it's best I PM you on them, or start another thread.)

In any case, I managed to set the fuses properly using Avrdude. (Every evening this week I found myself being drawn deeper and deeper into the dark side. :cold_sweat:) So now I am able to test the libs with both cores at both 1MHz and 8MHz.

I wonder if anyone else has tried these libs yet.

Sorry it took me so long to get back to you, it's been a hectic week around here. I got all set up with the scope this morning and the 4.9k resistor pull-ups, and the scope picture was perfect so I plugged in the digipot (which controls a fan pwm controller) and it worked perfectly too!

So either A) 3.9k resistor pull-ups do not work, or B) I didn't have them all the way inserted into the breadboard. In any case, you code is working perfectly for me, thanks! (I made no changes in software/firmware/sketch/etc)

Sorry if I cause you mental issues with my screwup!

Blink seems to be running the right speed for me, for whatever that is worth.

Aces! Great to hear. Thanks!

bHogan,

I'm going to give this a try with attiny85 as i2c slave. Has anyone tried to implement onReceive and onRequest? Is there any fundamental reason why this would not work on attiny85?

Thanks for the work to date, George

Is there any fundamental reason why this would not work on attiny85?

I don't think there is. It was just a bit over my head so I didn't take the time to do it.

If you add those, let me know and I'll update the libs I put on the Playground.

Posting in this old thread because this is the google top entry for attiny85 i2c and the question is relevant.

Does anyone know how to change the slave address for an Attiny?

The default appears to be 0x26 from the TinyWireS library code but i've been looking in the attiny85 datasheet and cannot find where it's specified nor how to change it (because I don't know what to look for).

I want to change it to allow me to connect to two (or more) Attiny85's.

You set the slave address in your sketch with the .begin -

define I2C_SLAVE_ADDR 0x26 // i2c slave address (38)

TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode

Thanks.

I was actually most of the way through determining it for myself :)

For anyone interested in I2C for the attiny and other atmel AVR mc's the atmel here's what I've found.

The atmel application notes can be found here http://www.atmel.com/products/microcontrollers/avr/tinyAVR.aspx?tab=documents

Scroll down and look for Application notes AVR310, AVR311, AVR312 and AVR315 and have a read. It's in here that I found the specification for the pullups, the C commands and process flow diagrams.

My determination is that to connect to multiple attiny85 slaves you skip the define and specify the address you want in the TinyWireS object begin method calls for each attiny you want to slave (or use multiple defines).

From what I could determine there is no default address, it's all in the initialisation (the begin function)

My determination is that to connect to multiple attiny85 slaves you skip the define and specify the address you want in the TinyWireS object begin method calls for each attiny you want to slave (or use multiple defines).

From what I could determine there is no default address, it's all in the initialisation (the begin function)

Ah yea. It looks like what you determined pretty much agrees with my post above. That's nice.