SSD1306 oled trash pixels

This is my SSD1306 oled it's showing trash pixels in the corner of the screen. Is there any way to fix it ?



The two pictures show that the screen stops showing the trash for a few seconds but then shows the trash.
Also the Adafruit (i am using Adafruit SSD1306 library) oled demo test example works fine. I've also noticed that the pixels on the corner change when other stuff updates on the screen , so i'm shure they are ASCII or other data forced on the screen.
Also here's the sketch :point_right:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>

const int SensorDataPin = 2;
const int DHT22Pin = 3;

OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
DHT dht(DHT22Pin, DHT22);

Adafruit_SSD1306 display(128, 64, &Wire, -1);

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

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }

  delay(1);
  display.clearDisplay();
  display.setTextColor(WHITE);
  dht.begin();
}

void clearLastLine() {
  display.setTextSize(1);
  display.setCursor(0, 60);
  display.print(""); // Clear the last line
}

void displayTemperatureAndHumidity(float waterTemp, float airTemp, float humidity) {
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Water Temperature: ");
  display.setTextSize(2);
  display.setCursor(0, 10);
  display.print(waterTemp, 1);  // Use print with precision to avoid extra decimal places
  display.print(" C");

  display.setTextSize(1);
  display.setCursor(0, 30);
  display.print("Air Temperature & RH: ");
  display.setTextSize(1);
  display.setCursor(0, 40);
  display.print(airTemp, 1);  // Use print with precision to avoid extra decimal places
  display.print(" C");

  display.setTextSize(1);
  display.setCursor(0, 50);
  display.print(humidity, 1);  // Use print with precision to avoid extra decimal places
  display.print("%");

  // Scrolling long text
  String longText = "";
  int charWidth = 6;  // Assuming a fixed width for characters (adjust as needed)
  int textWidth = display.getCursorX() + (longText.length() * charWidth);

  if (textWidth > display.width()) {
    static int scrollPosition = 0;
    display.setTextSize(1);
    display.setCursor(0 - scrollPosition, 60);
    display.print(longText);
    scrollPosition = (scrollPosition + 1) % textWidth;
  } else {
    display.setTextSize(1);
    display.setCursor(0, 60);
    display.print(longText);
  }
}

void loop() {
  display.clearDisplay();

  sensors.requestTemperatures();
  float waterTemperature_Celsius = sensors.getTempCByIndex(0);
  float airHumidity = dht.readHumidity();
  float airTemperature_Celsius = dht.readTemperature();

  Serial.print("Water Temperature = ");
  Serial.print(waterTemperature_Celsius);
  Serial.println(" C");
  Serial.print("Air Humidity = ");
  Serial.print(airHumidity);
  Serial.println(" %");
  Serial.print("Air Temperature = ");
  Serial.print(airTemperature_Celsius);
  Serial.println(" C");

  displayTemperatureAndHumidity(waterTemperature_Celsius, airTemperature_Celsius, airHumidity);

  clearLastLine();
  display.display();

  // Adjust the delay or use non-blocking delay methods based on your requirements
  delay(1);
}
`#include <Wire.h>            
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>

const int SensorDataPin = 2; // DS18B20 Pin 
const int DHT22Pin = 3; // DHT22 Pin 

OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
DHT dht(DHT22Pin, DHT22); 

Adafruit_SSD1306 display(128, 64, &Wire, -1); // Display size  
void setup() {
  Serial.begin(9600); // Serial monitor baud rate 

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Oled addres (change to actual oled addres for project to work)
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }

  delay(1);
  display.clearDisplay();
  display.setTextColor(WHITE); // Text color (may not change the real oled color if oled does not support feature)
  dht.begin();


}
 // Water temperature display 
void displayTemperatureAndHumidity(float waterTemp, float airTemp, float humidity) {
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print(F("Water Temperature: ")); 
  display.setTextSize(2);
  display.setCursor(0, 10);

  char waterTempStr[10];
  dtostrf(waterTemp, 4, 1, waterTempStr);
  display.print(waterTempStr);
  display.print(F(" C"));
// Air temperature & RH display 
  display.setTextSize(1);
  display.setCursor(0, 30);
  display.print(F("Air Temperature & RH: "));
  display.setTextSize(1);
  display.setCursor(0, 40);

  char airTempStr[10];
  dtostrf(airTemp, 4, 1, airTempStr);
  display.print(airTempStr);
  display.print(F(" C"));

  display.setTextSize(1);
  display.setCursor(0, 50);

  char humidityStr[10];
  dtostrf(humidity, 4, 1, humidityStr);
  display.print(humidityStr);
  display.print(F("%"));

}

void loop() {
  display.clearDisplay();

  sensors.requestTemperatures();
  float waterTemperature_Celsius = sensors.getTempCByIndex(0);
  float airHumidity = dht.readHumidity();
  float airTemperature_Celsius = dht.readTemperature();
// Serial print of all sensor readings 
  Serial.print(F("Water Temperature = "));
  Serial.print(waterTemperature_Celsius);
  Serial.println(F(" C"));
  Serial.print(F("Air Humidity = "));
  Serial.print(airHumidity);
  Serial.println(F(" %"));
  Serial.print(F("Air Temperature = "));
  Serial.print(airTemperature_Celsius);
  Serial.println(F(" C"));

  display.display()`

This is the new and updated sketch!

You need to show your sketch. You want answers, not guesses.

Ok shure i'l do that now

If the dots change while the program is running, that is a sign of memory problems, like writing outside of array bounds, using String objects, etc.

DO NOT use Strings. They poke holes in memory and eventually cause AVR-based Arduinos to crash. Use C-strings (zero terminated character arrays) instead.

  String longText = "";

I would be very surprised if that code does anything at all other than use up CPU cycles. I don't see how it could possibly clear anything.

Hey there

had the same issue on (i believe) the same display. @jremington gave the proper hint, the issue is with memory (more specifically RAM vs. flash) usage.

my solution was to enclose all strings in my Serial.print()/println() and display.print()/println() calls with the F() macro - which keeps strings in flash. it does have some drawbacks tho, two identical f-macroed strings will end up in 2 instances of the string in flash, for example. i noticed in your code you did it in some places, not in others.

the F() macro is described halfway down PROGMEM - Arduino Reference

i can now use the display in 128x64 pixel resolution with the Adafruit_SSD1306 library without a garbled last line.

optionally you can use the absolutely powerful u8g2 library but it's a lot more complex, at least for my brain...

btw if you want to use the degree symbol: display.write((char)247); does it for me if i dont use the cp437 font.

hth

1 Like

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