Skip to content

Instantly share code, notes, and snippets.

@smittytone
Last active August 29, 2015 14:02
Show Gist options
  • Save smittytone/2aa569eecec90a04035c to your computer and use it in GitHub Desktop.
Save smittytone/2aa569eecec90a04035c to your computer and use it in GitHub Desktop.
Electric Imp Squirrel class for Bosch BMP180 Temperature and Pressure Sensor
class BMP180_Sensor
{
// Squirrel Class for Bosch BMP180 Temperature and Pressure Sensor
// [http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf]
// As used on the Adafruit BMP180 breakout board
// [http://www.adafruit.com/products/1603]
// Bus: I2C
// Code by Tony Smith (@smittytone) June 2014
// Version 1.0
// Note: current version only returns temperature readings
// Constants for BMP180
static BMP180_out_msb = "\xF6"
static BMP180_out_lsb = "\xF7"
static BMP180_ctrl = "\xF4"
static BMP180_chipid = "\xD0"
static BMP180_temp = "\x2E"
static BMP180_press = "\x34"
static BMP180_eprom_AC1 = "\xAA"
static BMP180_eprom_AC2 = "\xAC"
static BMP180_eprom_AC5 = "\xB2"
static BMP180_eprom_AC6 = "\xB4"
static BMP180_eprom_MC = "\xBC"
static BMP180_eprom_MD = "\xBE"
// Callibration values to be read at initialization
// from BMP180's on-board EPROM
_ac5 = 0
_ac6 = 0
_mc = 0
_md = 0
// I2C values
_i2c_address = 0
_i2c = null
constructor (imp_i2c_bus, i2c_address_7_bit)
{
_i2c = imp_i2c_bus
_i2c.configure(CLOCK_SPEED_400_KHZ);
_i2c_address = i2c_address_7_bit << 1
}
function init()
{
// Read in per-chip callibration data for temperature conversions
// ac5 and ac6 are unsigned 16-bit values
local a = _i2c.read(_i2c_address, BMP180_eprom_AC5, 2);
_ac5 = (a[0] << 8) + a[1];
a = _i2c.read(_i2c_address, BMP180_eprom_AC6, 2);
_ac6 = (a[0] << 8) + a[1];
// mc and md are signed 16-bit values, so need to be sign-extended
// to the 32-bit values Squirrel uses
a = _i2c.read(_i2c_address, BMP180_eprom_MC, 2);
_mc = (((a[0] << 8) + a[1]) << 16) >> 16;
a = _i2c.read(_i2c_address, BMP180_eprom_MD, 2);
_md = (((a[0] << 8) + a[1]) << 16) >> 16;
}
function get_temp()
{
// Get the current temperature data, and convert
// from internal units to degrees Celsius
// Signal BMP180 to take a reading
_i2c.write(_i2c_address, BMP180_ctrl + BMP180_temp)
// Pause 5ms while chip's ADC digitises the reading
imp.sleep(0.005)
// Get the reading
local a = _i2c.read(_i2c_address, BMP180_out_msb, 2)
// Convert to Celsuis
local ut = (a[0] << 8) + a[1]
local x1 = (ut - _ac6) * _ac5 / 32768.0
local x2 = _mc * 2048.0 / (x1 + _md)
local b5 = x1 + x2
local temperature = (b5 + 8) / 160.0
return temperature
}
}
@feesta
Copy link

feesta commented Aug 12, 2014

This is a great start for working with this sensor. Have you considered making the pressure function? I took a stab at it (https://gist.github.com/feesta/926869ea3e3d373ca76c) referencing the Arduino library but the returned pressure is off. Do you have thoughts on what I'm doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment