The code that should be able to read temperature, pressure, and altitude data from a BMP280 sensor and display it on a 0.95 inch RGB OLED display:

#include <Wire.h>
#include "SPI.h"
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP280.h"
#include <Adafruit_SSD1331.h>

#define OLED_DC 6
#define OLED_CS 10
#define OLED_CLK 13
#define OLED_MOSI 11
#define OLED_RESET 9
Adafruit_SSD1331 display = Adafruit_SSD1331(OLED_DC, OLED_CS, OLED_CLK, OLED_MOSI, OLED_RESET);

// Setup connection of the sensor
Adafruit_BMP280 bmp; // I2C
// For SPI connection:
// #define BMP_SCK 13
// #define BMP_MISO 12
// #define BMP_MOSI 11
// #define BMP_CS 10
// Adafruit_BMP280 bme(BMP_CS); // hardware SPI
// Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

// Variables
float pressure; // To store the barometric pressure (Pa)
float temperature; // To store the temperature (oC)
int altimeter; // To store the altimeter (m) (you can also use it as a float variable)

void setup() {
display.begin(); // Begin OLED display
bmp.begin(); // Begin the sensor
Serial.begin(9600); // Begin serial communication at 9600bps
Serial.println("Adafruit BMP280 test:");
}

void loop() {
// Read values from the sensor:
pressure = bmp.readPressure();
temperature = bmp.readTemperature();
altimeter = bmp.readAltitude(1050.35); // Change the "1050.35" to your city's current barometric pressure (https://www.wunderground.com)

// Print values to serial monitor:
Serial.print(F("Pressure: "));
Serial.print(pressure);
Serial.print(" Pa");
Serial.print("\t");
Serial.print(("Temp: "));
Serial.print(temperature);
Serial.print(" oC");
Serial.print("\t");
Serial.print("Altimeter: ");
Serial.print(altimeter); // This should be adjusted to your local forecast
Serial.println(" m");

// Display values on OLED display:
display.clearDisplay();
display.setCursor(0, 0);
display.println("Pressure: " + String(pressure) + " Pa");
display.println("Temperature: " + String(temperature) + " oC");
display.println("Altimeter: " + String(altimeter) + " m");
display.display();

delay(5000); // Update

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Do you have a question, or should this be in the showcase section?

I have question my code do not work yet so is not valid as show case!

Are you trying to change the display type to an SSD1331 from some other type of display?
The SSD1331 library has no clearDisplay() or display() functions.

1 Like

Yes that is true!

Den fre 6 jan. 2023 13:07david_2018 via Arduino Forum <notifications@arduino.discoursemail.com> skrev:

here i try to fix the code still geting bugs!// Inkludera bibliotek för I2C-kommunikation, OLED display och BMP280 sensorn
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP280.h"

// Definiera digitala pinnar för OLED displayen
#define OLED_DC 6
#define OLED_CS 7
#define OLED_CLK 8
#define OLED_MOSI 9
#define OLED_RESET 4
#define BLACK 0x0000

// Skapa objekt för OLED displayen och BMP280 sensorn
Adafruit_SSD1331 display(OLED_DC, OLED_CS, OLED_CLK, OLED_MOSI, OLED_RESET);
Adafruit_BMP280 bmp;

void setup() {
// Initiera seriell kommunikation och OLED displayen
Serial.begin(9600);
display.begin(); // Initiera OLED displayen
display.setRotation(1); // Välj displayrotation (0, 1, 2 eller 3)
display.fillScreen(BLACK); // Rensa displayen

// Initiera BMP280 sensorn
if (bmp.getStatus() == 0) {
Serial.println("Kunde inte hitta BMP280 sensorn!");
Serial.println("Error: " + String(bmp.getStatus()));

while (true);  // Fastna i en oändlig loop om sensorn inte hittas

}
}

void loop() {
// Put your code here that you want to run repeatedly
displayTempPressAlt();
}

// Läs värden från BMP280 sensorn
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure();
float altitude = bmp.readAltitude();

Serial.print("Temperature: ");
Serial.println(temperature);

Serial.print("Pressure: ");
Serial.println(pressure);

Serial.print("Altitude: ");
Serial.println(altitude);
}

Are you deliberately choosing to ignore the request to use code tags when posting code ?

...and are you also deliberately not telling us what the bugs are?

no deliberately i forget to tell you that i fixed the code !
// Inkludera bibliotek för I2C-kommunikation, OLED display och BMP280 sensorn
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP280.h"

// Definiera digitala pinnar för OLED displayen
#define OLED_DC 8
#define OLED_CS 10
#define OLED_CLK 13 // SCL sp,interface
#define OLED_MOSI 11 // SDA sp,interface
#define OLED_RESET 9
#define BLACK 0x0000

// Skapa objekt för OLED displayen och BMP280 sensorn
Adafruit_SSD1331 display(OLED_DC, OLED_CS, OLED_CLK, OLED_MOSI, OLED_RESET);
Adafruit_BMP280 bmp;

void setup() {
// Initiera seriell kommunikation och OLED displayen
Serial.begin(9600);
display.begin(); // Initiera OLED displayen
display.setRotation(1); // Välj displayrotation (0, 1, 2 eller 3)
display.fillScreen(BLACK); // Rensa displayen

// Initiera BMP280 sensorn
if (bmp.getStatus() == 0) {
Serial.println("Kunde inte hitta BMP280 sensorn!");
Serial.println("Error: " + String(bmp.getStatus()));

while (true);  // Fastna i en oändlig loop om sensorn inte hittas

}
}

void loop() {
// Läs värden från BMP280 sensorn
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure();
float altitude = bmp.readAltitude();

// Skriv ut värdena på seriell monitor
Serial.print("Temperatur: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Lufttryck: ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Höjd: ");
Serial.print(altitude);
Serial.println(" m");
Serial.println(); // Tom rad för att få lite mellanrum i ut
}

Yet again, no code tags

Why not ?

And you forgot to tell us how you fixed it.
{bin}

Thanks for the support i appreciate it.
I missed semicolon in the code}
Saved the sketch local in desktop that took away some of the bugs.

I cleaned the code and streamlined it.
Now it runs.

Den fre 6 jan. 2023 16:04SemperIdem via Arduino Forum <notifications@arduino.discoursemail.com> skrev:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.