C program in embedded Linux reads data from BMP280 temperature sensor and displays it on Adafruit1.2

Hello guys, I am trying to write a C program in embedded Linux that reads data from a BMP280 temperature sensor and displays the temperature on an Adafruit 1.3 OLED display connected via SPI. However, I am facing issues with reading data from the I2C sensor. I Connected the BMP280 sensor to the I2C bus and verified the connection using i2cdetect.Then i Set up the OLED display on the SPI bus and verified the connection. This is my attempt to read from the BMP280: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #define BMP280_ADDRESS 0x76 int main() { int file; char *filename = "/dev/i2c-1"; if ((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus"); return 1; } if (ioctl(file, I2C_SLAVE, BMP280_ADDRESS) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; }
// Reading data from the BMP280 char reg[1] = {0xF7}; // Register to read from write(file, reg, 1); char data[8] = {0}; if (read(file, data, 8) != 8) { perror("Failed to read from the i2c bus"); } else { // Process the data (e.g., convert raw data to temperature) int temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); // Convert temp_raw to actual temperature // ... (calculation based on BMP280 datasheet) printf("Temperature: %d\n", temp_raw); } close(file); return 0; } Can someone please provide guidance or a detailed work-through on how to properly read from the BMP280 and display the temperature on the OLED screen?
3 Replies
Marvee Amasi
Marvee Amasi7mo ago
@Enthernet Code Have you considered using a library to simplify the communication with both the BMP280 and the OLED display? While your code snippet demonstrates a good start with the I2C bus access, libraries can handle the low-level details and provide a more user-friendly interface for reading sensor data and controlling the display. Here are some popular options: * WiringPi: A versatile library for interfacing with various hardware components in Linux. It offers functions for I2C communication and can be used to read from the BMP280. * Adafruit_BBIO: Specifically designed for BeagleBone Black boards, this library includes support for I2C and SPI communication, making it ideal for interacting with both the BMP280 and the OLED display. Using a library can save you time and effort in writing complex I2C and SPI routines, allowing you to focus on the core functionality of reading temperature and displaying it.
Enthernet Code
Enthernet Code7mo ago
Thanks I'll try this out
Dtynin
Dtynin7mo ago
@Enthernet Code To read data from the BMP280, you need to send a command to the sensor specifying the temperature register and then read the temperature data from the sensor. you can try this #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #define BMP280_ADDRESS 0x76 int file; char *filename = "/dev/i2c-1"; if ((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus"); return 1; } if (ioctl(file, I2C_SLAVE, BMP280_ADDRESS) < 0) { perror("Failed to acquire bus access and/or talk to slave"); return 1; } char reg[1] = {0xFA}; // Temperature register address write(file, reg, 1); char data[3] = {0}; if (read(file, data, 3) != 3) { perror("Failed to read from the i2c bus"); } else { // Process the data (e.g., convert raw data to temperature) int temp_raw = (data[0] << 16) | (data[1] << 8) | data[2]; // Convert temp_raw to actual temperature // ... (calculation based on BMP280 datasheet) printf("Temperature: %d\n", temp_raw); } close(file); These steps should read the temperature data from the BMP280 sensor. After that, you can use the SPI interface to communicate with the Adafruit OLED display and send the temperature data to it for display.
Want results from more Discord servers?
Add your server