MCP4725 12-Bit DAC Tutorial Created by lady ada Last updated on 2018-03-05 10:51:16 PM UTC
Guide Contents Guide Contents Overview Wiring Arduino Code 2 3 4 7 Using the library 7 Increasing the speed CircuitPython Code Usage Download Files Schematic & Fabrication Print © Adafruit Industries https://learn.adafruit.
Overview 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 will have it. Great for audio / analog projects, such as when you can't use PWM but need a sine wave or adjustable bias point.
Wiring Wiring up the MCP4725 breakout PCB is super easy. To start, we'll attach the breakout headers so we can plug it into a breadboard. Break off a strip of 6-pins of 0.1" male header and stick the LONG pins down into a breadboard Break off a strip of 6-pins of 0.1" male header and stick the LONG pins down into a breadboard Place the breakout board on top so the short ends of the header stick up through the pads © Adafruit Industries https://learn.adafruit.
Solder each pin using a soldering iron and solder, to make solid connection on each pin. This part is not optional! You cannot 'press fit' the header on, it must be attached permanently Now that the header is attached, we can wire it up. We'll demonstrate using an Arduino. FIrst, connect VDD (power) to a 3-5V power supply, and GND to ground. The DAC uses I2C, a two-pin interface that can have up to 127 unique sensors attached (each must have a different ADDRESS). © Adafruit Industries https://learn.
SDA to I2C Data (on the Uno, this is A4 on the Mega it is 20 and on the Leonardo digital 2) SCL to I2C Clock(on the Uno, this is A5 on the Mega it is 21 and on the Leonardo digital 3) There's two other pins remaining. A0 allow you to change the I2C address. By default (nothing attached to A0) the address is hex 0x62. If A0 is connected to VDD the address is 0x63. This lets you have two DAC boards connected to the same SDA/SCL I2C bus pins.
Arduino Code Next up, download the Adafruit MCP4725 library. This library does all of the interfacing, so you can just "set and forget" the DAC output. It also has some examples to get you started The library is available on GitHub. You can download it by clicking the button below. Download Adafruit_MCP4725 Library https://adafru.it/cDA Rename the uncompressed folder Adafruit_MCP4725. Check that the Adafruit_MCP4725 folder contains Adafruit_MCP4725.cpp and Adafruit_MCP4725.
First, be sure to call begin(addr) where addr is the i2c address (default is 0x62, if A0 is connected to VCC its 0x63). Then call setVoltage(value, storeflag) to set the DAC output. value should range from 0 to 0x0FFF. storeflag indicates to the DAC whether it should store the value in EEPROM so that next time it starts, it'll have that same value output.
CircuitPython Code It's easy to use the MCP4725 digital to analog converter with CircuitPython and the Adafruit CircuitPython MCP4725 module. This module allows you to easily write Python code that controls the output voltage from the DAC. First wire up a MCP4725 to your board exactly as shown on the previous pages for Arduino using an I2C connection.
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.
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.
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 main.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. Remember to change to use bitbangio if necessary for your board! # Simple demo of setting the DAC value up and down through its entire range # of values.
Download Files For more details about the chip, please check out the MCP4725 datasheet MCP4725 Arduino Library is on GitHub Fritzing object in the Adafruit Fritzing library EagleCAD PCB files on GitHub Schematic & Fabrication Print © Adafruit Industries https://learn.adafruit.
© Adafruit Industries Last Updated: 2018-03-05 10:51:15 PM UTC Page 14 of 14