It's easy to use the MCP4725 digital to analog converter with Python and CircuitPython, and the Adafruit CircuitPython MCP4725 module.  This module allows you to easily write Python code that controls the output voltage from the DAC.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a MCP4725 to your board exactly as shown on the previous pages for Arduino using an I2C connection.  Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3V to sensor VIN (red wire on STEMMA QT version)
  • Board GND to sensor GND (black wire on STEMMA QT version)
  • Board SCL to sensor SCL (yellow wire on STEMMA QT version
  • Board SDA to sensor SDA (blue wire on STEMMA QT version

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V3 to sensor VIN (red wire on STEMMA QT version)
  • Pi GND to sensor GND (black wire on STEMMA QT version)
  • Pi SCL to sensor SCL (yellow wire on STEMMA QT version)
  • Pi SDA to sensor SDA (blue wire on STEMMA QT version)

CircuitPython Installation of MCP4725 Library

Next you'll need to install the Adafruit CircuitPython MCP4725 library on your CircuitPython board.  Make sure you are running the latest version of Adafruit CircuitPython for your board before starting..

You'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  For example the Circuit Playground Express guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_mcp4725.mpy

You can also download the adafruit_mcp4725.mpy from its releases page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_mcp4725.mpy file copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of MCP4725 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-mcp4725

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the DAC we'll initialize it and set the output voltage from the board's Python REPL.  Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import board
import busio
import adafruit_mcp4725
i2c = busio.I2C(board.SCL, board.SDA)
dac = adafruit_mcp4725.MCP4725(i2c)

Now you can set the output voltage just like controlling a DAC with CircuitPython's built-in AnalogOut class and the value property.  Simply set this to any 16-bit value (0-65535) and the output of the Vout pin will change to a voltage proportional to 0-3.3V.  For example to set the output to 1.65V or about halfway within its range:

dac.value = 32767

Hook up a multimeter to the Vout pin of the board (positive/red lead to Vout, ground/black lead to board GND) and you should see about 1.65 volts DC output.  Try setting dac.value to other numbers like 0 or 65535 to see how the voltage changes.

Hook up a multimeter to the Vout pin of the board (positive/red lead to Vout, ground/black lead to board GND) and you should see about 1.65 volts DC output.  Try setting dac.value to other numbers like 0 or 65535 to see how the voltage changes.

You can use the MCP4725 instance anywhere you might use the AnalogOut class!

However you might prefer a few other simpler properties to change the output voltage:

  • normalized_value - Set this to a floating point number between 0 and 1.0.  A value of 0 is ground/0V and 1.0 is Vdd or max voltage/3.3V.  Anything in-between is a proportional voltage.  This is handy for scaling the output value without having to worry about how many bits of resolution it has.
  • raw_value - Set this to a 12-bit value 0-4095 to control the raw 12-bit output of the DAC.  Unlike the value property this raw_value exposes the true 12-bit resolution of the DAC and is free from quantization errors.  If you need the most precise output use the raw_output value for setting voltage.
dac.normalized_value = 0.5  # ~1.65V output
dac.raw_output = 2047 # Also ~1.65V output

That's all there is to using the MCP4725 DAC with CircuitPython!

Below is a complete example that shows changing the DAC voltage to a triangle wave that goes up and down repeatedly.  Save this as code.py on your board and connect a multimeter to measure the Vout pin voltage to see it oscillate up and down from 0 to 3.3V and back.

Full Example Code

# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
# SPDX-License-Identifier: MIT

# Simple demo of setting the DAC value up and down through its entire range
# of values.
import board
import busio

import adafruit_mcp4725


# Initialize I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)

# Initialize MCP4725.
dac = adafruit_mcp4725.MCP4725(i2c)
# Optionally you can specify a different addres if you override the A0 pin.
# amp = adafruit_max9744.MAX9744(i2c, address=0x63)

# There are a three ways to set the DAC output, you can use any of these:
dac.value = 65535  # Use the value property with a 16-bit number just like
# the AnalogOut class.  Note the MCP4725 is only a 12-bit
# DAC so quantization errors will occur.  The range of
# values is 0 (minimum/ground) to 65535 (maximum/Vout).

dac.raw_value = 4095  # Use the raw_value property to directly read and write
# the 12-bit DAC value.  The range of values is
# 0 (minimum/ground) to 4095 (maximum/Vout).

dac.normalized_value = 1.0  # Use the normalized_value property to set the
# output with a floating point value in the range
# 0 to 1.0 where 0 is minimum/ground and 1.0 is
# maximum/Vout.

# Main loop will go up and down through the range of DAC values forever.
while True:
    # Go up the 12-bit raw range.
    print("Going up 0-3.3V...")
    for i in range(4095):
        dac.raw_value = i
    # Go back down the 12-bit raw range.
    print("Going down 3.3-0V...")
    for i in range(4095, -1, -1):
        dac.raw_value = i

This guide was first published on Sep 05, 2012. It was last updated on Mar 27, 2024.

This page (Python & CircuitPython) was last updated on Mar 27, 2024.

Text editor powered by tinymce.