micro:bit and MCP4725 DAC example

MCP4725 is a single channel, 12-bit, voltage output Digital-to-Analog Converter with integrated EEPROM and an I2C Compatible Serial Interface.

Features

12-Bit Resolution
On-Board Non-Volatile Memory (EEPROM)
±0.2 LSB DNL (typ)
External A0 Address Pin
Normal or Power-Down Mode
Fast Settling Time of 6µs (typ)
External Voltage Reference (VDD)
Rail-to-Rail Output
Low Power Consumption
Single-Supply Operation: 2.7V to 5.5V
I2CTM Interface:
Eight Available Addresses
Standard (100 kbps), Fast (400 kbps) andHigh Speed (3.4 Mbps) Modes
Small 6-lead SOT-23 Package
Extended Temperature Range: -40°C to +125°C

As this is a 12 bit DAC converter. What this means is that it will accept up to 4096 possible inputs to provide an analog output, where an output value of zero is zero and an output value of 4095 is full scale.
Full scale is determined by the reference voltage you supply to the VCC pin. Also you can see from above that the supply voltage can be anywhere from 2.7 volts to 5.5 volts. We will use 5v, or as close as what is supplied via the USB in. You may want to measure this voltage for accurate readings, I’ve seen this vary.

This means that to work out the value of the Least Significant Bit (LSB) is as follows:

1 LSB = VCC Voltage / 4096
Again the easiest way to interface this to an Arduino is to purchase a module, tehse are available from many sources, here is what my one looked at.

Layout

Fairly straightforward as its an I2C device, I connected the Vout to 0.


Code

The module has the ability to use different I2C addresses, so run the code from here first.  My one was the default 0x60 address

This example uses the Adafruit MCP4725 library

#include <Wire.h>
#include <Adafruit_MCP4725.h>
#define voltsIn PIN_A0
Adafruit_MCP4725 dac; // constructor
void setup(void) {
Serial.begin(9600);
dac.begin(0x60); // The I2C Address: Run the I2C Scanner if you're not sure
}
void loop(void) {
uint32_t dac_value;
int adcValueRead = 0;
float voltageRead = 0;
float dac_expected_output;
for (dac_value = 0; dac_value < 4096; dac_value = dac_value + 15)
{
dac_expected_output = (3.3/4096.0) * dac_value;
dac.setVoltage(dac_value, false);
delay(250);
adcValueRead = analogRead(voltsIn);
voltageRead = (adcValueRead * 3.3 )/ 1024.0;
Serial.print("DAC Value: ");
Serial.print(dac_value);
Serial.print("\tExpected Voltage: ");
Serial.print(dac_expected_output,3);
Serial.print("\tArduino ADC Value: ");
Serial.print(adcValueRead);
Serial.print("\tArduino Voltage: ");
Serial.println(voltageRead,3);
}
}
 

 

Links

MCP4725 I2C DAC Breakout Development Board 12Bit Resolution

1 COMMENT