# MCP4725 12-Bit DAC with Raspberry Pi

## Overview

Danger: 

![](https://cdn-learn.adafruit.com/assets/assets/000/001/738/medium800/raspberry_pi_DAC_Scope.png?1396775912)

Already mastered&nbsp;[Analog Inputs with the Pi](http://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi), and looking for a new challenge? &nbsp;How about:&nbsp;How can&nbsp;I&nbsp;generate an analog output on the Pi?!  
  
There are several ways you can accomplish this, but one of the easiest and most flexible is to use a dedicated IC called a **Digital to Analog Convertor** &nbsp;(or DAC).&nbsp;&nbsp;A DAC allows you to specify a numeric value (0..255 for an 8-bit DAC, 0..4095 for a 12-bit DAC, etc.), and the IC will output a voltage based on the supply voltage, and relative to that numeric value. &nbsp;For example, using a 12-bit DAC like the [MCP4725](http://www.adafruit.com/products/935) we'll be using here, setting the value to 2048 on a 3.3V system will results in ~1.65V output on the DAC.  
  
This guide will show you everything you need to know to be able to generate precise analog outputs using your Pi and the MCP4725 12-Bit I2C DAC, from connecting everything up, to how to use our easy Python library.

# What You'll Need
The following products are used in&nbsp;this tutorial:  

- [Adafruit MCP4725 12-Bit DAC](http://www.adafruit.com/products/935 "Link: http://www.adafruit.com/products/935")
- [Pi Cobbler](https://www.adafruit.com/products/914)

# MCP4725 12-Bit DAC with Raspberry Pi

## Configuring Your Pi for I2C

Danger: 

Before you can get started with I2C on the Pi, you'll need to run through a couple quick steps from the console. &nbsp;  
  
If you are running Occidentalis and are familiar with Terminal commands, then the description below will be sufficient.  
  
If not, then to learn more about how to&nbsp;setup I2C with either Raspbian or Occidentalis, then take a minor diversion to this Adafruit Tutorial:&nbsp;[http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c](http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c "Link: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c")  
  
When you are happy to continue&nbsp;enter the following commands to add SMBus support&nbsp;(which includes I2C) to Python:

```
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
```

i2c-tools isn't strictly required, but it's a useful package since you can use it to scan for any I2C or SMBus devices connected to your board. &nbsp;If you know something is connected, but you don't know it's 7-bit I2C address, this library has a great little tool to help you find it:

```
sudo i2cdetect -y 0
```

This will search /dev/i2c-0 for all address, and if an MCP4725&nbsp;DAC breakout is properly connected and it's set to it's default address it should show up at 0x62.  
  
If you are using a 512MB Raspberry Pi version 2, you'll want to use /dev/i2c-1 by running **sudo i2cdetect -y 1** as the i2c port # changed from #0 to #1

![](https://cdn-learn.adafruit.com/assets/assets/000/001/721/medium800/raspberry_pi_DAC_I2CDETECT.png?1396775664)

Once both of these packages have been installed, you have everything you need to get started accessing I2C and SMBus devices in&nbsp;Python.

# MCP4725 12-Bit DAC with Raspberry Pi

## Hooking It Up

Danger: 

The easiest way to hook the MCP4725 up to your Pi is with the Adafruit Pi Cobbler, as seen in the wiring diagram below:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/725/medium800/raspberry_pi_DACWireup.png?1396775745)

The output voltage will be visible on **VOUT** , which you should connect to your target device, or to a [multimeter](https://www.adafruit.com/products/71) or [oscilloscope](https://www.adafruit.com/products/681) if you just want to see the results.

# MCP4725 12-Bit DAC with Raspberry Pi

## Using the Adafruit Library

Danger: 

The Python code for&nbsp;Adafruit's&nbsp;MCP4725 breakout&nbsp;on the&nbsp;Pi&nbsp;is available on Github at &nbsp;[https://github.com/adafruit/Adafruit\_Python\_MCP4725](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. &nbsp;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&nbsp;an appropriate folder, and you have your MCP4725 breakout properly connected, you can test it out with the&nbsp;following command (the driver includes a simple demo program in the **examples** subfolder):&nbsp;

```
cd examples
sudo python simpletest.py
```

This will alternate&nbsp;the voltage of the output from VDDto VDD/2 to 0 and back. &nbsp;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). &nbsp;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)
```


## Featured Products

### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)
### MCP4725 Breakout Board - 12-Bit DAC with I2C Interface

[MCP4725 Breakout Board - 12-Bit DAC with I2C Interface](https://www.adafruit.com/product/935)
Your microcontroller probably has an ADC (analog -\> digital converter) but does it have a DAC (digital -\> analog converter)??? Now it can! This breakout board features the easy-to-use MCP4725 12-bit DAC. Control it via I2C and send it the value you want it to output, and the VOUT pin...

Out of Stock
[Buy Now](https://www.adafruit.com/product/935)
[Related Guides to the Product](https://learn.adafruit.com/products/935/guides)

## Related Guides

- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [CircuitPython MIDI to CV Skull](https://learn.adafruit.com/circuitpython-midi-to-cv-skull.md)
- [MCP4725 12-Bit DAC Tutorial](https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial.md)
- [Analog Inputs for Raspberry Pi Using the MCP3008](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.md)
- [piBeacon - DIY Beacon with a Raspberry Pi](https://learn.adafruit.com/pibeacon-ibeacon-with-a-raspberry-pi.md)
- [Running OpenGL-based Games & Emulators on Adafruit PiTFT Displays](https://learn.adafruit.com/running-opengl-based-games-and-emulators-on-adafruit-pitft-displays.md)
- [Raspberry Pi LED Matrix Display](https://learn.adafruit.com/raspberry-pi-led-matrix-display.md)
- [Introducing the Raspberry Pi 2 - Model B](https://learn.adafruit.com/introducing-the-raspberry-pi-2-model-b.md)
- [AdaBox 005](https://learn.adafruit.com/adabox005.md)
- [Internet of Things Printer for Raspberry Pi](https://learn.adafruit.com/pi-thermal-printer.md)
- [Programming Microcontrollers using OpenOCD on a Raspberry Pi](https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi.md)
- [Connecting the MAX31855 Thermocouple Amplifier breakout to an Electric Imp](https://learn.adafruit.com/connecting-the-max31855-thermocouple-amplifier-breakout-to-an-electric-imp.md)
- [Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi](https://learn.adafruit.com/connecting-a-16x32-rgb-led-matrix-panel-to-a-raspberry-pi.md)
- [BNO055 Absolute Orientation Sensor with Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/bno055-absolute-orientation-sensor-with-raspberry-pi-and-beaglebone-black.md)
- [Matrix and 7-Segment LED Backpack with the Raspberry Pi](https://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi.md)
