Using the Adafruit Library

The Python code for Adafruit's MCP4725 breakout on the Pi is available on Github at  https://github.com/adafruit/Adafruit_Python_MCP4725

This code should be a good starting point to understanding how you can access SMBus/I2C devices with your Pi, and getting things moving with your DAC breakout.

Downloading the Code from Github

The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros.  Simply run the following commands from an appropriate location (ex. "/home/pi"):
sudo apt-get install git build-essential python-dev
cd ~
git clone https://github.com/adafruit/Adafruit_Python_MCP4725.git
cd Adafruit_Python_MCP4725
sudo python setup.py install

Testing the Library

Once the code has be downloaded to an appropriate folder, and you have your MCP4725 breakout properly connected, you can test it out with the following command (the driver includes a simple demo program in the examples subfolder): 

cd examples
sudo python simpletest.py

This will alternate the voltage of the output from VDDto VDD/2 to 0 and back.  You can use a multimeter or oscilloscope to check the voltage on the output and see it changing between values.

Writing Your Own Code

The MCP4725 library is quite straight forward, and includes a single function: .set_voltage(voltage), with voltage having a value between 0 and 4095 (corresponding to a 12-bit value).  If you're new to Python, though, have a look at the simpletest.py example to see how you can instantiate an instance of the base DAC class, and change the voltage.

From the command-line you can edit this file in nano (vi is also included if you prefer that) with the following command:

sudo nano simpletest.py
This will result in something similar to the following, though we've scrolled down a few pages to show the actual instantiation code, etc.:
# Simple demo of setting the output voltage of the MCP4725 DAC.
# Will alternate setting 0V, 1/2VDD, and VDD each second.
# Author: Tony DiCola
# License: Public Domain
import time

# Import the MCP4725 module.
import Adafruit_MCP4725

# Create a DAC instance.
dac = Adafruit_MCP4725.MCP4725()

# Note you can change the I2C address from its default (0x62), and/or the I2C
# bus by passing in these optional parameters:
#dac = Adafruit_MCP4725.MCP4725(address=0x49, busnum=1)

# Loop forever alternating through different voltage outputs.
print('Press Ctrl-C to quit...')
while True:
    print('Setting voltage to 0!')
    dac.set_voltage(0)
    time.sleep(2.0)
    print('Setting voltage to 1/2 Vdd!')
    dac.set_voltage(2048)  # 2048 = half of 4096
    time.sleep(2.0)
    print('Setting voltage to Vdd!')
    dac.set_voltage(4096, True)
    time.sleep(2.0)
Last updated on 2016-04-20 at 04.23.54 PM Published on 2012-08-17 at 10.39.28 AM