This application note describes the use of two converter S0 inputs for voltage change detection. Although the converter has four S0 inputs, only two are available in this application, since the other two are used as pull-ups for previously mentioned ones. The converter used in this application is ACR-CV-101L-I4-D.
This setup can then be used to detect voltage in the circuit (up to max. 30V; with 33kΩ resistor in series up to max. 36V), for example as a fault (when fault indicator turns on) or device start monitoring. It can also be used as monitoring of switching devices with low switch-off resistance (when resistance is other than “infinite”, for instance, 30kΩ …).
The figure below shows the connection of S0 inputs.
S3 input is used as a pull-up for S1 input
S4 input is used as a pull-up for S2 input
function onChange(src)
orig_state = 0
current_state = 0
-- Store pin states at time when this event occurs
pos = 1
for i=1,2 do
orig_state = orig_state + api.DIOreadPin(i) * pos
pos = pos * 2
end
print("Current logic state is ".. tostring(orig_state))
if src == 1 then -- switch 1
print("Switch 1 changed state...")
buf = string.char(orig_state)
api.loraSend(1, 10000, buf, 1)
else
if src == 2 then -- switch 2
print("Switch 2 changed state...")
buf = string.char(orig_state)
api.loraSend(1, 10000, buf, 1)
end
end
end
function scanCompare()
oldSwitch1 = api.getVar(1)
oldSwitch2 = api.getVar(2)
api.DIOwritePin(3,1) -- enable pull up
api.delayms(1)
thisSwitch1 = api.DIOreadPin(1)
api.DIOwritePin(3,-1) -- set high impedance
api.DIOwritePin(4,1) -- enable pull up
api.delayms(1)
thisSwitch2 = api.DIOreadPin(2)
api.DIOwritePin(4,-1) -- set high impedance
if oldSwitch1 ~= thisSwitch1 then
print("Switch 1: " .. tostring(thisSwitch1))
api.setVar(1, thisSwitch1)
onChange(1)
end
if oldSwitch2 ~= thisSwitch2 then
print("Switch 2: " .. tostring(thisSwitch2))
api.setVar(2, thisSwitch2)
onChange(2)
end
end
function onWake ()
scanCompare()
cntr = api.getVar(6)
comp = api.getVar(7)
print("cntr/comp " .. tostring(cntr) .. " / " .. tostring(comp))
if cntr >= comp then
orig_state = 0
-- Store pin states at time when this event occurs
pos = 1
for i=1,2 do
orig_state = orig_state + api.DIOreadPin(i) * pos
pos = pos * 2
end
print("Current logic state is ".. tostring(orig_state))
buf = string.char(orig_state)
api.loraSend(1,10000,buf, 1)
print("Wake in 12hours")
api.setVar(6, 0)
api.setVar(7, 4320)
else
cntr = cntr + 1
api.setVar(6, cntr)
end
api.wakeUpIn(0,0,0,10)
end
function onInputChanged()
-- unused
end
function onThreshold()
-- unused
end
function onStartup()
print("Starting up, 10s synchronous scan of input 1 and 2 with 3 and 4 used as pullup, respectively")
api.DIOwaitForEvent(1,0)
api.DIOwaitForEvent(2,0)
api.DIOwaitForEvent(3,0)
api.DIOwaitForEvent(4,0)
api.setVar(6, 0) -- variable used for counting
api.setVar(7, 4320) -- 12hrs elapsed == 12*60*6 = 4320 (10s scanning)
api.setVar(1,1) -- auxiliary variable for switch 1 (default log 1)
api.setVar(2,1) -- auxiliary variable for switch 2 (default log 1)
end