Skip to content

Instantly share code, notes, and snippets.

@beerriot
Created February 20, 2017 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beerriot/5ed0f778b92a6ec2bc1862f5800c308f to your computer and use it in GitHub Desktop.
Save beerriot/5ed0f778b92a6ec2bc1862f5800c308f to your computer and use it in GitHub Desktop.
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