OLED 128x64 and Adafruit

Hello all!

I am beginner with arduino and I am trying to put together sort of a weather station using two sensors and an OLED display.

I got it to work as I want except that the OLED is showing some gibberish in the very bottom, in addition to the useful information that I want. After tinkering with the code, it appears to me that, somehow, that gibberish is related to the sections of code not related to display.print. As I comment our some sections, the gibberish section gets shorter or longer, depending on how much I comment out.

I am using the libraries Adafruit_GFX.h and Adafruit_SSD1306.h.

Here is my code (pardon the mess, I am not good with it):

/*Humidity and Temperature Sketch
  11.25.2019
*/
#include <Wire.h>
#include <math.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define DHTPIN 2 // pin 2 as output 
#define DHTTYPE DHT22 // sensor model 
#define LEDPIN 3
// OLED display TWI address
#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(-1);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


Adafruit_BMP280 bmp; // I2C

//Define Variables
float phi; // variable for humidity
float temp; // variable for temperature
float temperaturec; // variable for temperature in C
float temperatureF; 
float Temp; // 
float TempR; // absolute temp in R
float Twb; // Wet Bulb Temp
float LogPws;
float Pws;
float Pw; // partial pressure of water vapor
float Ws; // Saturation Humidity Ratio 
float mu; // Degree of saturation
float W; // Humidity Ratio
float P; // Pressure 
float Td; // Dew Point 
float H; // Enthalpy of Air 
 
// Setup of sensor at the arduino cloeck speed

DHT dht(DHTPIN, DHTTYPE);


void setup() {
  Wire.begin();
  
  Serial.begin(9600);

  Serial.println(F("BMP280 test"));

  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  // Initialize DHT22 Sensor

  dht.begin();

  pinMode(LEDPIN, OUTPUT);

  // initialize and clear display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  

}

void calculations(){
//Time for sensor to stabilize

  delay(5000);

  phi = dht.readHumidity(); // get humidity
  temp = dht.readTemperature(true); // get temperature
  temperaturec = bmp.readTemperature();
  temperatureF = temperaturec*(9*0.2)+32;
  Temp = (temp + temperatureF)/2; // average temp from 2 sensors 
  TempR = Temp+459.67;
  P = bmp.readPressure()*0.000145037738;

  if (Temp <= 32){
    LogPws = ((-1.0214165*pow(10,4))/TempR)+(-4.8932428*pow(10,0))+(-5.3765794*pow(10,-3)*TempR)+(1.9202377*pow(10,-7)*pow(TempR,2))+(3.5575832*pow(10,-7)*pow(TempR,3))+(-9.0344688*pow(10,-14)*pow(TempR,4))+(4.1635019*pow(10,0)*log(TempR));
    Pws = exp(LogPws);
  }
  else{
    LogPws = ((-1.0440397*pow(10,4))/TempR)+(-1.1294650*pow(10,1))+(-2.7022355*pow(10,-2)*TempR)+(1.2890360*pow(10,-5)*pow(TempR,2))+(-2.4780681*pow(10,-9)*pow(TempR,3))+(6.5459673*log(TempR));
    Pws = exp(LogPws); 
  }

  // Partial Pressure Water Vapor
  
  Pw = (phi/100)*Pws;

  // Humidity Ratio 
  
  W = 0.621945*(Pw/(P-Pw));

  // Wet Bulb Temperature 

  Twb = (temperaturec*atan(0.151977*pow((phi+8.313659),0.5))+atan(temperaturec+phi)-atan(phi-1.676331)+0.00391838*pow(phi,1.5)*atan(0.023101*phi)-4.686035)*1.8+32;

  // Dew Point 

  if (Temp < 32){
      Td = 90.12+26.142*log(Pw)+0.8927*pow(log(Pw),2);
  }

  else {
    Td = 100.45+33.193*log(Pw)+2.319*pow(log(Pw),2)+0.17074*pow(log(Pw),3)+1.2063*pow(Pw,0.1984);
  }
  
  // Enthalpy 

  H = 0.240*Temp + W*(1061 + 0.444*Temp);
  
}

void displayData(){
  /*
  Serial.print("Humidity:");
  Serial.print(phi);
  Serial.print("\t");
  Serial.print("Temperature:");
  Serial.print(Temp);
  Serial.print(" *F");
  Serial.print("\t");
  Serial.print(F("Wet Bulb Temp = "));
  Serial.print(Twb,4);
  Serial.print(" *F");
  Serial.print("\t");
  Serial.print(F("Dew Point = "));
  Serial.print(Td,4);
  Serial.print(" *F");
  Serial.print("\t");
  
  Serial.print(F("LNPws = "));
  Serial.print(LogPws);
  Serial.print("");
  Serial.print("\t\t");
  Serial.print(F("Pws = "));
  Serial.print(Pws,4);
  Serial.print(" psi");
  Serial.print("\t\t");
  
  
  Serial.print(F("Humidity Ratio = "));
  Serial.print(W,4);
  Serial.print(" lbsh2o/lbsda");
  Serial.print("\t");
  Serial.print(F("Enthalpy = "));
  Serial.print(H,4);
  Serial.print(" BTU/lbda");
  Serial.print("\t");
  
  Serial.print(F("Pressure = "));
  Serial.print(bmp.readPressure());
  Serial.print(" Pa");
  Serial.print("\t\t");
  
  
  Serial.print(F("Approx altitude = "));
  Serial.print(bmp.readAltitude(1012.19)*3.28084); /* Adjusted to local forecast! */
 // Serial.print(" ft");
  //Serial.println();
  
  
  
  
  //display.drawPixel(0, 0, WHITE);
  //display.drawPixel(127, 0, WHITE);
  //display.drawPixel(0, 63, WHITE);
  //display.drawPixel(127, 63, WHITE);
  
  //display data as text 
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(30,1);
  display.print("PSYCHO!");
  display.setCursor(2, 18);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("Tdb:");
  display.print(Temp);
  display.print(" F db");
  display.setCursor(2, 27);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("Twb:");
  display.print(Twb);
  display.print(" F wb");
  display.setCursor(2, 36);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("RH:");
  display.print(phi);
  display.print("%");
  display.setCursor(2, 45);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("Hum:");
  display.print(W, 3);
  display.print(" Lb/Lb");
  display.setCursor(2, 54);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("DP:");
  display.print(Td, 2);
  display.print(" F");
  
  }

void loop() {
  
  // Repeat Calculations
  calculations();
  
  displayData();
   // update display with all of the above graphics
  display.display();
  delay(5000);
  display.clearDisplay();

   
  /*
  Conditions for visual cues - LED
  
  
  if (temperatureF < 65) {
    digitalWrite(LEDPIN, HIGH);
  }
  else {
    digitalWrite(LEDPIN, LOW);
  }
  */
}

And I am attaching a picture of what I mean by the gibberish.

Any ideas on how to get rid of it? Many thanks in advance!

Hi Joel,
I get the impression that the sensor readings are only done once - the instructions are in the Setup section which is by design executed only once.

The Loop part contains only the display.display () function which means that the display is refreshed and refreshed and refreshed without the sensor being read and its new values displayed (you can test this by warming the DHT22 with your fingers or by breathing on the equipment). With some static or electric irregularities in your sensors or wiring the gibberish is produced.

A step towards solution is to copy the instructions for reading the sensor into the Loop section, before the display.display() call.
Success !