NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Looking for I2C tutorial/guide

October 28, 2009
by tech20
tech20's Avatar

I have been using the nerd kit for about 3 months so far and have learned the basic and somewhat advanced concepts of the MCU, but I2C seems to be one were i can't find any tutorials that will give a indepth look into how good look into how it works on the MCU side, as most have a large prewritten library which is mainly giving you the intire code needed, but not explaining it. Has anyone found a "bare-bones" style tutorial were they show you how it works in code or such?

October 28, 2009
by mikedoug
mikedoug's Avatar


I've not done ANY programming with the SPI, but I've read a bit about it. The MPU documentation is the first place I'd point to ( Chapter 21: 2-wire Serial Interface (starting on page 210) seems to be the place you want. You'll notice the last feature listed is "Compatible with Philips I2C protocol".

Again -- I've not done ANY I2C programming (I don't even have a device I could test this with), and nothing I say here should be taken as gospel -- at least not until the nerds check it out.

Section 21.6 is where it begins to teach you how to program the unit. You have two choices: 1) Interrupt based and 2) polling based. The interrupt method is the most efficient method, but the polling will be easier to implement. Figure 21-10 shows you the flow of how things work, with a detailed step-by-step description immediately following the diagram.

Wow... I was going to walk through more of this, but let's just say that chapter 21 is VERY involved. Luckily it does include some "bare-bones" code on page 222 (in the "C Example" column) -- it's nicely broken down by the same phases as figure 21-10. This example is treating the MPU as a master transmitter to transmit data (I believe). The other three methods of operation don't have code samples, but are pretty detailed.

Can you tell us more about what you're hooking up, and post some code that you're already trying to get working -- we might be able to help debug/offer suggestions based on your code and the documentation itself.

I'm going to have to get me a device that does I2C -- just for the sake of doing it. :)


October 28, 2009
by mikedoug
mikedoug's Avatar

Wow... Lots of good I2C information here:

This might be of interest -- though I don't want to pay to get their code, but looking through a library like this just might help you understand it:


November 09, 2009
by jbremnant
jbremnant's Avatar

I tried interfacing with Wii Nunchuck controller which uses I2C in fast mode (40kHz). I have some code that works to certain extent - it can initiate communication with the nunchuck and starts reading data from it. However, the data coming back seem incomplete and I get intermittent hangs. Not sure why, and I haven't had the time to dig in deeper.

If you guys would like to take a look at the code and improve/fix it, I'd be great. Also hope it could be a starting point for getting I2C working on nerdkit.

You can download the source code here.

  • twi_simple.c - blocking implementation of I2C master to slave communication
  • twi.c - non-blocking interrupt based implementation of I2C. This is part of arduino distribution.

Post a Reply

Please log in to post a reply.

Did you know that our USB NerdKit works on Windows, Linux, and Mac OS X? Learn more...