Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Enthernet Code on 5/23/2024 in #middleware-and-os
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?
4 replies