Adafruit is open and shipping! Let's build back better, together!
0

Timing issues with OLED library startup
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Timing issues with OLED library startup

by jdl on Sun Dec 27, 2020 12:04 pm

It seems like using Adafruit_Keypad and Adafruit_SSD1306 in the same program is giving me hard-to-debug timing issues at startup. I've checked that the display and the keypad examples work individually, so I'm pretty sure it's not a hardware issue. (But never say never...)

Basically, if I comment out the line instantiating the Adafruit_Keypad, the sketch starts the display as normal and prints a timestamp to serial every 2 sec. If I uncomment the delay(2000); line before display.begin, it also starts and gets to the loop, though I see some "⸮⸮⸮⸮⸮⸮⸮⸮" printed to serial at startup. Adjusting the delay time or rearranging the order of things in setup() can make the issue come and go.

Sometimes, just changing that delay value or adding additional Serial.printlns makes the board un-flashable (without double-pressing reset). I'd like to see if there's a "correct" way to use these together, to avoid encountering these issues. I noticed someone else had mentioned reset delays with the SSD1306 board here: viewtopic.php?f=47&t=172124a&p=840479&hilit=SSD1306#p840207

Here's my minimal reproducing example:

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include "Adafruit_Keypad.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
//In order of breakout board:
//CS -> D13
//RST -> D12
//D/C -> D11
//CLK -> SCK
//DATA -> MOSI
//VIN -> 5V
//3.3Vo -> none
//GND -> GND
#define OLED_CS     13
#define OLED_RESET  12
#define OLED_DC     11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
                         &SPI, OLED_DC, OLED_RESET, OLED_CS);

// Keypad
const byte ROWS = 4; // rows
const byte COLS = 4; // columns
//define the symbols on the buttons of the keypads
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte colPins[COLS] = {10, 9, 7, 5}; //connect to the first 4 pins on keypad board L->R (not counting first and last pad)
byte rowPins[ROWS] = {0, 4, 6, 8}; //connect to the last 4 pins (not counting first and last pad)

//// Initialize Adafruit_Keypad class
Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  //delay(2000);
  Serial.println("Starting up");
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

}

void loop() {
  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  Serial.println(millis());
}


I've also attached a fritzing image of the connections as well.
Attachments
uv sensor_bb.png
uv sensor_bb.png (190.15 KiB) Viewed 112 times

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit_support_carter on Wed Jan 06, 2021 3:40 pm

Could it be related to the first FAQ here?
https://learn.adafruit.com/monochrome-o ... shooting-2

adafruit_support_carter
 
Posts: 18117
Joined: Tue Nov 29, 2016 2:45 pm

Re: Timing issues with OLED library startup

by jdl on Wed Jan 06, 2021 3:47 pm

It could be, but the example code doesn't seem to need any delay; it works fine. The amount of delay needed in the code I attached changes in hard to understand ways with minor changes. In other words, I'll have something working, then add a serial print somewhere, and it won't work any more. It's also breaking my ability to even re-flash the controller without putting it in 'recovery mode', which is not mentioned in the FAQ item.

I was hoping someone could explain how to make this work reliably. The example code works fine and requires no additional delay. Can I trigger the reset myself in software? Does the library do that when I call begin()?

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by jdl on Wed Jan 06, 2021 3:49 pm

Oh, also, from the FAQ answer: "It will work on reset since that typically does not cycle power."

This doesn't seem to be the case for my code and circuit.

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit_support_carter on Wed Jan 06, 2021 3:54 pm

Nothing obvious. This behavior seems odd:
Basically, if I comment out the line instantiating the Adafruit_Keypad, the sketch starts the display as normal and prints a timestamp to serial every 2 sec

There isn't much that happens in the constructor:
https://github.com/adafruit/Adafruit_Ke ... ad.cpp#L26

Does it work normally once it gets passed the printing of the "⸮⸮⸮⸮⸮⸮⸮⸮" in serial monitor?

adafruit_support_carter
 
Posts: 18117
Joined: Tue Nov 29, 2016 2:45 pm

Re: Timing issues with OLED library startup

by jdl on Wed Jan 06, 2021 4:00 pm

I agree, it seems like the issue is really in something other than the code I have there. I seem to remember that the software running on the arduino includes some amount of additional functionality that 'wraps' the sketch code. I'm not sure what happens in there, but it seems like there's something race-y in the initial startup that I can trigger with this code and circuit.

If you have access to the hardware in question, could you try reproducing it? I wonder if there's a subtle hardware defect masquerading as a software problem.

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit_support_carter on Wed Jan 06, 2021 7:00 pm

Please post a photo of your actual setup. We'll take a look and see if there's anything obvious with the hardware. And if we test, it will help us match up better with your setup.

adafruit_support_carter
 
Posts: 18117
Joined: Tue Nov 29, 2016 2:45 pm

Re: Timing issues with OLED library startup

by jdl on Thu Jan 07, 2021 11:27 am

IMG_0399.jpeg
IMG_0399.jpeg (115.31 KiB) Viewed 57 times
IMG_0400.jpeg
IMG_0400.jpeg (124.6 KiB) Viewed 57 times
IMG_0401.jpeg
IMG_0401.jpeg (92.55 KiB) Viewed 57 times

Here's updated code that shows the UV sensor reading on the OLED display.

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include "Adafruit_Keypad.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_VEML6070.h"

// display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
//In order of breakout board:
//CS -> D13
//RST -> D12
//D/C -> D11
//CLK -> SCK
//DATA -> MOSI
//VIN -> 5V
//3.3Vo -> none
//GND -> GND
#define OLED_CS     13
#define OLED_RESET  12
#define OLED_DC     11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
                         &SPI, OLED_DC, OLED_RESET, OLED_CS);

// Keypad
const byte ROWS = 4; // rows
const byte COLS = 4; // columns
//define the symbols on the buttons of the keypads
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte colPins[COLS] = {10, 9, 7, 5}; //connect to the first 4 pins on keypad board L->R (not counting first and last pad)
byte rowPins[ROWS] = {0, 4, 6, 8}; //connect to the last 4 pins (not counting first and last pad)

// Initialize Adafruit_Keypad class
//Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

Adafruit_VEML6070 uv = Adafruit_VEML6070();

void setup() {
  Serial.begin(9600);
  delay(500);
  Serial.println("Starting up");
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  uv.begin(VEML6070_1_T);  // pass in the integration time constant
}

void loop() {
  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(0, 0, SSD1306_WHITE);
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10, 0);
  display.println(uv.readUV());

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  Serial.print(F("UV light level: ")); Serial.println(uv.readUV());
  Serial.println(millis());
}


If I uncomment the line "//Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);", I am no longer able to flash the board. It shows the following.

Code: Select all | TOGGLE FULL SIZE
Arduino: 1.8.13 (Mac OS X), Board: "Adafruit ItsyBitsy 32u4 5V 16MHz"

Sketch uses 16262 bytes (56%) of program storage space. Maximum is 28672 bytes.
Global variables use 492 bytes of dynamic memory.
avrdude: ser_drain(): read error: Device not configured
avrdude: ser_drain(): read error: Device not configured
avrdude: ser_send(): write error: Device not configured
avrdude: ser_recv(): read error: Device not configured
avrdude: butterfly_recv(): programmer is not responding

avrdude: ser_recv(): read error: Device not configured
avrdude: butterfly_recv(): programmer is not responding
avrdude: ser_drain(): read error: Device not configured
avrdude: ser_send(): write error: Device not configured
avrdude: ser_recv(): read error: Device not configured
avrdude: butterfly_recv(): programmer is not responding
avrdude: ser_send(): write error: Device not configured
avrdude: ser_recv(): read error: Device not configured
avrdude: butterfly_recv(): programmer is not responding
avrdude: ser_recv(): read error: Device not configured
the selected serial port avrdude: butterfly_recv(): programmer is not responding
 does not exist or your board is not connected
avrdude: butterfly_recv(): programmer is not responding
avrdude: ser_send(): write error: Device not configured
avrdude: ser_recv(): read error: Device not configured
avrdude: butterfly_recv(): programmer is not responding
Found programmer: Id = ""; type =

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by jdl on Thu Jan 07, 2021 11:28 am

IMG_0403.jpeg
IMG_0403.jpeg (119.37 KiB) Viewed 57 times
IMG_0402.jpeg
IMG_0402.jpeg (86.95 KiB) Viewed 57 times

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit_support_carter on Thu Jan 07, 2021 1:55 pm

You know, this may just be RAM issue with the 32u4. The individual pieces are working, but not enough for all of them together. Is there any special reason you are using the Itsy 32u4?

adafruit_support_carter
 
Posts: 18117
Joined: Tue Nov 29, 2016 2:45 pm

Re: Timing issues with OLED library startup

by jdl on Thu Jan 07, 2021 2:14 pm

The totals after compiling seem low enough to be within the 2.5kB of RAM, if I'm interpreting this correctly:

Sketch uses 16262 bytes (56%) of program storage space. Maximum is 28672 bytes.
Global variables use 492 bytes of dynamic memory.

I think that 492 bytes must not include the 512 bytes for the display buffer, but that still doesn't get to 2.5kB...

I'm inspired by (but not trying to exactly recreate) this: https://apenasimagens.com/en/solar-uv-e ... e-project/

I chose the Itsy Bitsy 32u4 because it was roughly comparable (number of pins, available in 5V), but had micro-USB for programming on-board. Comparing the ATmega32U4 (https://www.microchip.com/wwwproducts/en/ATmega32U4) to the ATmega328 (https://www.microchip.com/wwwproducts/en/ATmega328) used by that author, it looks like I should have *more* than enough RAM...

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit2 on Thu Jan 07, 2021 3:59 pm

i dont trust that number, there's other dynamic allocations...
either way, its very likley something happening with USB/memory corruption. what is it? we dont know for sure, its one of the reasons we're not huge fans of C usage for microcontroller projects ... you could try another keypad library

adafruit2
Site Admin
 
Posts: 19629
Joined: Fri Mar 11, 2005 7:36 pm

Re: Timing issues with OLED library startup

by jdl on Thu Jan 07, 2021 4:14 pm

Is there anything else you need from me to get to the bottom of this issue?

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Re: Timing issues with OLED library startup

by adafruit2 on Thu Jan 07, 2021 4:20 pm

we wouldnt be able to dedicate any time to debugging this project, you can open an issue on github if youd like!

adafruit2
Site Admin
 
Posts: 19629
Joined: Fri Mar 11, 2005 7:36 pm

Re: Timing issues with OLED library startup

by jdl on Thu Jan 07, 2021 4:27 pm

I have put non-negligible effort over multiple days into paring down the example code to a minimal reproducing example, waited patiently for the post to even be acknowledged, provided everything you have requested, and am no closer to a solution. I bought these particular components in part because they had first-party library support. I understand you're not interested in debugging your own libraries, but that doesn't mean I'm interested in doing so.

I clearly made a mistake in purchasing these components from Adafruit. I'm so disappointed.

jdl
 
Posts: 17
Joined: Wed Dec 31, 2014 9:00 pm

Please be positive and constructive with your questions and comments.