AnalogLamb Fish32 board: Getting started with SSD1306 display

In this tutorial we will check how to interact with the OLED display of the AnalogLamb Fish32 board and print a “Hello World” message.


Introduction

In this tutorial we will check how to interact with the OLED display of the AnalogLamb Fish32 board and print a “Hello World” message. Since the display already comes connected to the board, we don’t need to worry about the electronic connections and we can use it right away.

In order to interact with the display we will need to use the I2C protocol. Fortunately, there’s already an implementation in the Arduino core for the ESP32, which corresponds to the Wire.h library.

Additionally, we will need to install this library, which allows to interact with the display using a higher level API that provides very easy to use functions. It can be installed via the Arduino IDE library manager, as shown in figure 1.

ESP32 Arduino SSD1306 OLED display library

Figure 1 – Installing the library using the Arduino IDE library manager.

Please note that this tutorial is very similar to the previous one where we interacted with the SSD1306 OLED display. In this case, we are taking in consideration the features of the Fish32 seed board, more precisely, the pins that are connected to the OLED.

 

The code

The first thing we need to do is including the Wire.h library, which allows to establish communication with devices using the I2C protocol. The display of this ESP32 board communicates using this protocol.

#include "Wire.h"

Additionally, we need to include the SSD1306.h library, which provides the higher level functions needed to interact with the display. This way, we don’t need to worry about the lower level communication protocol details.

#include "SSD1306.h"

We will also use the #define macro to specify the values of the SCL and SDA pins of the ESP32. As can be seen in the back of the board (in contains some pin mapping information written in the PCB), the SDA pin corresponds to 22, and the SCL to 23.

#define SDA 22
#define SCL 23

Finally, we need an object of class SSD1306which will expose all the methods we need to interact with the display. This class becomes available when we include the SSD1306.h library.

The constructor of this class receives as first parameter the I2C address of the display, which is 0x3c. Additionally, it receives the number of the SDA and SCL pins, respectively. Remember that we have defined these values before with the #define directives, which we will now use.

SSD1306 display(0x3c, SDA, SCL);

Moving on to the Arduino setup function, we will initialize the display with a call to the init method of the SSD1306 object. This method call receives no parameters.

display.init();

To write some content to the display, we simply need to call the drawString method on the same SSD1306 object. As first and second arguments, we pass the x and y coordinates, respectively, where the string should be drawn.

As third argument, we pass the string to draw. We will pass a very simple “Hello World” string, as can be seen below.

display.drawString(0, 0, "Hello World from ESP32!");

To finalize, we call the display method, which ensures the content is drawn in the display. This method returns void and receives no arguments.

display.display();

The final source code can be seen below. Note that the main loop can be left empty since we have already drawn the content to the display, which will be persisted.

#include "Wire.h"
#include "SSD1306.h" 

#define SDA 22
#define SCL 23

SSD1306 display(0x3c, SDA, SCL);

void setup() {

  display.init();
  display.drawString(0, 0, "Hello World from ESP32!");
  display.display();
}

void loop() {}

 

Testing the code

To test the code, simply compile it and upload it to the ESP32 board. Once the procedure finishes, the “Hello World” string defined in the code should get printed in the display, as shown in figure 2.

ESP32 AnalogLamb board Display Hello World.png

Figure 2 – Hello world with the Fish32 seed board.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s