Saturday, March 7, 2015

Part 2 - MCP4726 - 12 Bit I2C DAC Driver for ESP8266 CBDB Board


   Now, with a nice and powerful 12 bit DAC working on our ESP8266 CBDB Board ( see Part 1 ) let's test it to the limit a little bit. As you will see from the Oscilloscope and DMM images below, MCP4726 DAC Output it's nice, smooth and stable. Not bad at all for a 1$ range DAC!

 First step: DAC Output voltage programmed at 0.5V


Oscilloscope - 0.5V DAC Output

DMM - 0.5V DAC Output

What do you think about generating some standard waveforms with ESP8266 CBDB Board ? :)

1. Square Wave

function squareWave(nrit)
 local t=0
 while t<nrit do
       mcp4726:write_dac(2.5)
       tmr.delay(500000)
       tmr.wdclr()
       mcp4726:write_dac(0)
       tmr.delay(500000)
       tmr.wdclr()
    t=t+1
 end
end


Oscilloscope - Square Wave DAC Output
DMM - Square Wave DAC Output


2. Sawtooth Wave

function sawtoothWave(nrit)
 local t=0
 while t<nrit do
    for i=0,3,0.01 do
       mcp4726:write_dac(i)
       --tmr.delay(100)
       tmr.wdclr()
    end
    t=t+1
 end
 mcp4726:write_dac(0)
end


Oscilloscope - Sawtooth Wave DAC Output


DMM - Sawtooth Wave DAC Output

3. Triagle Wave

function triangleWave(nrit)
 local t=0
 while t<nrit do
    for i=0,3,0.01 do
       mcp4726:write_dac(i)
       --tmr.delay(100)
       tmr.wdclr()
    end
    for i=3,0,-0.01 do
       mcp4726:write_dac(i)
       --tmr.delay(100)
       tmr.wdclr()
    end
    t=t+1
 end
end


Oscilloscope - Triangle Wave DAC Output
DMM - Triangle Wave DAC Output

4. SIN Wave

   For this one we will use a predefined table with calculated SIN points values:

s1={1.27,1.30,1.33,1.36,1.39,1.43,1.46,1.49,1.52,1.55,1.58,1.61,1.64,1.67,1.70,1.73,
  1.76,1.79,1.82,1.84,1.87,1.90,1.93,1.95,1.98,2.00,2.03,2.05,2.08,2.10,2.13,2.15,
  2.17,2.19,2.21,2.24,2.26,2.28,2.29,2.31,2.33,2.35,2.36,2.38,2.39,2.41,2.42,2.44,
  2.45,2.46,2.47,2.48,2.49,2.50,2.51,2.51,2.52,2.53,2.53,2.54,2.54,2.54,2.54,2.54,
  2.55,2.54,2.54,2.54,2.54,2.54,2.53,2.53,2.52,2.51,2.51,2.50,2.49,2.48,2.47,2.46,
  2.45,2.44,2.42,2.41,2.39,2.38,2.36,2.35,2.33,2.31,2.29,2.28,2.26,2.24,2.21,2.19,
  2.17,2.15,2.13,2.10,2.08,2.05,2.03,2.00,1.98,1.95,1.93,1.90,1.87,1.84,1.82,1.79,
  1.76,1.73,1.70,1.67,1.64,1.61,1.58,1.55,1.52,1.49,1.46,1.43,1.39,1.36,1.33,1.30,
  1.27,1.24,1.21,1.18,1.15,1.11,1.08,1.05,1.02,0.99,0.96,0.93,0.90,0.87,0.84,0.81,
  0.78,0.75,0.72,0.70,0.67,0.64,0.61,0.59,0.56,0.54,0.51,0.49,0.46,0.44,0.41,0.39,
  0.37,0.35,0.33,0.30,0.28,0.26,0.25,0.23,0.21,0.19,0.18,0.16,0.15,0.13,0.12,0.10,
  0.09,0.08,0.07,0.06,0.05,0.04,0.03,0.03,0.02,0.01,0.01,0,0,0,0,0,
  0,0,0,0,0,0,0.01,0.01,0.02,0.03,0.03,0.04,0.05,0.06,0.07,0.08,
  0.09,0.10,0.12,0.13,0.15,0.16,0.18,0.19,0.21,0.23,0.25,0.26,0.28,0.30,0.33,0.35,
  0.37,0.39,0.41,0.44,0.46,0.49,0.51,0.54,0.56,0.59,0.61,0.64,0.67,0.70,0.72,0.75,
  0.78,0.81,0.84,0.87,0.90,0.93,0.96,0.99,1.02,1.05,1.08,1.11,1.15,1.18,1.21,1.24}


function sinWave(nrit)
  local sn=0
  while sn<nrit do
    for f = 1, #s1 do
      v = string.format("%f",s1[f])
      --print(v)
       mcp4726:write_dac(v)
       --tmr.delay(1)
      tmr.wdclr()
    end
  sn=sn+1
  end
end



Oscilloscope - SIN Wave DAC Output
DMM - SIN Wave DAC Output


For programming and uploading the software we will continue to use the LuaUploader as before.

To run the test, just save the code on ESP as 'func_gen.lua', restart ESP and run:

           require('mcp4726')
           sda=2 --GPIO4
           scl=1 --GPIO5
           mcp4726:init(sda, scl)
           mcp4726:write_dac(0.5)  -- 0.5 steady DAC output

          require('func_gen')
          squareWave(10)
          sawtoothWave(10)
          triangleWave(10)
          sinWave(10)


If you run in problems regarding available memory (should not if running after proper ESP Module restart) or want to optimise the memory usage just compile the program and driver before execution:

         node.compile('mcp4726.lua')
         node.compile('func_gen.lua')



Uploading and running the function generator test


And the "LIVE" action :




No comments:

Post a Comment