Implementing FreeRTOS Solutions on ESP 32 Devices using Arduino

Tutorial Lesson 1 of 4

Tom Wilson
6 min readAug 16, 2024

In this lesson we will begin our hands-on journey. We will set up the hardware that will be used throughout the course and test it using simple Arduino programming techniques. In future lessons we’ll use the same hardware but develop more complex solutions using FreeRTOS functions.

Required Hardware

Since this is a hobbyist learning project, the hardware components are pretty generic with loose requirements. Here is what my setup looks like. While it is not very exciting, it can be used to demonstrate many of the essential features of FreeRTOS programming on the Arduino platform

Development Board — I use an ESP 32 WROOM but most ESP 32 boards should work. Here is an Amazon link to the one I used in the testing.

LEDs and limiting resistors— I used generic blue, red, and green LEDs from my junk box. 330 ohm resistors were used to limit current flow but any resistors between 220 and 1K ohms should work. Higher resistance will reduce the brightness of the LED output.

OLED I2C Display — I used a small (0.96 inch) display as seen here, but any display driven by an I2C interface that supports the SSD1306 driver should work.

Pushbutton Switch — Any pushbutton switch designed for breadboard installation should work. The one used here has push-to-close contacts.

Generic breadboard and hookup wire

System Wiring

If you look closely at the photo of my breadboard circuit above you may notice a constraint I found in laying out the circuit. The ESP 32 breakout board is almost as wide as the center section of my breadboard, making it difficult to connect to pins on both edges of the board. Looking at the pinout of the ESP WROOM 32 however, I was able to locate the the power, GPIO, and I2C pins I needed on one side of the board. I mounted the WROOM 32 board offset in the breadboard so that all the pins I needed to connect to had more space for hookup.

ESP 32 WROOM Pinout diagram

The wiring steps for the circuit are described below. You can select your own component placement based on the dimensions of your breadboard.

  1. Connect the 3V3 pin to the positive power rail on your breadboard and the GND pin to the negative rail. Insert a jumper wire from the negative rail on one side of the board to the negative rail on the other.
  2. The three LEDs are placed with the short leg attached to the negative rail and the long pin to the center section of the breadboard.
  3. The limiting resistors connect to the long pin end of the LEDs and bridge across the center of the breadboard to the connection space across.
  4. Jumper wires then connect the ends of the resistors opposite the LED to the appropriate pins on the ESP 32. Green connects to pin D15, red connects to pin D4, and blue connects to pin 16.
  5. On the OLED display board, place jumpers between the VCC and GND pins to the positive and negative power rails. Run a jumper from the pin marked SDA to pin D21 on the ESP 32 and the pin marked SCL to pin D22.
  6. Connect a jumper from pin D23 to one pin on the pushbutton switch and a wire from the other side of the switch to the ground rail.

That’s it. You should be good to go.

Initial Test Programming

Before we get into FreeRTOS programming, let’s test the wiring and the Arduino configuration with a simpler test program. The simple program below will cycle each light on indivdually for 0.5 seconds and display the light color on the OLED display. When the button is pressed the lights will go out and the display will say “DARK”. Not a very useful program but it will confirm the wiring is correct and the Arduino IDE is properly configured.

IDE Setup - This tutorial utilizes the Arduino IDE version 2.3.2 running under Windows 10 but other versions should work as well. If you don’t have it configured for the ESP 32 board then use the Boards Manager menu to download the ESP32 by Espressif Systems library. Once loaded you will have a large number of board options to choose from. I use the ESP DEV MODULE which supports the functions used in the project. You will also want to include the Adafruit SSD1306 library which provides support for the OLED display board which uses the SSD1306 driver chip. Information about the driver is available here. There are no setup requirements necessary to utilize the FreeRTOS functions — they are handled directly by the Arduino/Espressif toolchain that is configured when the ESP 32 board library is installed. Connect the USB-C connector on the breakout board to a USB port on the IDE computer and configure the Arduino IDE to utilize the port. If you are not sure which port it is on, on Windows 10 you can use the Device Manager application to identify it. It will show up under the Ports category as a USB-Serial CH340 device. Here is what the IDE should look like.

Code Entry — Since the purpose of this exercise is simply to test the wiring and IDE configuration there is no need to explain the code. simply cut and paste it into a new Arduino sketch. Save it as rtos_initial.

/*********
This code was developed for use with the tutorial series entitled
"Implementing FreeRTOS Solutions on ESP 32 Devices using Arduino"

Refer to https://medium.com/@tomw3115/implementing-freertos-solutions-on-esp-32-devices-using-arduino-114e05f7138a for more information.

This software is provided as-is for hobbyist use and educational purposes only.

published by Tom Wilson - September 2024 *********/

#include <Adafruit_SSD1306.h>
#include <splash.h>

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>

// define LCD display settings
#define OLED_ADDR 0x3C
#define OLED_RESET 4

// define GPIO pin numbers for LEDs and push button
int pinGreen = 15;
int pinRed = 4;
int pinBlue = 16;
int pinBtn = 23;

// define 1/2 second pause time for light sequencing
int waitTime = 500;

// initialize the lcd display (once)
Adafruit_SSD1306 Display(OLED_RESET);

// the setup function runs once when you press reset or power the board
void setup()
{
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);

//set GPIO pins to output
pinMode(pinGreen, OUTPUT);
pinMode(pinRed, OUTPUT);
pinMode(pinBlue, OUTPUT);
pinMode(pinBtn, INPUT_PULLUP); // button is in HIGH state when unpressed

// initialize the OLED display board
Display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
Display.clearDisplay();
Display.display();

//start with all LEDs OFF
digitalWrite(pinGreen, LOW);
digitalWrite(pinRed, LOW);
digitalWrite(pinGreen, LOW);
}

// function to display messages on the OLED display
void displayMsg(char color[6]) // pass the character array to display
{
Display.clearDisplay();
Display.setTextSize(1);
Display.setTextColor(WHITE);
Display.setCursor(40, 0);
Display.println("LED Lit");
Display.setTextSize(2);
Display.setTextColor(WHITE);
Display.setCursor(30, 15);
Display.println(String(color));
Display.display();
}

void loop()
{
while (true) // run forever until you unplug it
{
if (digitalRead(pinBtn) == HIGH) // check for button push
{
// sequence through LED colors
digitalWrite(pinGreen, HIGH);
displayMsg("Green");
delay(waitTime);
digitalWrite(pinGreen, LOW);
digitalWrite(pinRed, HIGH);
displayMsg("Red");
delay(waitTime);
digitalWrite(pinRed, LOW);
digitalWrite(pinBlue, HIGH);
displayMsg("Blue");
delay(waitTime);
digitalWrite(pinBlue, LOW);
}
else
{
// skip LED sequencing and display Dark" message if the button is pressed
displayMsg("Dark");
}
}
}

Compile and upload the code to your board and it should automatically reset and start running — there is no need to hit the boot or enable buttons. The LED lights should cycle on at 1/2 second intervals and the OLED display will display the color of the light currently on. When you press the button the lights should go off (after the current cycle) and the display will say “Dark” until the button is released. We will use this hardware and IDE configuration for all future lessons. Here is what it should look like.

Lesson 1 Demonstration

Congratulations! You have now verified your wiring and Arduino IDE configuration settings. And you now have a wonderful blinky LED display. In the next lesson we will use the same circuit layout and Arduino IDE settings to build some more interesting solutions using FreeRTOS.

Ready for Lesson 2? Click here.

--

--

Tom Wilson

I am a retired engineer living the Pura Vida lifestyle in a beachfront community along the Pacific coast of Costa Rica.