I'm unable to test this now, but here goes:
Turns out I did have the ability to test it (found an extra display module) and this does indeed explain the problem. The only other change I needed to make to your code in order to replicate the problem was change the display's I2C address to 0x3C
. Adafruit GFX Library v1.10.13 and Adafruit SSD1306 version v2.5.1.
From comments on the question:
I added #define SSD1306_LCDHEIGHT 64
before line #53 to remove the
compilation error
Yeah, you can't do that. Under the old method of defining the screen dimensions, you are intended to modify the header itself:
// ONE of the following three lines must be #defined:
//#define SSD1306_128_64 ///< DEPRECTAED: old way to specify 128x64 screen
#define SSD1306_128_32 ///< DEPRECATED: old way to specify 128x32 screen
//#define SSD1306_96_16 ///< DEPRECATED: old way to specify 96x16 screen
// This establishes the screen dimensions in old Adafruit_SSD1306 sketches
// (NEW CODE SHOULD IGNORE THIS, USE THE CONSTRUCTORS THAT ACCEPT WIDTH
// AND HEIGHT ARGUMENTS).
You can see by default it is using a screen with dimensions 128x32. Later in the header this is used to set SSD1306_LCDHEIGHT
to 32
in the header.
#if defined SSD1306_128_32
#define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_32 defined
#define SSD1306_LCDHEIGHT 32 ///< DEPRECATED: height w/SSD1306_128_32 defined
#endif
What you have done in your code is redefine SSD1306_LCDHEIGHT
to 64
from the point where you issued the #define
downward, which only affects the code compiled in your sketch.
Meanwhile, over in Adafruit_SSD1306.cpp, SSD1306_LCDHEIGHT
is still 32
, because this file is compiled independently of your .ino and is unaware of your own re#define
. In other words, part of the code you've put into your Arduino thinks it has 32 lines and part of it thinks it has 64 lines.
So, the solution would be get rid of the #define
that you inserted into the sketch and to modify header to read:
#define SSD1306_128_64 ///< DEPRECTAED: old way to specify 128x64 screen
//#define SSD1306_128_32 ///< DEPRECATED: old way to specify 128x32 screen
//#define SSD1306_96_16 ///< DEPRECATED: old way to specify 96x16 screen
Or to use the new method of supplying the dimensions by way of the constructor as is done in newer examples:
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
/// ...
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
The Adafruit SSD1306 version v2.5.1's ssd1306_128x64_i2c.ino example that uses this newer method of passing screen dimensions into the Adafruit_SSD1306
constructor and compiled and ran without modification in testing here.