Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?


Failed to load latest commit information.
Latest commit message
Commit time
August 22, 2017 12:10
August 17, 2017 12:32
August 22, 2017 12:21
August 17, 2017 11:16
August 17, 2017 12:51
August 22, 2017 12:21


Python Library for driving MCP4922 DAC (Digital to Analog Converter) on Raspberry Pi 3 Model B+

About MCP 4922

The MCP4922 is a Digital to Analog Converter that has 2 Channels and 12 bit resolution. The regular version has 14 pins and the datasheet is available here:


To install the library run the following commands on a Raspberry Pi or other Debian-based OS system:

Install Git

If you don't have it already:

sudo apt-get install git build-essential python-dev


To run this module you need RPi.GPIO and spidev:

sudo pip install RPi.GPIO
sudo pip install spidev

Clone the repository

git clone

Install with

cd Python-RPi-MCP4922
sudo python install


Getting started

Go to the example directory and execute the test script:

cd examples
sudo python

This script will initialize the MCP4922 and incrementally increase the voltage on both channels. Then it will decrease the voltage and loop forever.

If you want to manually set the channel and voltage, you can use the other test script:

sudo python

Use it in your own code


First import the required GPIO module in python:

import RPi.GPIO as GPIO

Set your pin mode to BCM:


and then import the MCP4922 module:

from MCP4922 import MCP4922

MCP4922 Class

The MCP4922 Class takes 3 arguments: spibus, spidevice and cs. To configure just simple use this code in python:

dac = MCP4922(spibus=0, spidevice=0, cs=8)

Select the bus for your MCP4922. Should be either 0 or 1. If no argument is given it will turn to the default of 0.

Have a closer look at the spidev module ( for more details.


Select the device connected to the bus, should be either 0 or 1. If no argument is given it will turn to the default of 1.

Also checkout the spidev documentation for more details.


Chip select pin. Enter the pin number (Broadcom or Physical pin) where the MCP4922 is connected to. If no argument is given it will turn to the default of 8 (Broadcom pin numbering) or 24 (physical pin numbering).

setVoltage(channel, value)

This method sets the desired output voltage at the requested channel. It is as easy as:

dac.setVoltage(0, 2048)

This will set the voltage output to 50% of the reference voltage.


The MCP4922 has two channels, you can select by giving the arguments 0 or 1. 0 is channel A, 1 is channel B.

Anything else will throw an error at you.


Interger value from 0 to 4095, where 4095 is 100% of the reference voltage.

If you insert a value beyond that reach it will be clamped to 4095 (or 0 if you give a negative value).

setVoltage_gain(channel, value)

The MCP4922 has the ability to output the double of the reference Voltage Reference Voltage is measured by the MCP4922 at pin 13 (VrefA) for Channel A and pin 11 (VrefB) for Channel B Note that the output voltage cannot exceed the supply voltage from pin 1 (VDD).


dac.setVoltage_gain(0, 2048)

setVoltage_buffered(channel, value)

Using the buffer feature of the MCP4922, refer to the datasheet for details.


dac.setVoltage_buffered(0, 2048)


Completely shutdown selected channel for power saving. Sets the output of selected channel to 0 and 500K Ohms. Read Datasheet (SHDN) for details.




Here is an example of hooking up your MCP4922 to your Raspberry Pi.

Of course no warranties for any damage.

MCP4922 Breadboard Example

And the schematic view:

MCP4922 Schematic Example

Also: Refer to the datasheet