Wednesday, April 20, 2016

MPDMv4 - AC MAINS Dimmer - software example

UPDATE !! Fixed broken Tindie Link from below, now should be OK UPDATE !!


--------------------------------------------------- DISCLAIMER --------------------------------------------------
WARNING!! You will play with LIVE MAINS!! Deadly zone!! 
      If you don't have any experience and are not qualified for working with MAINS power I will not encourage you to play arround!. The author take no responsibility for any injury or death resulting, directly or indirectly, from your inability to appreciate the hazards of household mains voltages.
   The circuit diagrams are as accurately as possible, but are offered with no guarantees whatsoever. 
    There is no guarantee that this design meets any Rules which may be in force in your country so please check before your local rules/regulations.
----------------------------------------------------------------------------------------------------------------------------
 
                                                             Creative Commons License

MPDMv4 by ESP8266-Projects.com is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.  

 ---------------------------------------------------------------------------------------------------------------------------

    For any new orders/requests please feel free to use as usual: tech at esp8266-projects.com. 
   
    MPDMv4 Boards are also available on Tindie: AC MAINS Dimmer - MPDMv4









As been a Voltage controled AC MAINS Dimmer you can control it with:
  • PWM signal
  • DAC output Voltage
  • or if you don't want any kind of MCU involved, just user a 10k Potentiometer in a voltage divider as part of a simple VCNT input circuit!

In this example we will use a MCP4728 4 channels/12 Bit DAC as a VCNT (voltage control) command source for our MPDMv4 Dimmer Board.



What we will need:


Software implementation

 1. MCP4728 DAC Driver

1.1 I2C Bus Initialisation
    init = function (self, sda, scl)
          self.id = 0
          i2c.setup(self.id, sda, scl, i2c.SLOW)
    end,

1.2 Set DAC Voltage output on the selected Channel

    dac = function(self, ch_reg,voltage)
          volt=(voltage*4096)/vcal
          msb = bit.rshift(volt, 8)  
          lsb = volt-bit.lshift(msb,8)   
          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
          i2c.write(id,msb)
          i2c.write(id,lsb)
          i2c.stop(id)
    end,




1.3 Set DAC Register 

   set_reg_dac = function(self, reg)
          i2c.start(id)
          i2c.address(id, dac_addr ,i2c.TRANSMITTER)
          i2c.write(id,ch_reg)
          i2c.stop(id)
     end

2. MAIN Program
id=0
sda=2
scl=1
dac_addr=0x60
ch_reg=0x58      -- DAC CH A - Ext REF -
vcal=3.2325      -- external voltage reference = Vcc

require('mcp4728')             --call MCP4728 Driver module
mcp4728:init(sda, scl)       --Init I2C BUS
mcp4728:dac(ch_reg,2.8)  --Set VCMD Voltage (0-2.8V)

    2.1 Dimming stage example based on timer :


vcmd=0
tmr.alarm( 0, 1000, 1, function()
    print("Set VCMD value : "..vcmd)
    mcp4728:dac(ch_reg,vcmd)
    vcmd=vcmd+0.10
    if (vcmd>=2.81) then vcmd=0 end
end)

tmr.stop(0)   --stop the timer when you want to finnish cycling thru dimmer stages.

       











2 comments:

ChirpyAphid said...

Hi, I have tried using the code above however I am not having any luck with this. Are you able to provide a link to working examples of the code which can be downloaded and used? I am using a NodeMCU and would like to use the LUA code as above. I am assuming we need to save all the bits under step 1 in a single file and call it mcp4728.lua along with the code from step 2 saved as (in my case) main.lua however I get the following error: main.lua:9: attempt to index global 'mcp4728' (a nil value)
stack traceback:
pwm_main.lua:9: in main chunk
[C]: in function 'dofile'
stdin:1: in main chunk

ChirpyAphid said...

I meant to say the main app is called pwm_main.lua

Post a Comment