Same sketch, different result

Using an MCP4725 with Arduino Uno and Arduino NANO V3 I get a precise sine wave @ 50 Hz 5Vpp
The same sketch loaded on Arduino NANO Every results in a frequency of 20 Hz.
To increase the frequency I should decrease the number of data in the array.
Il looks like the Every is slower than Uno and Nano V3, even if the cpu frequency is 20MHz (Every) instead of 16MHz (Uno and Nano v3),
Please note: the following sketch is a simplified program from the official MCP4725 Arduino Library (Version 2.0 - the latest). The modification I have done is just the number of data too get a precise 50Hz. With Uno and Nano v3 it works, with Every it does not work

Some suggestion?

This is the sketch:

#include <Wire.h>
#include <Adafruit_MCP4725.h>
int i = 0;
Adafruit_MCP4725 dac;

#define DAC_RESOLUTION    (7)

const PROGMEM uint16_t DACLookup_FullSine_7Bit[137] = {
  
4036, 4057, 4073, 4085, 4092, 4095, 4094, 4089, 4079, 4065,
4047, 4025, 3998, 3967, 3933, 3894, 3852, 3805, 3755, 3702,
3644, 3584, 3520, 3454, 3384, 3311, 3236, 3158, 3078, 2996,
2912, 2826, 2738, 2649, 2559, 2467, 2375, 2282, 2188, 2094,
2001, 1907, 1813, 1720, 1628, 1536, 1446, 1357, 1269, 1183,
1099, 1017,  937,  859,  784,  711,  641,  575,  511,  451,
 393,  340,  290,  243,  201,  162,  128,   97,   70,   48,
  30,   16,    6,    1,    0,    3,   10,   22,   38,   59,
  83,  112,  144,  181,  222, 266,   314,  366,  422,  480,
 542,  608,  676,  747,  821, 898,   976, 1058, 1141, 1226,
1313, 1401, 1491, 1582, 1674, 1767, 1860, 1954, 2048, 2141,
2235, 2328, 2421, 2513, 2604, 2694, 2782, 2869, 2954, 3037,
3119, 3197, 3274, 3348, 3419, 3487, 3553, 3615, 3673, 3729,
3781, 3829, 3873, 3914, 3951, 3983, 4012, 
};

void setup(void) {
  dac.begin(0x62);
}

void loop(void) {
    for (i = 0; i < 137; i++) {
      dac.setVoltage(pgm_read_word(&(DACLookup_FullSine_7Bit[i])), false);
    }
}

Your topic was MOVED to its current forum category as it is more suitable than the original

If you do not want to get rounding or interpolation errors, I think your main options are:

  • To rebuild the table.
  • To use the builtin sin() function instead of a table.

I see this in the Adafruit docs:

One thing thats a little annoying about the Arduino Wire library in this case is it is set for 100KHz transfer speed. In the MCP4725 library we update the speed to 400KHz by setting the TWBR

TWBR = 12; // 400 khz
You can speed this up a bit more, if you'd like, check the ATmega328 datasheet for how to calculate the TWBR register.

Since the Every uses a different processor, I'll guess that this TWBR adjustment isn't having the desired effect on Wire speeds.

Can't understand.
I have already increased the I2C frequency, but surpisingly it does not affect the loop speed.
At present the only chance to get 50Hz seems the reduction of the number of data, but this will of course reduce the accuracy of the sinewave.
Strange, really strange.

It probably depends where you did it. Perhaps your change is overwritten by Adafruit's.

The observed frequencies are not in the 400,000 vs. 100,000 kHz ratio. It does not look like an I2C frequency issue.

Changed the speed inside the Adafruit library variable. It is originally 100000, increased to 400000 and then 1500000.
No way...

I ran the code on an Uno, though I don't have a DAC. Fifty cycles with the DAC call took 489 mS. Fifty with No DAC call, just summing the numbers in progmem (to make sure it wasn't optimized away) took eight.

It seems likely then that the issue lies in the DAC library code although of course without the hardware, my test may be flawed. Perhaps the optimizer is doing a better job on the Uno.

I think the same....the problem should reside in the ADafruit library, not compatible with the Nano Every. But I can't be sure of this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.