Ssd1306 oled display data logger project

Hello guys, i'm trying to make an arduino Temperature and humidity data logger and i am facing some problems of Ssd1306 oled display 128x32 in the down right side


#include <Wire.h>
#include <SPI.h>  
#include <SD.h>               // Include SD library
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>                         // Include DHT library code
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 #define B1      8            // Button B1 is connected to Arduino pin A1
#define B2      9  
#define DHTPIN  2                        // DHT11 data pin is connected to Arduino pin 8
#define DHTTYPE DHT22                   // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE);  
File dataLog;
boolean sd_ok = 0;// Configure DHT library
    
char temperature[] = "00.0 C";
char humidity[]    = "00.0 %";
char Time[]     = "  :  :  ";
char Calendar[] = "  /  /20  ";
byte i, second, minute, hour, date, month, year, previous_second;
 
void setup(void) {
  Serial.begin(9600);
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64) 
  Wire.begin();
  dht.begin();                // Initialize the DHT library
  delay(1000);
  display.clearDisplay();
  display.drawFastHLine(0, 32, SSD1306_LCDWIDTH, WHITE);
  display.setTextSize(1);
  display.setTextColor(WHITE, BLACK);
  display.setCursor(0, 0);
  display.println(F("TIME:"));
  display.setCursor(0, 8);
  display.println(F("DATE:"));
  display.setCursor(0, 16);
  display.println(F("TEMP:"));
  display.setCursor(0, 24);
  display.println(F("RH:"));
  display.display();
  
}
 
void loop() {
  
  byte RH = dht.readHumidity();
  byte Temp = dht.readTemperature();
  temperature[0] = Temp / 10 + 48;
  temperature[1] = Temp % 10 + 48;
  humidity[0]    = RH / 10 + 48;
  humidity[1]    = RH % 10 + 48;
  display.setCursor(46,16);
  display.print(temperature);
  display.setCursor(46, 24);
  display.print(humidity);
  display.display();
  delay(1000);
}

Thanks in advance

I'm sorry I can't help you with the Adafruit library. However if you are using text only you might try this library by Bill Greiman
Its much smaller and works great on my 1.3" OLED board.

Try one of the examples included with the Adafruit_SSD1306 library.

I think you should specify display dimensions in this line:
Adafruit_SSD1306 display(OLED_RESET);

Hi Hisham,
a) first, I think that you'd better start by a good reading of data :
float Temp = dht.readTemperature();
// because direct float-> byte conversion as you do looses precision
float RH = dht.readHumidity();

b) Then you need to convert your floats into a char string to display them :
double _temp = +15.5; // or float if you prefer
char s_temp[ 8 ]; // arduino's implementation of sprintf can't handle floats and doubles
dtostrf( _temp, 4, 2, s_temp ); // so we convert double to a char string with dtostrf()

c) At this point you can use display.print(char *) to display your data

d) Last, most of the time, when a char garbage is displayed AFTER a string value, it is because the string is not correctly ended by a trailing '0' char, and that can occur if the lenght of the string we allocated in RAM is too short and we write after its end.
so you can try
char s_temp[enougth_space];
memset(s_temp, 0, enought_space);
to be sure.
if it can help you,

Which arduino are you using? You are likely running out of dynamic memory (RAM), the adafruit display library does not allocate the display buffer at compile time so the compiler will not warn you of low memory. Using a display and SD library at the same time on an UNO or other arduino with only 2K of ram frequently runs into memory problems.

Have a look at the U8g2 library (can be installed from the library manager), it has options for using a page buffer, which is much smaller than the full display buffer used by Adafruit's library, as well as a text-only display that does not need a buffer.

Although the image is not clear, I think you are driving a 128x64 set as a 128x32. Hence you will get the striped text look and corruption for say, graphics.
When properly driving it as a 128x64, the text will be clear and crisp

I have used that library and this initiator works:
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

You can see that you can set the resolution there.
Some other libraries you have to make that change in the library file itself before compiling.

Our website has a sample usage of this (excellent) library (in the download section)

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