0

Read from BMP180
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Read from BMP180

by hibart on Mon Feb 11, 2019 7:16 am

Hey everyone,

I would like to read data from temperature and pressure sensor BMP180 via I2C interface.
There is a ton of code for that device using micropython or C. Unfortunately there is no library for circuit python out here so I wanted to create one. But the problem I get faced with is the I2C class of circuit python. It only has a "readfrom_into" and a "writeto" method. But how can I read from and write to a special register on the slave device?

For the BMP180 for example I have to write data to register 0xF4 to initialize the temperature reading and have to read the result from 0xF6. Could anyone please give an example?


Thanks a lot!

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by adafruit_support_carter on Tue Feb 12, 2019 2:55 pm

Sorry. We didn't write a CP driver for that since it is discontinued.

For trying to figure out how to write your own, maybe use something close for reference? Like the BMP280:
https://github.com/adafruit/Adafruit_Ci ... hon_BMP280

In general, you'll want to use I2CDevice instead of using the I2C bus directly. That way locking / unlocking the bus will be taken care of for you. You can see examples of register read/write in the BMP280 driver.

adafruit_support_carter
 
Posts: 11091
Joined: Tue Nov 29, 2016 2:45 pm

Re: Read from BMP180

by hibart on Tue Feb 12, 2019 3:28 pm

Thanks a lot. I did it the exact way already and it looks good. I must just change registers and coefficients and then it should work! :)

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by adafruit_support_carter on Tue Feb 12, 2019 3:34 pm

Awesome. If you want to share the driver we host a community bundle you could PR to:
https://github.com/adafruit/CircuitPyth ... ity_Bundle

And this guide has some additional information about library writing/sharing:
https://learn.adafruit.com/creating-and ... y/overview

adafruit_support_carter
 
Posts: 11091
Joined: Tue Nov 29, 2016 2:45 pm

Re: Read from BMP180

by hibart on Tue Feb 12, 2019 5:49 pm

Allright, cool. Thanks a lot, sir. I really appreciate that.

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by hibart on Tue Feb 12, 2019 8:50 pm

I'm currently struggling a bit converting binary array data I'm getting.
When trying to read calibration registers I'm getting "bytearray(b'\xfbt')" for register 0xAC (reading 2 byte).

I know that the byte array consists of hex values but \xfbt isn't hex. What does the t at the end stands for?

Thanks a lot for ya help, I really appreciate that!

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by hibart on Wed Feb 13, 2019 11:36 am

Ha! I figured it out but I can't explain why Circuit Python is doing it like this. Makes no sense to me.

Example:

For one of the registers I'm getting back: b'\x19s'
This is a 2-byte bytearray where the first byte is 19 as hex = 25 int and the second byte is displayed as ascii character s = hex 73 = int 115. But why does it get mixed up with first byte hex and second byte ascii??? Any idea?

Thanks a lot guys!

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by hibart on Wed Feb 13, 2019 12:33 pm

All right, I don't know why the output is like that but with the struct library it is super easy to convert binary data. After experimenting with it I was able to read pressure, temperature and calculated altitude from the BMP180. I'll also create a pull request for a custom package :) Cheers!

hibart
 
Posts: 7
Joined: Mon Feb 11, 2019 7:12 am

Re: Read from BMP180

by adafruit_support_carter on Wed Feb 13, 2019 3:14 pm

It's not hex values, it's more like ASCII characters. To print as hex, do something like:
Code: Select all | TOGGLE FULL SIZE
''.join("{:02x}".format(x) for x in b)

where b is your bytearray.

adafruit_support_carter
 
Posts: 11091
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.