Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Beer IoT (Part 4) example code: Altimeter Reading
addr = 0x53 -- Default I2C address
reg_pwr_ctl = 0x2D -- Power control register
pwr_ctl_on = 0x08 -- Active measurement mode
reg_x0 = 0x32 -- X0 register
length = 6 -- Bytes for a full measurement
-- Power up
he.set_power(true)
-- ADXL345 starts in an idle mode; activate it
he.i2c.txn(he.i2c.tx(addr, reg_pwr_ctl, pwr_ctl_on))
-- ADXL345 automatically advances through the rest of the
-- axis registers, so we can read them all with one command
status, buffer = he.i2c.txn(he.i2c.tx(addr, reg_x0),
he.i2c.rx(addr, length))
-- values are 16bit, little endian, signed integers
if status and #buffer == length then
x,y,z = string.unpack("i2i2i2", buffer)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment