![]() |
|
1-wire DS18B20 change resolution - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Hardware (https://forum.logicmachine.net/forumdisplay.php?fid=12) +--- Thread: 1-wire DS18B20 change resolution (/showthread.php?tid=301) |
1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 Is it possible to change the resolution from a 1-wire DS18B20 to 12 bits with a LogicMachine, like in this topic: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/ And i like to change the send delta to a lower value, now the range starts with 0.2 RE: 1-wire DS18B20 change resolution - admin - 17.05.2016 You can read 12-bit result via a script but there's no big point in doing that. Even though theoretical resolution is better the resulting value won't be stable due to how the measurement works. The better approach is to do over-sampling by averaging the resulting values over a specific period of time. RE: 1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 (17.05.2016, 08:02)admin Wrote: You can read 12-bit result via a script but there's no big point in doing that. Even though theoretical resolution is better the resulting value won't be stable due to how the measurement works. The better approach is to do over-sampling by averaging the resulting values over a specific period of time. Oke, but i like to have my values with 0,1 precision. With a arduino and the same type sensor i can do this (theoretical ), but connected to the LM4 it gives strange results: see attached picture. RE: 1-wire DS18B20 change resolution - buuuudzik - 17.05.2016 (17.05.2016, 08:10)gjniewenhuijse Wrote:(17.05.2016, 08:02)admin Wrote: You can read 12-bit result via a script but there's no big point in doing that. Even though theoretical resolution is better the resulting value won't be stable due to how the measurement works. The better approach is to do over-sampling by averaging the resulting values over a specific period of time. You can add an event-based script which repeat this value with custom precision or you can use custom values for visualisation if this is only for visualisation purposes. RE: 1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 Oke, i can do a reading and custom precision with: Code: require('ow')
value = ow.readfile('SENSORID', 'temperature', true) -- third argument will force a new conversion
log( round(value,1) )I know its theoretical, but can i change the precision by lm4 or do i need to connect them to a arduino and execute the following code: Code: ds.search(addr); // address on 1wire bus
if (addr[0] == DS18B20) // check we are really using a DS18B20
{
ds.reset(); // rest 1-Wire
ds.select(addr); // select DS18B20
ds.write(0x4E); // write on scratchPad
ds.write(0x00); // User byte 0 - Unused
ds.write(0x00); // User byte 1 - Unused
ds.write(0x7F); // set up en 12 bits (0x7F)
ds.reset(); // reset 1-Wire
ds.select(addr); // select DS18B20
ds.write(0x48); // copy scratchpad to EEPROM, next reboot it has the new precision
delay(15); // wait for end of write
}RE: 1-wire DS18B20 change resolution - admin - 17.05.2016 You don't need to change the precision, reading "temperature" property will read the 12-bit value, by default LM uses 10-bit value ("temperature10" property). RE: 1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 (17.05.2016, 08:45)admin Wrote: You don't need to change the precision, reading "temperature" property will read the 12-bit value, by default LM uses 10-bit value ("temperature10" property). perfect.. thanks RE: 1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 something like this? resident script every 60 seconds: Code: -- better to save the last 5 readings and avg them and then write to bus
require('ow')
require('user.nit_measurements')
-- second argument temperature10 (10-bit value)
-- third argument will force a new conversion
readValue = ow.readfile('28.690E0A070000', 'temperature', true)
-- measure last 5 readings
mValue = measValue (5, readValue)
log (round(mValue,1))user_library nit_measurement: Code: avgMax = 5
function measValue (iMax,iValue)
-- init value array
if not measCount then
if iMax then
avgMax = iMax
end
measCount = 0
measValues = {}
for i=1,avgMax do
measValues[i] = iValue
end
end
measCount = measCount + 1
-- set values
for i=avgMax,2,-1 do
measValues[i] = measValues[i-1]
end
measValues[1] = iValue
--log(measValues)
-- reset counter
if (measCount == avgMax) then
measCount = 0
end
rValue = 0
for i=1,avgMax do
rValue = rValue + measValues[i]
end
rValue = rValue / avgMax
return rValue
endRE: 1-wire DS18B20 change resolution - admin - 17.05.2016 Here's a function you can use to calculate the average, this version will keep last count values and will returnt the average for every insert: Code: function avg(store, count, value)
local sum = 0
-- add new value as first (newest) store element
table.insert(store, 1, value)
-- remove old elements, until store size is correct
while #store > count do
table.remove(store)
end
-- calucate value sum
for _, value in ipairs(store) do
sum = sum + value
end
-- get average value
return sum / #store
endAnd here's how you can use it: Code: if not store then
require('ow')
-- load avg function here
store = {}
end
value = ow.readfile('28.690E0A070000', 'temperature', true)
avgvalue = avg(store, 5, value)You can add an extra counter in your resident script if you want to send a new value for each X samples. RE: 1-wire DS18B20 change resolution - gjniewenhuijse - 17.05.2016 great! |