| 1 | ## BMP180 sensor |
| 2 | |
| 3 | Read values from a BMP180 sensor and dump them to the serial port. |
| 4 | |
| 5 | For connection details [refer here](/programming/i2c) |
| 6 | |
| 7 |  |
| 8 | |
| 9 | |
| 10 | The code |
| 11 | |
| 12 | !!! tip "examples/C/BMP180_UART_DEMO.c" |
| 13 | ```c |
| 14 | |
| 15 | /* |
| 16 | Scan the I2C bus and send results via UART. |
| 17 | */ |
| 18 | |
| 19 | #include <avr/kp.h> // Include file for I/O operations |
| 20 | |
| 21 | |
| 22 | #define REG_CONTROL 0xF4 |
| 23 | #define CMD_TEMP 0x2E |
| 24 | |
| 25 | #define REG_RESULT 0xF6 |
| 26 | |
| 27 | int main (void) |
| 28 | { |
| 29 | |
| 30 | uint8_t addresses[20], found,temp_cmd[]={REG_CONTROL, CMD_TEMP}, res[10]={0,0,0,0,0,0,0,0,0,0},i=0; |
| 31 | |
| 32 | i2c_init(); |
| 33 | uart_init(38400); |
| 34 | |
| 35 | |
| 36 | for(;;) |
| 37 | { |
| 38 | |
| 39 | found = i2c_scan(&addresses[0]); // i2c scan will store the addresses in `addresses`, and return total found sensors. |
| 40 | |
| 41 | for(i=0;i<found;i++){ |
| 42 | uart_send_byte_ascii(addresses[i]); // send address |
| 43 | uart_send_byte(','); // send comma. |
| 44 | |
| 45 | //BMP180 detected at 119 (0x77). read values from it and send over UART |
| 46 | for(;addresses[i] == 0x77;){ |
| 47 | delay_ms(10); |
| 48 | // write to 0x77 (bmp180 address) , 0xF4 and 0x2E |
| 49 | i2c_write(0x77 , &temp_cmd[0], 2); // init temperature measurement |
| 50 | delay_ms(10); |
| 51 | // read 2 bytes from the result register |
| 52 | i2c_read(0x77, REG_RESULT, &res[0],2); |
| 53 | |
| 54 | uart_send_byte_ascii(res[0]); // send MSB |
| 55 | uart_send_byte(','); |
| 56 | uart_send_byte_ascii(res[1]); // send LSB |
| 57 | uart_send_byte('\n'); |
| 58 | // rawT = (res[0] << 8) + res[1] |
| 59 | // a = c5 * (rawT - c6) |
| 60 | // Temperature = a + (mc / (a + md)) |
| 61 | |
| 62 | // sample values. loaded from the chip: |
| 63 | // c5 = 0.004824447631835938 |
| 64 | // c6 = 20636.0 |
| 65 | // mc = -942.88 |
| 66 | // md = 16.50625 |
| 67 | |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | } |
| 73 | if(found) |
| 74 | uart_send_byte('\n'); |
| 75 | delay_ms(500); |
| 76 | |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ## Example calculations |
| 84 | |
| 85 | The BMP180 hardware contains a dozen calibration coefficients which must be read and stored before acquiring data. |
| 86 | We have avoided this step to avoid complicating the program, and have instead provided sample values for the |
| 87 | temperature measurement process. |
| 88 | |
| 89 | ```bash |
| 90 | c5 = 0.004824447631835938 |
| 91 | c6 = 20636.0 |
| 92 | mc = -942.88 |
| 93 | md = 16.50625 |
| 94 | ``` |
| 95 | |
| 96 | Sample calculation for raw values 116(MSB), 196(LSB) . |
| 97 | |
| 98 | <pre><font color="#008700">In [</font><font color="#33DA7A"><b>2</b></font><font color="#008700">]: </font>(<font color="#008700">116</font><<<font color="#008700">8</font>) + <font color="#008700">196</font> |
| 99 | <font color="#870000">Out[</font><font color="#F66151"><b>2</b></font><font color="#870000">]: </font>29892 |
| 100 | |
| 101 | <font color="#008700">In [</font><font color="#33DA7A"><b>3</b></font><font color="#008700">]: </font>rawt= (<font color="#008700">116</font><<<font color="#008700">8</font>) + <font color="#008700">196</font> |
| 102 | |
| 103 | <font color="#008700">In [</font><font color="#33DA7A"><b>4</b></font><font color="#008700">]: </font>a = <font color="#008700">0.00482444</font>*(rawt - <font color="#008700">20636</font>) |
| 104 | |
| 105 | <font color="#008700">In [</font><font color="#33DA7A"><b>5</b></font><font color="#008700">]: </font>a |
| 106 | <font color="#870000">Out[</font><font color="#F66151"><b>5</b></font><font color="#870000">]: </font>44.65501664 |
| 107 | |
| 108 | <font color="#008700">In [</font><font color="#33DA7A"><b>6</b></font><font color="#008700">]: </font>a+(-<font color="#008700">942</font>/(a+<font color="#008700">16.506</font>)) |
| 109 | <font color="#870000">Out[</font><font color="#F66151"><b>6</b></font><font color="#870000">]: </font>29.253048985591825 |
| 110 | </pre> |