Monday, October 15, 2012

AVR Atmega BMP085/BMP180 pressure sensor library 01

update: checked and working on BMP180 too

BMP085/BMP180 Barometric Pressure Sensor AVR Atmega library.



The BMP085 is a high-precision, low-power barometric pressure sensor.
The BMP085 offers a measuring range of 300 to 1100 hPa with an absolute accuracy of down to 0.03 hPa.

User can setup parameter in file bmp085.h.
User can select the mode of operation, and the offset for the reading, also can enable an avarage filter on measurements. It can measure pressure, altitude, and temperature.

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.





Code


Notes
  • read risk disclaimer
  • excuse my bad english

74 comments:

  1. i compile this code for atmega328p/5V/16Mhz and its does not work. why?

    ReplyDelete
    Replies
    1. Double check your wiring, check your BMP085, even with other library.
      Then debug the bus using a logic analizer, or a simple uart terminal.

      Delete
  2. With arduino adafruit_BMP085 library - all works.
    With your library - nothing.
    I remove all code about BMP085 and testing only UART

    > #define UART_BAUD_RATE 9600
    >
    > int main (void) {
    > //init uart
    > uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
    > uart_puts("Hello init!\r\n");
    > //init interrupt
    > sei();
    > }
    But this does not work again and i not see in terminal "Hello init!" :(

    ReplyDelete
    Replies
    1. So, it's not my library which is not working :), is the uart library. Which is the Peter Fleury one.
      Download his last version of the library, check that it works with your compiler / microprocessor. There are some version of his uart library that has problems with ATmega328, it might be the one i've used for this project, but i've not encountered this problem cause I've used an ATmega8.
      You could use arduino or adafruit if it works.

      Delete
  3. Thank you Davide ! It helps !
    Rafał, Poland

    ReplyDelete
  4. Hello again!
    I ude your library and I don't understand this part: //path to i2c fleury lib
    #include BMP085_I2CFLEURYPATH. I got an error: bmp085.c:(.text+0x356): undefined reference to `i2c_init'
    make: *** [04_LCD.elf] Error 1.
    What should I change?

    ReplyDelete
    Replies
    1. Hello,
      you have to setup the relative path to the i2c fleury library, i.e. where i2cmaster.h and twimaster.c are.

      Delete
    2. Ok, so I have folder bmp085 and there are bmp085.c and bmp085.h and folder TWI and there are i2cmaster.h and twimaster.c I wrote #include TWI/i2cmaster.h and still have error

      Delete
    3. (Here is the right position :) ), anyway, as i tell you, check the include statament of the avrgcc processor, which is fairly similar than then asci C one.
      Start a simple project, create your own blink led code, and put the blink code in an external library, and eventually folder, then use #include to compile and load the function.
      If you can not do this, if you mantain the directory structure of the code you can download here, you should not have compile errors.

      Delete
  5. Hi Davide,

    Ty for the hard work! I have a question though. I use the Arduino isp to flash my atmega. Can i still use this library and display the data on realterm this way?

    Greetz,

    Jedu

    ReplyDelete
    Replies
    1. Yes, you could flash the hex straight to you micro even using Arduino ISP, but I'm pretty sure you have to do it by command line avrdude. Note that provided here is the code of the project, you have to compile and link it, using avrgcc, then you can upload the hex generated. Data it is diasply throght uart, it doesn't mind how you upload the file to your ATmega.

      Delete
  6. Hi Davide,

    Your Library support the BMP180?, you use a Peter Fleury i2c master library, but this library have a .s (Assembly) file, it´s necessary this file?, in your source code does not have, I ask because I would use this library in a ATtiny2313

    thanks

    ReplyDelete
    Replies
    1. Hello, it depends if you would like to use the TWI hardware interface, in that case you do not need the assembly file but just the twimaster.c file. Take a look at the P.Fleury i2c library manual page here: http://homepage.hispeed.ch/peterfleury/group__pfleury__ic2master.html

      Delete
    2. I've never tryed this on the BMP180, anyway, anyway i take a look at the BMP180 sheet, and it seams to me it has the same pressure calculation alghoritm and i2c registers.. so it should work, but you have to try.

      Delete
  7. Hello, I tried to compile your code, but I got an errors in AVR Studio 4, can you give me a solution? Thank you.

    /avr/bin/ld.exe: region `text' overflowed by 18 bytes
    and
    /avr/bin/ld.exe: main.elf section `.text' will not fit in region `text'

    ReplyDelete
    Replies
    1. Hello,
      you are trying to compile the code on a micro that does not have enought space for the "text" region.
      If you are using ATmega8, check that you have linked also libmath (-lm directive on the linker command)

      Delete
    2. Hello,
      thank for your reply. I am using ATMEGA8. I tried to link libmath, but still no change. If possible, can you send me please you project file with all files? Thank you.

      Delete
    3. The download files here are just my complete project files. I can also attach you compiled hex. Send me your mail, i will reply with compiled hex for Atmega8 @ 1MHz

      Delete
    4. Thank you :) my email is bystricanp () gmail () com

      Delete
  8. Hello,

    After intitialization of bmp085_init(); I can't use my LCD. Although LCD uses totally different ports such as PORTB 0...7 for data port and PORTD 5...7 for control port. I use ATmega32 of which SDA uses PORTC1 and SDC uses PORTC0
    For clock pulse I use 3.6864MHz xtal.

    Could you please tell me what modifications i would need to do in the source files or header files to get a working code?

    http://pastebin.com/hLYjREpF

    ReplyDelete
    Replies
    1. Hello,
      It may be that the bmp05 it's not initialized the right way. Check that the bmp085 alone works, without the LCD. Debug it using the serial port. If you have a logic analizer, check the bmp085 bus. Check you sensor with another hardware. Check that the fuse are properly set up for your crystal, and that the SPI channell is properly setted.

      Delete
  9. It must have been a breadboard circuit thing. I've built it again and It now works fine. :) Congrats!

    ReplyDelete
    Replies
    1. Well, i was happy yesterday so that it seemed to work. I use noise filtering capacitors. I give 2.5V to the BMP180 (not BMP085) with 2x47k resistor voltage divider. If I burn the chip it's operating for about some minutes showing me the actual temperature, altitude, air pressure datas on the LCD. Then it gets freezed. Stops working and even after reset, LCD acts as if it was not initialized. No sign of running code. (No transmitting data on UART too) After a little while measurement datas gets randomly refreshed on the LCD. I surely know my own LCD & UART libraries are ok, because I've used them in my previous works. If I uncomment the initialization line in the main program //bmp085_init(); my avr dont get freezed. This issue happenes with both of my Atmega32. May it be possible that this library is incompatible with BMP180?

      Delete
    2. Hello, I've checked it today on a GY-68 board, which mounts a BMP180 and a voltage regulator for over 1 hour. I confirm you that it works on BMP180 too, no problem at all. Check that your supply voltage is enough for all your equipment. Check for conflict over the SPI ports of your LCD library. Check for long timer routing of your LCD. Check just the BMP180 output over UART. Then mix it to your LCD, just to exclude problems of all sorts.

      Delete
    3. Maybe it was my fault to connect the SDA, SCL pins to +5V instead of +3.3Vwith 4.7k resistors. I am without a working sensor now. I downloaded your zip files & I tested your libraries again with no sensor. Have you tied what happens when you have no sensor connected? Could you try this code please? I get no working UART communication if the sensor is initialized. Without uninitialized sensor ( //bmp085_init(); ), yes UART communication is working. http://pastebin.com/bKCfR5ZB

      Delete
    4. SDA, SCL can be pulled up to 5v through a 4k7 resistor without problem. Without sensor it does not work, cause the SPI get bloked if the Slave does not respond.

      Delete
  10. Thanks for libraries! Good job!
    I have problem with bmp85 library, my code:

    long l;
    double d;
    char printbuff[10];

    //get pressure
    l = bmp085_getpressure();
    ltoa(l, printbuff, 10);

    lcd_clrscr();
    lcd_gotoxy(1, 3); //znak,radek
    lcd_puts(printbuff);

    _delay_ms(8000);

    from debugger:
    l 0x00000000 long{registers}@ R25 R24 R23 R22
    +display show number 0,

    I didnt change anything in libraries bmp85/I2C. I have Atmega32, bmp180.

    Can you help me please?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Hello,
      at a first view, "bmp085_init" is missing. Anyway double check your wiring, also check your fuse. Check that the sensor works with other hardware. At a first step, use the sample mail file and output to uart, That way you will be shure if it is the LCD library or the bmp085 one that does not work.

      Delete
  11. the same code will work for bmp180?

    ReplyDelete
    Replies
    1. Hello. Yes. Tested on a BMP180 board and working.

      Delete
    2. what changes to be made in the main.c file

      Delete
    3. No change needs to be made. Just compile it with avrgcc. You can find a lot of tutorial online that teach how to compile with avrgcc.

      Delete
  12. error is "undefined reference to main".
    make.exe:***[main.elf] Error 1

    ReplyDelete
    Replies
    1. It's a compiler error, i suppose you are not linking or compiling the right way.

      Delete
    2. i have added the relevant files in the project
      in the makefile i have added all these files in the SRC type
      and then i compiled

      is there a way you can help me to fix this

      Delete
    3. When you said project, what project do you mean?
      If you use Eclipse IDE with AVR plugin, and avrgcc compliler, just create a new AVR project, add all the src file provided as src files. Select the proper ATmega micro, and F_CPU in the project properties. Select Relese as the default building ouput. Then Build the project.

      Delete
    4. This project it is tested on WinAVR 20100110, and working. A note: please do not flood with comments this blogpost, try to write comments that can be helpfull for other user too, full descriptive comments usually are simpler for user to read.

      Delete
    5. thanks for the help but still the error exists but thanks for all the help

      Delete
    6. If it helps, this is my Eclipse project file (48h expiration link): http://expirebox.com/download/b8169375f5768b3061d95f467327cdb8.html

      Delete
  13. hello david.
    i want to display only the pressure readings on the lcd display and i have ATMega32A
    please help me step by step
    i am totally new to this
    this is my first project

    ReplyDelete
    Replies
    1. Hello, you can start by the sample you find in this project. And of course the sample of an LCD library, my suggestion is the Peter Fleury one. But, if you are really a beginner, the Arduino project may be your friend.

      Delete
  14. Can you please give code for mpu6050 with atmega ..Just want to read data from mpu6050 ...And thanks for this code

    ReplyDelete
    Replies
    1. Hello. You can find the code for the mpu6050 library here: http://davidegironi.blogspot.it/2013/02/avr-atmega-mpu6050-gyroscope-and.html
      Read accelerometer and gyro data from the mpu6050, and pressure data from this sensor. I2C allows multiple sensors (with different address) on the same bus.

      Delete
  15. Hello Davide, did you remove the code? I can't seem to find it!
    Thanks in advance

    ReplyDelete
    Replies
    1. Hello, no it's google code that changed what should be a permalink. Anyway, you can find that code on page 3 of the downloads. Newer project will be submitted on github or sourceforge.

      Delete
  16. Hi Davide,

    I modify your library for the BMP180. I have a reading of 71525 Pa, which is (following the formula in the datasheet) = approx 2400m ... but I live in the city at sea level... Is your reading correct? Are you actually almost 300m above the sea level? Thanks!

    ReplyDelete
    Replies
    1. Hello, I've checked right now the library with a low cost BMP085 and another BMP180 board where i live now. According to my local weather service, I'm actually at 1025.2 hPa. Both the board gives almost the same result of my weather service, almost 100235 one board, 100255 Pa the other.

      Delete
  17. Why did you use value 0.0000225577 in your code? Shouldn't it be 0.0000225580?

    ReplyDelete
    Replies
    1. I've never encouter the 0.0000225580 constant. In troposhpere p = 101325 x (1 - 0.0000225577 x h)^5.25588. Anyway i a nano round will not make substatial difference here.

      Delete
    2. In my reply above here, that's the value I've always encounter, anyway I'm not a atmospheric physics expert, so I may be wrong.

      Delete
  18. Maybe not, but this comes from straight from Bosch data sheet, so I was wondering why your figure is different than that. Your lib works very well, but I'll change this value anyway to 0.0000225580. Thanks!

    ReplyDelete
    Replies
    1. Thank you for your feedback, it may helps someother, and it is usefull to me too!

      Delete
  19. Hi Davide,

    I have problem with wrong values returned from BMP085.
    This is what I get from the sensor:

    T:-92.10 [C], C:1194889728 [hPa]

    I use Atmega16A with exernal clock 16 MHz and send all values by bluetooth module.
    I changed in your code only line 17 in 'twinmaster.c' to:
    #define F_CPU 16000000UL

    Do you have any idea why these values are wrong?

    Cheers,
    Mateusz

    ReplyDelete
    Replies
    1. Hello, have you check the raw register values? Can you check this by wired UART?

      Delete
    2. I have checked this yesterday and the values were 'normal'. Its weird, because I didn't change enything.

      But I have another 'problem' The pressure returning from BMP085 shows something like:
      800hPa (Altitude ~1800 m under sea level), but I live in place where is ~220 m under sea level.

      Do you have any idea?

      Delete
    3. Additionaly the temperature is about 1,2 Celcius degree too low (I compared with temp from dht11 and ds18b20)

      Delete
    4. Hello, about the temperature shift, between (average on datasheet) 0 and 50C DTH11 it's a +-2C and DS18B20 it's +-0.5C, BMP850 it's +-1C, so it could be a valid temperature drift. About the barometric values.. what are values you get from your local weather (online) station?

      Delete
    5. Temperature - ok, it might be problem with accuracy.

      Pressure from local weather station is about 1005 hPa (but this is normalized pressure in terms sea level) My pressure is almost 200 hPa lower.

      Delete
    6. I cheated a little bit, because I have added this 200 hPa in main loop, but... seems like bmp085 returning still wrong values:
      today I have checked pressure in my city:1001 hPa, but my pressure is 1111 hpa (notice: 2 days ago outside was 1112 hpa, so I 'setup' == added in main loop value, to result be the same: 1112 hpa)
      Today differences between local station and BMP085 is 10 hpa... My senson measured the same value 2 days ago and today

      Delete
    7. I do not understaind the pressure part. Do you mean your sensor needs a 200hPA offset in order to be 10hPA far away from your local nearest station?

      Delete
  20. Hi Davide,
    your lib workes almost out of the box. Thanks for your work.
    But with my BMP180 and ATmega644 @16MHz I get ridiculous pressure readings of about 550 hPa. The temperature workes fine for me.

    With the Adafruit BMP085 lib and my Teensy 3.1 the pressure reading is ok (about 1015 hPa).
    Do you have any idea how to fix that?

    My Code:
    #define F_CPU 16000000UL

    #define checkbit(var,bit) (var &(1<
    #include
    #include
    #include
    #include
    #include
    #include
    #include "i2cmaster.h"
    #include "i2clcd.h"
    #include "bmp085.h"


    void display_init(void){

    lcd_init();
    lcd_command(LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKINGOFF);
    lcd_light(true);

    lcd_printlc(1,1,"Projekt Funk");
    lcd_printlc(3,1,"Temp. : ");
    lcd_printlc(4,1,"A Pr. : ");

    _delay_ms(50);
    }


    int main(void) {


    DDRA = 0b00000000;
    DDRB = 0b11111111;
    DDRC = 0b11111111;
    DDRD = 0b11111111;

    PORTA = 0b00000000;
    PORTB = 0b00000000;
    PORTC = 0b00000000;
    PORTD = 0b00000000;

    sei();

    char printbuff[100];
    long l;
    double d;

    i2c_init();
    display_init();
    bmp085_init();


    while (1) {
    d = bmp085_gettemperature();
    dtostrf(d, 6, 2, printbuff);
    lcd_printlc(3, 9, printbuff);
    lcd_print_p(" \337C");
    _delay_ms(10);

    l = bmp085_getpressure();
    ltoa(l, printbuff, 10);
    lcd_printlc(4 ,8," ");
    lcd_printlc(4, 9, printbuff);
    lcd_print_p(" Pa");
    _delay_ms(10);

    d = bmp085_getaltitude();
    dtostrf(d, 8, 2, printbuff);
    lcd_printlc(2, 1, printbuff);
    lcd_print_p(" m");
    _delay_ms(500);
    }
    }

    ReplyDelete
    Replies
    1. Hello, unluckly I do not have a ATmega644 to test it with. Have you checked it at other running frequency (1Mhz, 8Mhz...)? It may be a problem related to F_CPU. Also, check the bus between the Teensy and the Sensor using a logic analyser, and check it with my library the ATmega.

      Delete
    2. Yep, F_CPU in the main.c was causing the problem.
      If I declare F_CPU in the project properties, it workes.
      Thank you.

      Delete
    3. You're welcome. Happy to hear that! :)

      Delete
  21. Hi Davide ,

    Thanks for sharing your great experiment.I could the find the files of the project .Could you help me ?
    Thanks

    ReplyDelete
    Replies
    1. Hello and thank you. You can find files in the download link above in the post, page 3.

      Delete