Cannot control MCP4725 on matlab (through the arduino)

6 views (last 30 days)
I am having trouble controlling a MCP4725 DAC with Matlab (which is controlling the arduino hardware)
I have firstly made sure that the DAC does work by testing the triangle wave example from adafruit and reading the data off the analog pin.
However, when I use Matlab I find that sending a command to the DAC does not result in the desired output read on the analog pin. If I send command 64(*) then 4095, the output voltage remains at 0V. I am not sure how I am meant to send the commands to the DAC to result in the output I require. I have attached some of the outputs I received (I did try to adjust the size of the integer being sent to possibly improve it) Does anyone have advice on how the DAC control is meant to occur?
(I first cleared the arduino by sending it an empty sketch)
>>a = arduino('COM12','Mega2560','Libraries',{'rotaryEncoder','I2C'});
>> addrs = scanI2CBus(a)
addrs =
cell
'0x60'
>>dac=i2cdev(a,'0x60');
>> readVoltage(a,'A3')
ans =
0.0244
>> write(dac,64,'uint16'); %command = 64 for write command for DAC input
>> write(dac,4095,'uint16');
>> readVoltage(a,'A3')
ans =
0
>> write(dac,64,'uint8');
>> write(dac,0,'uint8');
>> readVoltage(a,'A3')
ans =
0.0098
>> write(dac,64,'uint8');
>> write(dac,255,'uint8');
>> readVoltage(a,'A3')
ans =
0.0098
>> write(dac,64,'uint8');
>> write(dac,4095,'uint16');
>> readVoltage(a,'A3')
ans =
0.0098
(*) I got 64 from the binary '01000000' (bin2dec('01000000') = 64) which would be the command required (figure 6.2 from the data sheet below) https://cdn-shop.adafruit.com/datasheets/mcp4725.pdf
  1 Comment
mcan06
mcan06 on 11 Dec 2018
Can u solve this problem? Actually when u write different integer values, u read different voltage values from Analog Input.

Sign in to comment.

Accepted Answer

Janelle Theron
Janelle Theron on 13 Apr 2017
Ok I actually figured it out but will leave the question here to help anyone who had the same confusion:
MATLAB Support Package for Arduino Hardware supports only 7-bit addressing. So I don't think that adjusting the precision would have helped, I had to instead send 3 bytes to the dac in one go: dac=i2cdev(a,'0x60'); write(dac,[64 0 0], 'uint8') readVoltage(a,'A3')
  3 Comments
Stijn Haenen
Stijn Haenen on 1 Apr 2020
By giving the command 'write(dac,[64 x1 x2])' the voltage output can be set on 1024 (10-bit) different voltages in a range of 0-5V. This means steps of 0.0049V so you cannot get exactly 2.5V. In voltages.mat all the possible voltages are listed with the corresponding numbers for x1 and x2.
with this script the voltages can be set:
load('voltages.mat')
a=arduino();
if ~contains([a.Libraries{1:end}],'I2C')
error('No I2C connection possible')
end
adress=scanI2CBus(a);
dev=device(a,'I2CAddress',adress{1});
voltage=2.5;
[~,x]=min(abs(voltage-volta(:,1)));
write(dev,[64 volta(x,2) volta(x,3)],'uint8');
voltage_real = readVoltage(a,'A0');
Andric Thamsir
Andric Thamsir on 27 May 2021
This is so helpful! Thanks for sharing. I spent days trying to figure out the head and tail of how to connect the DAC channels through library addons (for MCP4728) . This makes alot more sense, can configure functions and commands directly using the bits and bytes break down as per manual. With library addons, I wonder if the reason I couldn't get it to work was because it requires creation of addon libaries for all relevant adafruit specific class files, e.g. I2C_Device, BusOI, etc.
Now I am trying to get INA260 to work using this method. Has anyone tried?
Thanks again!

Sign in to comment.

More Answers (1)

Zoey Bigelow
Zoey Bigelow on 11 May 2022
"I have firstly made sure that the DAC does work by testing the triangle wave example from adafruit and reading the data off the analog pin."
This is exactly what I am trying to do! Does anyone know where I can find that example??

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!