Teensy 4.1 with MCP4725 DAC

Fenichel

Well-known member
I am developing an app that will make limited use of a MCP4725 DAC. I have used the MCP4725 before, driving it with Teensy 3.5s.

I can't get it to work with T4.1; there seems to be an interaction between the i2c apparatus and the Serial channel. This test program
Code:
// MCP4725 test program
// output is monotonically increasing [0, 1023]
// domain is [0, 4095]

// *************** Note *******************************
// Teensy    Line 1      Line 2           Serial Monitor
//  3.5     don't care  present    as expected, with MCP7425 output
//  4.1       absent     present    nothing, not even "hello"
//  4.1       present    present    "hello", then nothing
//  4.1       present    absent     as expected (no MCP4725 output)

#include <Arduino.h>

// #define Teensy35
#define Teensy41
#define Wire4725 Wire

#if defined(Teensy35) 
  #include <i2c_t3.h>
#elif defined(Teensy41)
  #include <i2c_driver.h>
  #include <i2c_driver_wire.h>
#endif

const int AnalogReadPin = A16;
const uint8_t MCP4725Address = 0x63;

class tMCP4725
  { private:
      uint8_t _i2cAddr;
    public:
      void Setup(uint8_t Addr);
      void SetVoltage(uint16_t DesiredOutput, bool WriteEEPROM);
      void SetVoltage(uint16_t DesiredOutput) { SetVoltage(DesiredOutput, false); }
  }; // tMCP4725

void tMCP4725::Setup(uint8_t Addr)
  { _i2cAddr = Addr;
    Wire4725.begin(Addr);
  } // tMCP4725::Setup

void tMCP4725::SetVoltage(uint16_t DesiredOutput, bool WriteEEPROM )
  { // @param[in]  DesiredOutput
    //             The 12-bit value representing the relationship between
    //             the DAC's input voltage and its output voltage.
    // @param[in]  WriteEEPROM
    //             If this value is true, 'output' will also be written
    //             to the MCP4725's internal non-volatile memory, meaning
    //             that the DAC will retain the current voltage output
    //             after power-down or reset.

    const int cmdWriteDAC          = 0x40;
    const int cmdWriteDACandEEPROM = 0x60;

    Wire4725.beginTransmission(_i2cAddr);
    if (WriteEEPROM)
      { Wire4725.write(cmdWriteDAC); }
    else
      { Wire4725.write(cmdWriteDACandEEPROM); }

    Wire4725.write(DesiredOutput / 16);        // Upper data bits (D11..D4)
    Wire4725.write((DesiredOutput % 16) << 4); // Lower data bits (D3..D0.x.x.x.x)
    Wire4725.endTransmission();
  } // tMCP4725::SetVoltage
tMCP4725 MCP4725;

void setup()
  { Serial.begin(9600);
    delay(2000);
    Serial.println("hello");
    MCP4725.Setup(MCP4725Address);
    delay(10000);                             // Line 1; see notes above
  } // setup

void loop()
  { int Reading;
    for (int I = -1; I < 4096; I = I + 128)
      { MCP4725.SetVoltage(max(0,I));         // Line 2; see notes above
        delay(100);
        Reading = analogRead(AnalogReadPin);
        Serial.printf("set %d, read %d\n", I, Reading);
       }
    delay(10000);
  } // loop
compiles and runs normally in a T3.5, but in a T4.1 it behaves as described in the note comments. The problem is independent of which I2C port is used.
 
I2C problem solved; I failed again to RTFM

Here's what I missed:
Code:
// MCP4725 test program

...
void tMCP4725::Setup(uint8_t Addr)
  { _i2cAddr = Addr;
    // Wire4725.begin(Addr);     // ********* this is what I had
    Wire4725.begin();            // ********* this is what it should be
  } // tMCP4725::Setup
...
 
Does your MCP4725 DAC have pullup resistors on SCL and SDA?

I have an Adafruit DAC that works with T3* and T4*. The breakout has 10K pullups.

(note: your if (WriteEEPROM) ... logic is reversed but probably not related to your problem)
 
thanks for your interest

Does your MCP4725 DAC have pullup resistors on SCL and SDA?
Yes, I have pullups, and in fact it is all working after I made the correction shown in post #2.

I have an Adafruit
(note: your if (WriteEEPROM) ... logic is reversed but probably not related to your problem)

Thanks; that turns out to have been (as you noted) unrelated to my original problem, but I'm glad you spotted it.
 
Back
Top