This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

1-wire DS18B20 change resolution
#1
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/...1112-bits/

And i like to change the send delta to a lower value, now the range starts with 0.2

Attached Files Thumbnail(s)
   
Reply
#2
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.
Reply
#3
(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.

Attached Files Thumbnail(s)
   
Reply
#4
(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.

Oke, but i like to have my values with 0,1 precision.
With a arduino and the same type sensor i can do this, but connected to the LM4 it gives strange results: see attached picture.

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.
Reply
#5
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
}
Reply
#6
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).
Reply
#7
(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
Reply
#8
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
end
Reply
#9
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
end

And 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.
Reply
#10
great!
Reply


Forum Jump: