how to generate 12bits resolution voltage from DAC pin of arduino due board in simulink

Hi, I am using Arduino due board DAC pin to generate voltage. I want resolution to be 12bits. But when i use in simulink, it generates 8bit resolution voltage from 0.56v to 2.76v. Please let me know how to generate the 12bit resolution voltage from DAC pins in simulink using Arduino due board. Block says it accepts 0-4095 values, but it does not react accordingly. It does Not react for each increament, it reacts only for multiple of 16 (example: 0, 16, 32, 48, 64 ........etc..).
Can you please let me know how to solve this problem.
Can we do this in PYTHON? :)
is there any solution using creating s-block??
Thanks

Answers (1)

Hi Aakash,
As per my understanding, you would like to know how to generate 12-bit resolution voltage from DAC pin of Arduino Due boards in Simulink.
To change the output total number of bits to 12, Please follow the steps mentioned below:
  • Open the Segmented DAC block by double clicking it.
  • Under Segment Settings, click on the ‘New Segment’ option. This allows you to add a new segment.
  • Change the number of bits to 4 in the newly added Segment.
  • Click on Apply’.
  • Then you can see that the total number of bits are changed to 12. Now you can generate the 12-bit resolution voltage from DAC pins in Simulink using Arduino Due board.
For further reference, please check this link to know about Design and Evaluate Segmented DAC:
I hope this resolves the issue you are facing.

12 Comments

Thank you for your reply.
The steps you mentioned i have applied. but still there is an error in my simulink block diagram. error says:
Cannot compile diagram 'segmentedDACrun' because it contains variable sample times or S-functions and is using a fixed-step solver.
Component:Simulink | Category:Block diagram error.
Let me know how to resolve this proble?
I have attached my block diagram file here. Please help!
Hi Aakash,
Simulink supports variable sample times for variable-step solvers only. You can read more about this in the page:
Could you try to use controllable sample times instead?
Thanks again for reply!
Could you please let me know any other way of having 12bits resolution from DAC pin?
Or do I have to shift to other programming languages like Python?
Thank you in advance
Still did not get the proper solution
Error says
Suggested Actions
You may configure the solver options for a fixed-step solver with an appropriate integration algorithmOpen
Select a target that supports a variable-step solver, such as rsim.tlcOpen
You can use 'Embedded Coder Quick Start' to help you generate code
can you simply share a simple DAC 12 bit voltage generation simulink file here??
there is no such thing like this;
  • DAC block (under Simulink-PS Converter -> Converters)
I don't see this block. Is it a normal DAC block in simulink support package for arduino??
Yes, the normal DAC block in Simulink can be used with the Simulink Support Package for Arduino.
The Simulink Support Package for Arduino provides Simulink blocks for reading and writing digital and analog signals to and from Arduino boards.
what you have suggested in the last message is exactly I do to generate the voltage, but thats where the problem is. It does not generate the voltage of 12bits resolution. It shows 4096 values, but does not react accordingly. it has 256 steps only in 4096 range. each step has 16 values.
Also, the block does not have the option of Bits and output range when I open it. Please check.
There is No option in block to change DAC block parameters.
Thats what the whole point of asking that question here.
Did you actually check?
Hi Aakash,
The current implementation of DAC block in Simulink support package has a limited resolution of 8 bits which is why you are seeing only 256 steps. However Arduino due supports 12 bits resolution. We can consider this as an enhancement and update our blocks to support 12 bits resolution. However if you are blocked by this, please let us know, we can share a patch with you that can enable 12 bit resolution.
Thanks,
Arun
Hello Arun,
Yes. you understood the exact problem. Thank you.
Please let me know how to resolve the problem?
Thank you
Hi Aakash,
Please modify the MW_analogWriteDAC.cpp file by running the following command:
edit(fullfile(fileparts(codertarget.arduinobase.internal.getSpPkgRootDir),'arduinobase','src','MW_analogWriteDAC.cpp'))
This will opoen the MW_analogWriteDAC.cpp file in the MATLAB editor.
Replace the existing MW_DACWrite function with the following function:
extern "C" void MW_DACWrite(uint8_T channelFlag, uint32_T value)
{
#if defined(_ROTH_DUE_)
static int init_12bitDac = 0;
if(init_12bitDac==0){
analogWriteResolution(12); /* set the resolution of the Arduino analogWrite() function to 12 bits */
init_12bitDac = 1;
}
analogWrite(((channelFlag == 0)? DAC0 : DAC1), value);
#elif defined(_ROTH_MKR1000_) || defined(_ROTH_NANO33_IOT_)
/* dividing the data by 4 to adjust for upscaling by 4 in Arduino analogWrite API */
analogWrite(DAC0, (value>>2));
#elif defined(_ROTH_MKRZERO_) || defined(_ROTH_MKRWIFI1010_)
/* dividing the data by 4 to adjust for upscaling by 4 in Arduino analogWrite API */
analogWrite(15u, (value>>2));
#elif defined(_ROTH_ESP32_)
dacWrite(((channelFlag == 1)? DAC1 : DAC2), value);
#endif
}
This will change the resolution of DAC to 12 bit on Arduino due board. Please note that this change will only work for Arduino due. Other arduino boards are not affected by this change.
Hope this helps!
Thanks,
Arun

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 20 Mar 2023

Edited:

on 14 Apr 2023

Community Treasure Hunt

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

Start Hunting!