Adafruit SSD1306 having problem with FreeRTOS

Hi everyone,

I am trying to display some text on OLED display but unable to do so. I am integrating the adafruit ssd1306 library with freeRTOS to do multitasking but seems like the code stucks somewhere.

I am attaching the code as well for your reference.

#include <Arduino_FreeRTOS.h>
#include <avr/io.h>
#include <SPI.h>
#include <Adafruit_GFX.h>           //Includes core graphics library
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
static Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
    Serial.begin(115200);
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
    }

 xTaskCreate(oledDisplayHandler, "OLEDHandler",128,NULL,1,NULL);
} // setup

void sendOLEDWelcomeText(){
  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F(" masifamu"));
  display.display();
}

void oledDisplayHandler(void *pvParameters){
    sendOLEDWelcomeText();
    while(1){
        Serial.println("FreeRTOS");
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

// main code here, to run repeatedly: 
void loop() {
} // loop

Seems like thread does not start because i don’t see FreeRTOS string over serial.

If i do it without FreeRTOS it works fine, code without freeRTOS is given below.

#include <avr/io.h>
#include <SPI.h>
#include <Adafruit_GFX.h>           //Includes core graphics library
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

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

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  sendOLEDWelcomeText();
} // setup


// main code here, to run repeatedly: 
void loop() {
} // loop

void sendOLEDWelcomeText(){
  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F(" masifamu"));
  display.display();
  delay(1000);
}

Someone please help me understand where i am going wrong?

Thanks.

Stuck somewhere is a very vague problem description, right ?

  1. Check return codes of FreeRTOS API calls like xTaskCreate.
    How do you know that it was successful ?
  2. Make use of the FreeRTOS built-in development support like defining configASSERT and enable stack checking .
    You provide just the minimal task stack to the OLED task. Maybe that’s just not enough.
  3. Use the debugger to step through the initialization code and set a breakpoint on the task function. Should be hit if the task was created successfully and the scheduler was started properly. Step through the task code to verify that it works as expected. …
1 Like

In addition to the suggestions from @hs2, break the code in the debugger and see what it is doing when it appears stuck. Also, increase the stack size as 128 might not be enough.

1 Like

Thanks for the suggestions.
I have done some experiments based on it.

whan i give 100 to the stack size, it does not successfully create the task, however when i change it to 64 task creation happens successfully. But unable to see FreeRTOS message over serial monitor and don’t see the anything on OLED display as well.

Thanks.

That means your heap (used to allocated the stack memory) is obviously too small !
It doesn’t help to decrease the stack size to an arbitrary small value until task creation succeeds. It’s very likely too small run the task.
Increase your heap if possible, give a larger stack to the task, as already mentioned enable stack checking, attach a debugger, set breakpoints and setp through the code line by line…
Well, all the things we all have done in past to learn :wink:

1 Like

BTW, I will do itj all to learn more about it.

Can you guide me on how to proceed for increasing the HEAP size? just to the info: i am using arduino uno for this project.

Thanks.

Sorry - unfortunately I’m not familiar with Arduino. But I’m sure it’s documented and probably others also had a similar question so you might ask google or use https://forum.arduino.cc/.

1 Like

Thanks, i will look over it as well