MCP4725 - DAC working with Pico

Posted on
  • Hi just to say got a MCP4725 - DAC working (thanks to some Python code form johnwhittington) on the Pico. I'm using Sparkfun module but Ada Fruit do one too.
    https://www.sparkfun.com/products/12918?_ga=1.22340158.462334063.1427963564

    var voltage = 4095;
    var dac = [2];
    voltage = (voltage > 4095) ? 4095 : voltage;
    
    // MCP4725 expects a 12bit data stream in two bytes (2nd & 3rd of transmission)
    dac[0] = (voltage >> 8) & 0xFF;    // [0 0 0 0 D12 D11 D10 D9 D8] (first bits are modes for our use 0 is fine)
    dac[1] =  voltage & 0xFF;                // [D7 D6 D5 D4 D3 D2 D1 D0]
    
    I2C1.setup({scl:b6, sda:b7, bitrate:100000});
    I2C1.writeTo(0x60,[dac[0],dac[1]]);
    
  • Nice - and that's all that's needed? No setup code or anything?

    You could actually simplify the code a bit further, because you can take advantage of the fact that I2C.writeTo takes bytes (so effectively does &0xFF for you).

    var voltage = 4095;
    voltage = E.clip(voltage,0,4095);
    // MCP4725 expects a 12bit data stream in two bytes (right aligned)
    I2C1.setup({scl:b6, sda:b7, bitrate:100000});
    I2C1.writeTo(0x60,[voltage >> 8,voltage]);
    

    It might be worth turning this into a module? It'd be tiny, but would at least make using the MCP4725 really easy for anyone who didn't want to worry about I2C commands.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

MCP4725 - DAC working with Pico

Posted by Avatar for LawrenceGrif @LawrenceGrif

Actions