Merge pull request #269 from ftruzzi/master
New module: HDC1000 temperature and humidity sensor.
This commit is contained in:
commit
acf0df3aad
|
@ -0,0 +1,12 @@
|
|||
HDC1000 = require("HDC1000")
|
||||
|
||||
sda = 1
|
||||
scl = 2
|
||||
|
||||
HDC1000.init(sda, scl)
|
||||
HDC1000.config() -- default values are used if called with no arguments. prototype is config(address, resolution, heater)
|
||||
|
||||
print(string.format("Temperature: %.2f °C\nHumidity: %.2f %%", HDC1000.getTemp(), HDC1000.getHumi()))
|
||||
|
||||
HDC1000 = nil
|
||||
package.loaded["HDC1000"]=nil
|
|
@ -0,0 +1,99 @@
|
|||
-------------------------------------------------------
|
||||
|
||||
-- This library was written for the Texas Instruments
|
||||
-- HDC1000 temperature and humidity sensor.
|
||||
-- It should work for the HDC1008 too.
|
||||
-- Written by Francesco Truzzi (francesco@truzzi.me)
|
||||
-- Released under GNU GPL v2.0 license.
|
||||
|
||||
-------------------------------------------------------
|
||||
|
||||
-------------- NON-DEFAULT CONFIG VALUES --------------
|
||||
------------- config() optional arguments -------------
|
||||
|
||||
-- HDC1000_HEAT_OFF 0x00 (heater)
|
||||
-- HDC1000_TEMP_11BIT 0x40 (resolution)
|
||||
-- HDC1000_HUMI_11BIT 0x01 (resolution)
|
||||
-- HDC1000_HUMI_8BIT 0x20 (resolution)
|
||||
|
||||
-------------------------------------------------------
|
||||
|
||||
local modname = ...
|
||||
local M = {}
|
||||
_G[modname] = M
|
||||
|
||||
local id = 0
|
||||
local i2c = i2c
|
||||
local delay = 20000
|
||||
|
||||
local HDC1000_ADDR = 0x40
|
||||
|
||||
local HDC1000_TEMP = 0x00
|
||||
local HDC1000_HUMI = 0x01
|
||||
local HDC1000_CONFIG = 0x02
|
||||
|
||||
local HDC1000_HEAT_ON = 0x20
|
||||
local HDC1000_TEMP_HUMI_14BIT = 0x00
|
||||
|
||||
-- reads 16bits from the sensor
|
||||
local function read16()
|
||||
i2c.start(id)
|
||||
i2c.address(id, HDC1000_ADDR, i2c.RECEIVER)
|
||||
data_temp = i2c.read(0, 2)
|
||||
i2c.stop(id)
|
||||
data = bit.lshift(string.byte(data_temp, 1, 1), 8) + string.byte(data_temp, 2, 2)
|
||||
return data
|
||||
end
|
||||
|
||||
-- sets the register to read next
|
||||
local function setReadRegister(register)
|
||||
i2c.start(id)
|
||||
i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
|
||||
i2c.write(id, register)
|
||||
i2c.stop(id)
|
||||
end
|
||||
|
||||
-- writes the 2 configuration bytes
|
||||
local function writeConfig(config)
|
||||
i2c.start(id)
|
||||
i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
|
||||
i2c.write(id, HDC1000_CONFIG, config, 0x00)
|
||||
i2c.stop(id)
|
||||
end
|
||||
|
||||
-- returns true if battery voltage is < 2.7V, false otherwise
|
||||
function M.batteryDead()
|
||||
setReadRegister(HDC1000_CONFIG)
|
||||
return(bit.isset(read16(), 11))
|
||||
|
||||
end
|
||||
|
||||
-- initalize i2c
|
||||
function M.init(sda, scl)
|
||||
i2c.setup(id, sda, scl, i2c.SLOW)
|
||||
end
|
||||
|
||||
function M.config(addr, resolution, heater)
|
||||
-- default values are set if the function is called with no arguments
|
||||
HDC1000_ADDR = addr or HDC1000_ADDR
|
||||
resolution = resolution or HDC1000_TEMP_HUMI_14BIT
|
||||
heater = heater or HDC1000_HEAT_ON
|
||||
writeConfig(bit.bor(resolution, heater))
|
||||
end
|
||||
|
||||
-- outputs temperature in Celsius degrees
|
||||
function M.getHumi()
|
||||
setReadRegister(HDC1000_HUMI)
|
||||
tmr.delay(delay)
|
||||
return(read16()/65535.0*100)
|
||||
end
|
||||
|
||||
-- outputs humidity in %RH
|
||||
function M.getTemp()
|
||||
setReadRegister(HDC1000_TEMP)
|
||||
tmr.delay(delay)
|
||||
return(read16()/65535.0*165-40)
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
HDC1000 NodeMCU module
|
||||
=======================
|
||||
|
||||
Here's my NodeMCU module for the TI HDC1000 temperature and humidity sensor. It should work with the HDC1008 too but I haven't tested it.
|
||||
|
||||
### Setup your sensor:
|
||||
First, require it:
|
||||
|
||||
`HDC1000 = require("HDC1000")`
|
||||
|
||||
Then, initialize it:
|
||||
|
||||
`HDC1000.init(sda, scl)`
|
||||
|
||||
Configure it:
|
||||
|
||||
`HDC1000.config()`
|
||||
|
||||
Default options set the address to 0x40 and enable both temperature and humidity readings at 14-bit resolution, with the integrated heater on. You can change them by initializing your sensor like this:
|
||||
|
||||
`HDC1000.config(address, resolution, heater);`
|
||||
|
||||
"resolution" can be set to 14 bits for both temperature and humidity (0x00 - default) 11 bits for temperature (0x40), 11 bits for humidity (0x01), 8 bits for humidity (0x20)
|
||||
"heater" can be set to ON (0x20 - default) or OFF (0x00)
|
||||
|
||||
### Read some values
|
||||
You can read temperature and humidity by using the following commands:
|
||||
|
||||
`temperature = HDC1000.getTemp()` in Celsius degrees.
|
||||
|
||||
`humidity = HDC1000.getHumi()` in %
|
||||
|
||||
### Check your battery
|
||||
|
||||
The following code returns true if the battery voltage is <2.8V, false otherwise.
|
||||
|
||||
`isDead = HDC1000.batteryDead();`
|
||||
|
||||
Happy making! Also, check out my Breakout Board and Arduino library for this chip: http://b.truzzi.me/hdc1000-temperature-and-humidity-sensor-breakout-with-arduino-library/.
|
Loading…
Reference in New Issue