OLED display lag

Hello everyone i’m very new to Arduino and Blynk and i need to do a weather station project with Blynk. So far i have made all the sensors work and display the values on a OLED 0.96" screen. For the time being i am testing everything without starting Blynk, i want to just display the values on the screen for the time being. The values are being displayed on the screen, however i have made the timer.setInterval(1000L) and the OLED screen is being refreshed every second for like 4 seconds and then it starts to lag, after those 4 seconds the display is being refreshed every 7 seconds or so.

#include <Wire.h>
#include <SPI.h>
#include <BH1750FVI.h> // sviesos jutiklio biblioteka
#include <Adafruit_BME280.h>  // BME280 biblioteka
#include <avr/dtostrf.h>  // biblioteka naudojama paversti išmatuotus duomenis į skaitmenis
#include <ss_oled.h> //OLED ekrano biblioteka

#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>


char auth[] = "Blynkprojektokodas";
char ssid[] = "WiFipavadinimas";
char pass[] = "WiFislaptazodis";

Adafruit_BME280 bme; // naudoja I2C sasaja
Adafruit_Sensor *bme_temp = bme.getTemperatureSensor();
Adafruit_Sensor *bme_slegis = bme.getPressureSensor();
Adafruit_Sensor *bme_dregme = bme.getHumiditySensor();
BH1750FVI LightSensor(BH1750FVI::k_DevModeContHighRes);

#define SDA_PIN 5
#define SCL_PIN 4
#define RESET_PIN -1
#define OLED_ADDR -1
#define FLIP180 0
#define INVERT 0
#define USE_HW_I2C 0
#define MY_OLED OLED_128x64
#define OLED_WIDTH 128
#define OLED_HEIGHT 64

SSOLED ssoled; // 

BlynkTimer timer;  // 
void setup() {
  if (!bme.begin(0x76)) {  // jutiklio aptikimas ir paleidimas
    while (1) delay(10);
}
  bme_temp->printSensorDetails();
  bme_slegis->printSensorDetails();
  bme_dregme->printSensorDetails();
  oledInit(&ssoled, MY_OLED, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, SDA_PIN, SCL_PIN, RESET_PIN, 400000L);
  oledFill(&ssoled, 0x0, 1);
  //Blynk.begin(auth, ssid, pass); 
  timer.setInterval(1000L, myupdate);  // atnaujina duomenis kas 1 sekunde
  myupdate();
  {
    LightSensor.begin(); // sviesos jutiklio paleidimas
    
  }
}


void loop() {
  Blynk.run();   
   timer.run();
   
}
// atnaujina OLED ekrano duomenis ir siuncia duomenis i Blynk
void myupdate(){
  sensors_event_t temp_event, pressure_event, humidity_event;
  bme_temp->getEvent(&temp_event);
  bme_slegis->getEvent(&pressure_event);
  bme_dregme->getEvent(&humidity_event);
  float ctemp = temp_event.temperature;  // temperaturos ismatavimas
  float drgm = humidity_event.relative_humidity;  // dregmes ismatavimas
  float sleg = pressure_event.pressure; // slegio ismatavimas
  float lux = LightSensor.GetLightIntensity(); // sviesos ismatavimas
   
  char manotemp[8]; 
  dtostrf(ctemp, 6, 2, manotemp); // pavercia temperaturos duomenis i skaitmenis
  char manodrgm[8]; 
  dtostrf(drgm, 6, 2, manodrgm); // pavercia dregmes duomenis i skaitmenis
  char manosleg[8]; 
  dtostrf(sleg, 6, 2, manosleg); // pavercia slegio duomenis i skaitmenis
  char manolux[8];
  dtostrf(lux, 6, 0, manolux); // pavercia sviesos stiprumo duomenis i skaitmenis
  // OLED ekrano duomenu atnaujinimas
  oledWriteString(&ssoled, 0,2,1,(char *)"Apsv=", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,40,1,(char *)manolux, FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,95,1,(char *)"lux", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,2,3,(char *)"Temp=", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,50,3,(char *)manotemp, FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,105,3,(char *)"C", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,2,5,(char *)"Dregm=", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,50,5,(char *)manodrgm, FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,105,5,(char *)"%", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,2,7,(char *)"Sleg=", FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,50,7,(char *)manosleg, FONT_NORMAL, 0, 1);
  oledWriteString(&ssoled, 0,105,7,(char *)"hPa", FONT_NORMAL, 0, 1);
  // duomenys siunciami i Blynk
  Blynk.virtualWrite(V3, ctemp);
  Blynk.virtualWrite(V4, drgm); 
  Blynk.virtualWrite(V5, sleg);
}

Do i need to change something in the code or it is just not possible to refresh the values every second?

According to the BME280 datasheet, it looks like the humidity-part of the sensor needs 1 sec to respond. Then you have all the other stuff in your myupdate timer that takes time to process and everything starts to lag after a few cycles. That’s my guess! Set the timer to once every 10 seconds or more.

Unless you take showers with your weather station, there won’t be any significant weather changes during a 10 seconds period. :wink: :stuck_out_tongue_winking_eye:

Datasheet:

https://www.mouser.com/datasheet/2/783/BST-BME280-DS002-1509607.pdf

1 Like

I mainly wanted to make the refresh 1 sec for the light sensor, so it could up the light right away, but ye i guess it’s fine if it takes a bit longer :smile:

You could always use two timers. One for the BME at 10-20 seconds interval, and one for the BH1750 at 2 seconds or so… I’m kinda allergic to 1 sec timers with Blynk because experience have taught me that they are prone to fail. But your light sensor seems to be fairly quick, so 1 sec might work. I guess it depends on how long it takes for the OLED to redraw.

https://www.mouser.com/datasheet/2/348/bh1750fvi-e-186247.pdf

Let us know how it plays out! :slight_smile:

I used two timers as you said and added a myupdate2 just for the light sensor, like this:

 timer.setInterval(10000L, myupdate);  // atnaujina duomenis kas 1 sekunde
  myupdate();
  timer.setInterval(1000L, myupdate2);
  myupdate2();

And it’s the same situation, it works for like a couple of seconds and the it starts to lag again and refresh every value simultaneously every 7 seconds. I’m lost to what could be causing this. The OLED screen looks capable to refresh the values every second for like 3 seconds :joy: and then is starts to lag.

Increase the timings to 10 seconds and 60 seconds. Does it make a difference?

Pete.

Yes increasing the timings kinda works. Temp, humidity and pressure are being updated every 60 seconds and the light sensor reading is being updated like every 5 seconds.

I assume that’s with 5 and 60 second timers rather than the 10 and 6 seconds I suggested?

Pete.

It’s with the 10 and 60 seconds, the BME readings are being updated every 60 seconds as described, but for some reason the light sensor reading is updated every 5 seconds instead of 10.

I’d suggest that you review your code, and maybe add-in some serial prints to get a better understanding of what is happening in the program flow.
If you have a timer calling a function every 10 seconds then it cant mysteriously be getting called every 5 seconds.

Pete.