Skip to content

Interfacing BME280 Sensor with the ATmega2560 Development Board

Introduction

In this guide, we will learn how to interface the BME280 sensor with the ATmega2560 development board using the I2C (Inter-Integrated Circuit) communication protocol. The BME280 sensor is a versatile environmental sensor that measures temperature, humidity, and atmospheric pressure. By connecting the BME280 sensor to the ATmega2560 using I2C communication, we can easily retrieve environmental data and utilize it for various applications such as weather monitoring, indoor climate control, and environmental sensing.

Problem Statement

The objective of this project is to interface the BME280 sensor with the ATmega2560 development board using I2C communication and retrieve temperature, humidity, and atmospheric pressure readings from the sensor.

Requirements

To successfully complete this project, you will need the following components:

  • ATmega2560 development board.
  • BME280 Sensor.
  • Micro USB cable
  • Breadboard.
  • Jumper Wires.
  • Potentiometer (Built-in)

Circuit Diagram

  • For LCD connection, refer to this page.
  • Connect VCC of BME280 sensor to 5v of the ATmega2560 development board.
  • Connect GND of BME280 sensor to GND of the ATmega2560 development board.
  • Connect SCL, SDA to PD0 and PD1 (PD0 - 43 and PD1 -44) respectively.
Pin Diagram
Pin Diagram

Code

  1. Open VS Code and paste the code into the file named main.c
  2. The previously downloaded library for LCD should also be present for this code to work, which can be found here.
  3. Since, bmme280 works on I2C communication we will use I2C library and a header file for getting values from the sensor both of them along with lcd header can be found here. (You can find a main.c file where the main function is implemented.)
  4. To compile and convert this into hex, run the following command in your terminal-
cmd
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o bme280.hex main.c
c
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif

#include <string.h>       // Standard C Library for string functions
#include <stdint.h>       // Standard C Library for uint8_t, uint16_t, uint32_t, etc
#include <stdio.h>        // Standard C Library for sprintf() function
#include <avr/io.h>       // Standard AVR IO Library
#include <util/delay.h>   // Standard AVR Delay Library
#include <stdlib.h>       // Standard C Library for itoa() function
#include "bme280.c"       // BME280 Library
#include <avr/io.h>       // Standard AVR IO Library
#include <util/delay.h>   // Standard AVR Delay Library
#include "lcd.c"          // LCD Library
char output[20];          // output string
int result = 0;           // result
uint16_t temperature = 0; // temperature
float humidity = 0;       // humidity
char str_val[20];         // string value
uint16_t pressure = 0;    // pressure
int main(void)
{
    LCD_Init();                                           /* Initialize LCD */
    LCD_Clear();                                          /* Clear LCD */
    LCD_gotoxy(0, 0);                                     /* Enter column and row position */
    LCD_string("BME280");                                 // print BME280
    _delay_ms(500);                                       // wait 500ms
    LCD_Clear();                                          // Clear LCD
    DDRC = 0Xff;                                          // set the port as OUTPUT
    Bme280CalibrationData calibrationData;                // calibration data
    result = bme280ReadCalibrationData(&calibrationData); // read calibration data
    while (1)
    {
        if (result == BME280_OK) // if read calibration data is OK
        {
            Bme280Data data;
            result = bme280ReadData(BME280_OSS_1, BME280_OSS_1, BME280_OSS_1, &data, &calibrationData); // read data
            if (result == BME280_OK) // if read data is OK
            {
                temperature = data.temperatureC;       // get temperature
                humidity = data.humidityPercent;       // get humidity
                pressure = data.pressurePa;            // get pressure
                pressure = pressure / 100;             // convert pressure to mmHg
                sprintf(output, "T:%d", temperature);  // print temperature and humidity
                LCD_gotoxy(0, 0);                      // Enter column and row position
                LCD_string(output);                    // print temperature and humidity
                LCD_WriteData(0xDF);                   // print degree symbol
                LCD_string("C ");                      // print C
                sprintf(output, "P:%dmmHg", pressure); // print pressure
                LCD_string(output);                    // print pressure
                LCD_gotoxy(0, 1);                      // Enter column and row position
                LCD_string("H:");                      // print humidity
                dtostrf(humidity, 3, 2, str_val);      // convert humidity to string
                strcat(str_val, "%");                  // add % to string
                LCD_string(str_val);                   // print humidity
                _delay_ms(100);                        // wait 100ms
            }
        }
    }
    return 0; // never reached
}
  1. Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
  2. In the Flash section, click on three dots and navigate to the desired hex file, and select it. Finally, click on Program to burn the hex file in the microcontroller.

Output

Upon successful upload and execution of the code, the LCD will show the temperature, humidity and temperature readings obtained from the BME280 sensor. The readings will be continuously updated on the LCD.

Conclusion

By following this guide, you have successfully interfaced the BME280 sensor with the ATmega2560 development board using I2C communication. You can now retrieve temperature, humidity, and atmospheric pressure readings from the BME280 sensor. This project opens up possibilities for various applications, such as weather monitoring, indoor climate control, or environmental sensing.