In the last post we looked at testing new I2C devices using the Bus Pirate when prototyping a new design.  In this post we look at making a more permanent implementation by interfacing the MCP4725 Digital-to-Analog Converter to the Raspberry Pi.  Just as last time, we will be driving an LED.  Some notes about our particular setup:

  1. We are using a Raspberry Pi running Occidentalis v0.2 from Adafruit Industries.  We are also using their I2C Library Adafruit_I2C.py.  We also forked their MCP4725 library so we could come with our own, more generic I2C library.
  2. We are using the Adafruit WebIDE and using SSH to remotely access the Pi from our Mac.
First, let’s start with how we wired up the MCP4725 breakout board and the Raspberry Pi via the Cobbler interface board.
RPi_MCP4725
Connecting a Raspberry Pi and the MCP4725 DAC Breakout Board

Now let’s look at the software.  You can download the files here from our BitBucket page.  The software is built in multiple layers.

Screen Shot 2013-12-02 at 12.04.21 AM
I2C_DAC.py
  1. SMBUS: First, built within the python environment in Occidentalis is the smbus library.  The Adafruit_I2C library relies on smbus to implement the I2C protocol to the GPIO pins.  PLEASE NOTE, smbus takes the Least Significant Bit (LSB) FIRST!  That means, you have to take care as we move up the software stack, to ensure we provide the byte containing the LSB first and finish with the Most Signicant Bit (MSB).  In short, in the last we fed the commands to the MCP4725 chip via the Bus Pirate in the exact OPPOSITE way.  Stay tuned to see the difference.
  2. ADAFRUIT_I2C.PY: The Adafruit_I2C library provides the generic framework for any I2C device.  We will have to build an additional custom library (building upon this library) for each I2C device we wish to interface with, which leads us to:
  3. I2C_DAC.PY:  We started with the generic MC4725.py library provided by Adafruit.  If however, you would want to implement a device that doesn’t have a pre-existing I2C interface library, this is where you would start in the stack by writing your own custom class that relies on the aforementioned Adafruit_I2C.py library.  Here is a code snippet:
  4. MAIN.PY:  Lastly, a driver program that tests and uses our custom I2C_DAC.py library.  This is also the file that will well actually run.
There are a great number of tutorials out there dedicated to hooking up an I2C device to a Raspberry Pi in general terms.  If you are running Occidentalis, once you have everything hooked up and before you have written any custom code, ensure the Raspberry Pi can see the I2C device by firing up a Terminal and SSH into the Pi, then execute the following:
pi@raspberrypi ~ $   sudo i2cdetect -y 1
You should see the following, with the address of your I2C device listed as follows:
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: 60 — — — — — — — — — — — — — — —
70: — — — — — — — —
pi@raspberrypi ~ $
Our 7-bit address of the MCP4725 is indeed 0x60 (remember, the adding the R/W bit gives the raw address as 0xC0 that we used in the Bus Pirate example).  Now navigate via the terminal to where we place the main.py and associated files.  To run the program enter:
pi@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/AdaFruit_I2C_Custom $ sudo python main.py  

You should see the LED turning on and off and 2 second intervals.  See the video below to see this all in action: