ATtiny Adventure -- I2C on ATtiny 84/85
Tags: robots, attiny85, attiny84, i2c
Published: 2013-11-11 10:00:00

---
title: ATtiny Adventure -- I2C on ATtiny 84/85
published: true
tags: robots, attiny85, attiny84, i2c
published_at: 20131111T10:00Z
series: Custom PCBs
canonical_url: https://ladvien.com/attiny-i2c
main_image: https://ladvien.com/images/ATTiny_Adventure_2.jpg
---


[Code](/files/Arduino_to_ATtiny_85__through_I2C_2_4.zip)

{% embed https://www.youtube.com/embed/gxLbelXcy1I %}

UPDATE: Added info on making SPI programming jig (makes life a lot easier).

UPDATE: Added ATtiny 84 info (though, the post is meant for the ATtiny 85).

I've been non-traditional microcontroller curious for a bit.  Then, I had to put a Digi-Key order together for some real cheap stupid stuff (some SOT-23 N-Channels for the LiPo charger circuit) and I thought, "What the hell, let's order some ATTiny 85s."  Being cheap like I am, I ordered SMD:

* [ATTiny 85 -- SOIC-8](http://www.digikey.com/product-search/en?pv7=2&k=ATTINY85-20SU-ND&mnonly=0&newproducts=0&ColumnSort=0&page=1&quantity=0&ptm=0&fid=0&pageSize=25) ($1.25 each)

I then ran over to OSHPark and made a little breakout for it:

* [ATTiny 85 -- SOIC-8 Breakout board](http://www.oshpark.com/shared_projectss/lcmVFzMW) ($.85 for 3, $.28 each)
* [ATtiny 84 -- SOIC-14 Breakout board with pogo-pin header](http://www.oshpark.com/shared_projectss/MBPvF8Qb) ($1.85 for 3).

This brought the **price for one ATTiny 85 board to $1.53 each**.  This is great, since the ATTiny 85 has an internal oscillator up to  8mhz, allowing it run **without any passives**.

![](https://ladvien.com/images/IMG_9166.jpg)

I was pretty excited the day they came in.  I soldered them together, put some headers on them, and tossed them into a bread board.  I switched over to Goggle and searched how to program these little guys.  The first article I hit was the one I eventually used, I just warn you, dear reader, read careful not to miss the bit about ignoring the error.  Personally, like the dumb-arse I am, programmed my first little ATtiny 85 a hundred times thinking it wasn't working before I caught the caveat in the instructable:

**"It should give the following error twice: avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny 85"**

**This error means you programmed it successfully.**

But you all probably got that.

* [Program an ATtiny 85 with an Arduino](http://www.instructables.com/id/Program-an-ATtiny-with-Arduino/)
* [Program an ATtiny 84 with an Arduino](http://www.instructables.com/id/Using-the-Arduino-Uno-to-program-ATTINY84-20PU/)

The easiest way to get going with I2C with the ATtiny 85 is using the TinyWireS and TinyWireM [libraries for Arduino](http://playground.arduino.cc/Code/USIi2c).  

* [TinyWireS](https://github.com/rambo/TinyWire) (this is Rambo's library, he updated the original with onRequest, onReceive functions)
* [TinyWireM](http://playground.arduino.cc/uploads/Code/TinyWireM.zip)

To get the ATtiny 84 to work you'll need to add support:

*  [TineWireS](https://github.com/svoisen/TinyWire) (supporting ATtiny 84)

They were developed to be comparable to the Wire library for Arduino.  To install them, just unzip them and place them in your Arduino libraries folder (e.g., C:\Program Files\Arduino\libraries).

![](https://ladvien.com/images/attiny_pinout.jpg)

[(Here's the pinoutfor the ATtiny 84](http://www.ledsandchips.com/upload/cards/attiny.jpg))

The I2C connections are pretty straight forward:

* **Arduino SDA** <--- 4.7k Resistor Tied to 5v ----> **ATtiny 85 -- PB0**
* **Arduino SCL** <--- 4.7k Resistor Tied to 5v ----> **ATtiny 85 -- PB3**
* **ATtiny 85 -- PB1** <--- 330 Resistor ---- LED ---- > **GND**

Below is code meant to demonstrate the purpose of this projects.  It sets the ATtiny 85 as an I2C slave.  It receives data over the I2C line, parses it into an integer, then writes this value of the integer to pin 1 (PB1).

```c
// the 7-bit address (remember to change this when adapting this example)
#define I2C_SLAVE_ADDRESS 0x4

// Get this from https://github.com/rambo/TinyWire
#include <TinyWireS.h>

// The default buffer size, Can't recall the scope of defines right now
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif

//Character variable used to echo data back.
char chrSendData;

//Variables used in getting and parsing data.
char rxChrData; //Receives the data.
char rxString[12];  //Varbiable for holding one string of data.
int rxIndex = 0; //Used to index rxString.

//Integer for holding the pwm value received from master.
int pwmValA;

void requestEvent(){  
  TinyWireS.send(chrSendData);
}

//Handles receiving i2c data.
void receiveEvent(uint8_t howMany)
{
    if (TinyWireS.available()){  
      if (howMany < 1)
      {   // Sanity-check
          return;
      }
      if (howMany > TWI_RX_BUFFER_SIZE)
      {   // Also insane number
          return;
      }

      howMany--;
      if (!howMany)
      {   // This write was only to set the buffer for next read
          return;
      }
      while(howMany--)
      {   //Gets i2c data.
          rxChrData = TinyWireS.receive();
          //Places the characters in an array one at a time.
          rxString[rxIndex] = char(rxChrData);
          //Increment the data array.
          rxIndex++;
          //If a stop character is read, parse the char array and convert it to a single integer.  
          if (rxChrData == ':'){
              //This is a low memory form of parsing the char array into an intger
              pwmValA = int(100*rxString[2]+10*rxString[3]+rxString[4]);
              //Prints the parsed value.
              Serial.println(pwmValA);
              //Writes the parsed value to pin 1 (PB1).
              analogWrite(1, pwmValA);
              //Resets the char array index.
              rxIndex = 0;  
         }
      }
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(1, OUTPUT); // OC1A, also The only HW-PWM -pin supported by the tiny core analogWrite
    TinyWireS.begin(I2C_SLAVE_ADDRESS);
    //Sets up the onReceive function (what we do if we get stuff).
    TinyWireS.onReceive(receiveEvent);
    //Sets up the onRequest function (what we do if asked to send something).
    TinyWireS.onRequest(requestEvent);
}

void loop()
{
//Detects a stop sending command.
TinyWireS_stop_check();

//Puts the data we got into a variable to send back for error checking.
chrSendData = char(rxChrData);

}
```
I've also included the code I used on my Arduino Mega, which was setup as the master.

Your setup **should not look like this :P**

![](https://ladvien.com/images/IMG_9163.jpg)

I've got several ideas I'd like to attempt with this setup.  But, it is somewhat silly.  The I2C reduces the ATtiny 85 to four pins.  But one of those is the reset pin (PB5), so really, only 3 usable pins.  

Before I started working with the Tiny I was lurking in the shoutbox and oversaw [Protowrx](http://letsmakerobots.com/user/18840) chatting about making an ATtiny into a serially controlled motor driver.  So, I set to it.  I had chosen I2C because I wanted to make a setup like the [DRV8830](http://dlnmh9ip6v2uc.cloudfront.net/datasheets/BreakoutBoards/drv8830.pdf) (Spark fun has a [breakout](https://www.sparkfun.com/products/11890)). Of course, like the numbskull I am I didn't do the simple math before sinking hours into interfacing with a chip.

Most H-Bridge ICs require three pins a motor.  Two digital pins for direction and one for PWM.  Even cheaping out and using one PWM pin for both motors, that's still five.  _And..._the ATtiny 85 has 8 pins.  1 x Power, 1 x Ground, 2 x I2C line, which leaves us with....**4 pins.** Oh wait! One of those is the reset pin and cannot be used without losing the ability to program it without an AVR programmer (which I have, but what a pain in the ass).  So! In short, there are 3 usable pins after interfacing with the ATtiny. I'd have done myself a favor if I had [remembered an 80s classic](http://www.youtube.com/watch?v=O5ROhf5Soqs).

Still, I've I got it in my head to attempt doing something like this: [2 Pin HBridge control](http://forum.arduino.cc/index.php?topic=133833.0). Only, tying the PWM line together.  Not having much luck right now (only spent about 20 minutes, wanted to get this typed up before I forgot crap).

Another idea is to use a Software Serial to [send one way communication](http://www.ernstc.dk/arduino/tinycom.html) through the serial line.  But it doesn't make much sense, since 4 pins aren't much better than 3, given my intentions.

Ok. In conclusion, I'm not sure why I did this.  It really doesn't make much sense, other than the adventure.  I'm sure it's one of those things I work out now and I won't find a use until much later.  The real killer is thinking about how you can buy a full Arduino Pro Mini on the [eBay](http://www.ebay.com/itm/Arduino-PRO-MINI-ATMEGA328-5V-16M-MWC-avr328P-Development-Board-/271251071444?pt=LH_DefaultDomain_0&hash=item3f27d2e1d4) for $3.45. A little more than double the cost of an ATtiny 85 but triple the pins and utility.  Eh!

**Making a ATtiny Jig:**

I hate breadboarding.  Let me admit that.  Mainly, it is having wires everywhere, my little dyslexic brain can't keep up.  And when I first started working with the ATtiny uCs I found it to be a pain to have to move my little ATtiny's between my full circuit and the SPI programming circuit.  So, I thought, "Why not make a SMD programming pad and jig interface?"

Well, here is the crude son-of-a-bitch:

![](https://ladvien.com/images/IMG_0309.jpg)It's nothing fancy, but it is a time saver.

I put both the interface pads and the jig in my Eagle library.

**POGO_PIN_JIG_SPI**

**POGO_PIN_SPI_PAD**

[Ladvien's Eagle Library](https://github.com/Ladvien/Eagle_Library)

Here is my ATtiny 84 with the pads and the Jig PCB:

[Jig](http://www.oshpark.com/shared_projectss/BjydrnT8) ($.60

[ATtiny 84 PCB](http://www.oshpark.com/shared_projectss/MBPvF8Qb) ($1.85)

These are the pogo pins I used:

[P100-B1 -- 1.36mm diameter](http://www.ebay.com/itm/151107292577?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649)

![](https://ladvien.com/images/IMG_0307.jpg)

It was not too hard to put together, I set it up something like this.  Then, it is all about added flux to where the pins meet the PCB and soldering like you would usual header pins.  

![](https://ladvien.com/images/IMG_0312.jpg)And here it is in action.  It surprised the hell out of me, worked exactly like I wanted it.

I'm sure I'll eventually add some stabilizer bars between the two-PCBs and maybe a guide pin to prevent me from pressing the pins in the wrong holes :(

Still, it is MUCH easier than pulling it from the breadboard and moving it to a new circuit.  Makes me happy.