This post shows how to interface Arduino UNO board with BMP280 barometric pressure and temperature sensor from Bosch Sensortec.
The Arduino reads temperature & pressure values from the BMP280 sensor and prints them (respectively in °C and hPa) on SSD1306 OLED display (128×64 pixel).
In this project the BMP280 sensor is used in I2C mode.
The SSD1306 OLED used in this project is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.
To see how to interface Arduino with SSD1306 OLED display, visit the following post:
Interfacing Arduino with SSD1306 OLED display
And to see how to interface Arduino with BMP280 sensor for the first time, take a look at this post:
Interfacing Arduino with BMP280 pressure and temperature sensor
Hardware Required:
- Arduino uno board
- SSD1306 OLED display (128×64 pixel)
- BMP280 sensor module (with built-in 3.3V regulator and level shifter) —-> BMP280 datasheet
- Breadboard
- Jumper wires
Arduino with SSD1306 OLED display and BMP280 sensor circuit:
The image below shows project circuit diagram.
Hint:
The BMP280 chip works with maximum voltage of 3.6V (supply voltage range is from 1.71 to 3.6V) which means we’ve to use a 3V3 voltage regulator to supply it from a 5V source.
Also, if we’re working with a 5V system (development board, microcontroller …) like the Arduino UNO board (ATmega328P microcontroller), we’ve to use a voltage level shifter (level converter) which converts the 3.3V (comes from the BMP280 chip) into 5V (goes to the ATmega328P) and vice versa. This level shifter is for the I2C bus lines (clock and data).
The BMP280 module shown in project circuit diagram has a built-in 3.3V regulator and level shifter.
Generally, the BMP280 module has at least 4 pins because it can work in SPI mode or I2C mode. For the I2C mode we need 4 pins: VCC, GND, SDA and SCL where:
GND (ground) is connected to Arduino GND pin,
VCC is the supply pin which is connected to Arduino 5V pin,
SDA is I2C bus serial data line, connected to Arduino analog pin 4 (A4),
SCL is I2C bus serial clock line, connected to Arduino analog pin 5 (A5).
The SSD1306 OLED display is connected to the Arduino UNO board as follows:
SSD1306 OLED GND goes to Arduino GND (ground),
SSD1306 OLED VDD to Arduino 5V,
SSD1306 OLED SDA pin (serial data) to Arduino analog pin 4 (A4),
SSD1306 OLED SCK pin (serial clock) to Arduino analog pin 5 (A5),
SSD1306 OLED RES pin (reset) to Arduino pin 4.
Pins A4 and A5 are hardware I2C module SDA and SCL pins (respectively) of the Arduino UNO.
The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x3D. If the DC pin is connected to ground (GND) then the I2C slave address will be 0x3C.
The SSD1306 OLED and the BMP280 sensor are connected to the same I2C bus (slave devices), SCL and SDA pins of the two devices are connected to SCL and SDA pins of the Arduino board.
The I2C slave address of the SSD1306 OLED differs from the one of the BMP280 sensor, this difference allows the master device (Arduino microcontroller) to talk to one of them (only one at a time).
Arduino with SSD1306 OLED and BMP280 sensor code:
The following Arduino code requires 3 libraries from Adafruit Industries:
The first library is a driver for the SSD1306 OLED display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “ssd1306” and install the one from Adafruit).
The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.
The third library is for the BMP280 sensor.
The previous 3 libraries can also be installed manually, download links are below:
Adafruit SSD1306 OLED driver —-> direct link
Adafruit graphics library —-> direct link
Adafruit BMP280 Library —-> direct link
You may need to install the Adafruit Unified Sensor library if it’s not already installed, download link is below:
Adafruit Unified Sensor library —-> direct link
After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for the other library files.
In the code there are total of 4 libraries, they’re included in the code as follows:
1 2 3 4 | #include <Wire.h> // include Arduino wire library (required for I2C devices) #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #include <Adafruit_BMP280.h> // include Adafruit library for BMP280 sensor |
The SSD1306 OLED display reset pin connection is defined in the code as shown below:
1 | #define OLED_RESET 4 // define display reset pin |
As any other I2C device, the BMP280 sensor has an I2C slave address which may be 0x76 or 0x77. This address depends on the connection of the SDO pin (used for SPI mode as serial data out or MISO), if the SDO pin is connected (directly or through resistor) to VCC (3.3V) the address will be 0x77, and if it’s connected to GND the address will be 0x76.
The default I2C address of the BMP280 library is defined as 0x77 and my device I2C address is 0x76.
In the code, the definition of the I2C slave address is shown below:
1 2 | // define device I2C address: 0x76 or 0x77 (0x77 is library default address) #define BMP280_I2C_ADDRESS 0x76 |
The initialization of the BMP280 sensor is done using the function begin() which returns 1 if OK and 0 if error. In the code the initialization with the previously defined address is as shown below:
1 | bmp280.begin(BMP280_I2C_ADDRESS) |
Reading the values of temperature and pressure is done as shown below:
1 2 3 | // get temperature and pressure from library float temp = bmp280.readTemperature(); // get temperature float pressure = bmp280.readPressure(); // get pressure |
Note that the BMP280 sensor library returns the value of the pressure in Pa unit and to convert it to hPa we’ve to divide it by 100.
1 bar = 10000 Pa = 100 hPa. ( 1 hPa = 100 Pa = 1 millibar)
Pa: Pascal
hPa: hectoPascal
Temperature and pressure values are displayed on the SSD1306 OLED screen.
If there is a problem with the BMP280 sensor (for example wrong device address) the screen will display Connection Error.
Full Arduino Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /* * Arduino with SSD1306 OLED display (128x64 Pixel) and * BMP280 barometric pressure & temperature sensor. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ #include <Wire.h> // include Arduino wire library (required for I2C devices) #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #include <Adafruit_BMP280.h> // include Adafruit BMP280 sensor library #define OLED_RESET 4 // define display reset pin Adafruit_SSD1306 display(OLED_RESET); // initialize Adafruit display library // define device I2C address: 0x76 or 0x77 (0x77 is library default address) #define BMP280_I2C_ADDRESS 0x76 Adafruit_BMP280 bmp280; // initialize Adafruit BMP280 library void setup(void) { delay(1000); // wait a second // initialize the SSD1306 OLED display with I2C address = 0x3D display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // clear the display buffer. display.clearDisplay(); display.setTextSize(1); // text size = 1 display.setTextColor(WHITE, BLACK); // set text color to white and black background display.setCursor(25, 0); // move cursor to position (15, 0) pixel display.print("BMP280 SENSOR"); display.display(); // update the display // initialize the BMP280 sensor if( bmp280.begin(BMP280_I2C_ADDRESS) == 0 ) { // connection error or device address wrong! display.setTextSize(2); // text size = 2 display.setCursor(5, 17); display.print("Connection"); display.setCursor(35, 37); display.print("Error"); display.display(); // update the display while(1); // stay here } display.setCursor(29, 11); display.print("TEMPERATURE:"); display.setCursor(38, 40); display.print("PRESSURE:"); display.display(); // update the display display.setTextSize(2); // text size = 2 } char _buffer[9]; void loop() { // get temperature and pressure from library float temp = bmp280.readTemperature(); // get temperature float pressure = bmp280.readPressure(); // get pressure // print data on the LCD // 1: print temperature if(temp < 0) sprintf(_buffer, "-%02u.%02u C", (int)abs(temp), (int)(abs(temp) * 100) % 100 ); else sprintf(_buffer, " %02u.%02u C", (int)temp, (int)(temp * 100) % 100 ); display.setCursor(11, 21); display.print(_buffer); // print degree symbols ( ° ) display.drawCircle(89, 23, 2, WHITE); // 2: print pressure sprintf(_buffer, "%04u.%02u", (int)(pressure/100), (int)((uint32_t)pressure % 100)); display.setCursor(3, 50); display.print(_buffer); display.setCursor(91, 50); display.print("hPa"); // update the display display.display(); delay(1000); // wait a second } // end of code. |
The following picture shows a protoboard circuit of the project:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Where can I get the header (*.h and *.ppp) files for this project?