Fig. 1 MCP4725 breakout board.
MCP4725 12-Bit DAC Interface to Raspberry Pi
by Lewis Loflin
YouTube video see MCP4725 12-Bit DAC Interface to Raspberry Pi.
The MCP4725 is a 12-Bit Digital-to-Analog Converter with EEPROM Memory. Here I'll connect the MCP4725 to Raspberry Pi and illustrate how to program the device. I'll be Debian based Raspbian Linux.
Fig. 1 MCP4725 breakout board available from a number of vendors. The boards usually have pull up resistors that need to be disconnected or use a level translator which is what I did. The Raspberry Pi I2C buss is 3.3V while I operated mine at 5V.
The features of the MCP4725:
The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered voltage output Digital-to-Analog Converter (DAC) with non-volatile memory (EEPROM).
Its on-board precision output amplifier allows it to achieve rail-to-rail analog output swing. The DAC input and configuration data can be programmed to the non-volatile memory (EEPROM) by the user using I2C interface command.
The non-volatile memory feature enables the DAC device to hold the DAC input code during power-off time, and the DAC output is available immediately after power-up. This feature is very useful when the DAC device is used as a supporting device for other devices in the network.
The device includes a Power-On-Reset (POR) circuit to ensure reliable power-up and an on-board charge pump for the EEPROM programming voltage. The DAC reference is driven from VDD directly. In power-down mode, the output amplifier can be configured to present a low, medium, or high resistance output load.
Fig 2 MCP4725 internal block diagram.
The MCP4725 DAC is a 12-bit device with values from 0-4095, with 4095 outputting a voltage near Vcc. First we write a control byte with the following specifications:
(Refer to page 18-19 spec sheet), uint8_t control_byte = 0b01000000; bits 7-5 are 010 write DAC only, 011 write DAC and EEPROM. We can write the 12-bit value to the DAC only or the DAC and the EEPROM. The values stored in the EEPROM sets the power up output voltage. This is byte 0.
If say 2048 is stored in the EEPROM the voltage output when power up is 1/2 Vcc.
The uint8_t is an unsigned 8-bit integer value that's defined across multiple versions of C.
In the example here I write to the DAC only because there's a delay writing to the EEPROM, in addition so many write cycles will shorten the life of the EEPROM.
Bits 4-3 are unused while bits 2-1 setup power down (PD) where a selected resistor value is switched from the output to ground. I set this value to open. Bit 0 is also unused.
The next two bytes are also uint8_t variables. The uint16_t val is an unsigned 16-bit integer value where one enters a value from 0-4095. The variable val is shifted four places right and loaded into byte 1 as the 8 MSB of val.
Then val is shifted four places left and loaded into byte 2 where bits 7-4 are the LSB if val while bits 3-0 are unused.
Then all three bytes are sent over I2C to the MCP4725 that outputs a voltage based on the value of val. After the three bytes are written the program exits.
Full program mcp4725.txt.
- Quick navigation main page:
- Basic Electronics Learning and Projects
- Arduino Microcontroller Projects
- Raspberry Pi and Linux
- Connecting a PC Printer Port to Electronics with Python
- Microchip PIC 18F2550
- PICAXE Microcontroller
- Software and setup:
- Raspberry Pi Openbox Desktop 2019
- Raspberry Pi USB Audio Connection 2019
- Connecting DS1307 RTC to Raspberry Pi
- Setting up Openbox for Raspberry Pi
- WiringPi and Pulse-Width-Modulation with Raspberry Pi
- WiringPi for Raspberry Pi and MAX6675 thermal-couple sensor
- WiringPi Blink an LED Demo
- Interface I2C LCD to Raspberry Pi in C
- ADS1115 4-Channel ADC Uses I2C with Raspberry Pi
- MCP4725 12-Bit DAC Interface to Raspberry Pi
- Raspberry Pi with PCF8591 Analog To Digital Control in C
- Raspberry Pi PCF8591 AD-DA Sensor Python Interface
This is a collection of programs and hardware hacks related to mainly Raspberry Pi and Debian Linux.
- Main Light Fast Linux Desktops with Openbox, JWM
- Test Reuse Surplus PC Power Supplies
- Add WBAR Launch Dock to Raspberry Pi
- Add MPG123 Terminal Music Player to Raspberry Pi, Linux
- Basics of Alsamixer Audio Control for Linux
- Add Solid State Hard Drive to Raspberry Pi
- Beep a PC Speaker Add Beeper to Raspberry Pi
- Using FEH Wallpaper Setter Under Linux
- Scrot Lite Weight Screen Shot Software for Linux
- Using Light Weight Beaver Text Editor
- Install Viewnior Image Viewer for Linux
- Zmixer ALSA Sound Control Tutorial
- Tutorial Xinitrc Desktop Manager Control for Linux
- Setup Raspberry Pi Using JWM Window Manager