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:
- 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.
- We are using the Adafruit WebIDE and using SSH to remotely access the Pi from our Mac.
|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.
- 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.
- 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:
- 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:
- 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.
You should see the LED turning on and off and 2 second intervals. See the video below to see this all in action: