ESP32 (25) – Oled display with U8G2

If you read my post ESP32, Wemos or not to Wemos you know that I brought a development board, that happened to be a D-duino-32 clone, with an ESP-WROOM-32 module and a 0.96″ oled display.

This display, available also as a standalone module on several websites (for example on Banggood), has the following features:

  • size: 0.96 inches
  • resolution: 128×64 pixels
  • controller: SSD1306 with I2C interface
  • power supply: 3.3V – 6V

u8g2-00a u8g2-00b

It’s very easy to interface it with the esp32 chip thanks to the work of olikraus and Neil Kolban. The first wrote the u8g2 library, while the second implemented the specific functions of the u8g2’s hardware abstraction layer (HAL) for the esp32.

u8g2, installation

u8g2 is a fantastic library for monochrome displays: it supports several types of displays and controllers, it’s easy to port to new platforms and offers many methods to draw geometric figures, display images and write strings using different fonts.

Let’s learn how to use it in your project. First download the archive with the content of the library’s Github repository:

u8g2-01

If it doesn’t exist, create the components folder in the main folder of your project. Unzip the archive into that folder and rename the subfolder u8g2-master in u8g2:

u8g2-02

In the u8g2 folder create a file named component.mk with the following content:

u8g2-03

Now download the files u8g2_esp32_hal.c and u8g2_esp32_hal.h from nkolban’s repository:

u8g2-04

You can copy the two files in the main folder of your project:

u8g2-05

u8g2, configuration

To use the u8g2 library in your program, first include the header file:

#include "u8g2_esp32_hal.h"

The HAL configuration is stored in the u8g2_esp32_hal_t struct:

typedef struct {
  gpio_num_t clk;
  gpio_num_t mosi;
  gpio_num_t sda;
  gpio_num_t scl;
  gpio_num_t cs;
  gpio_num_t reset;
  gpio_num_t dc;
} u8g2_esp32_hal_t;

The library supports both I2C and SPI displays: this is the reason why you see in the struct fields related to the two buses.

You can define and initialize the struct with default values with:

u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;

The oled display of the D-duino-32 board has an I2C interface connected to esp32’s pin 4 (SCL) and 5 (SDA):

u8g2_esp32_hal.sda = 5;
u8g2_esp32_hal.scl = 4;

When the configuration of the struct is complete, you can use the u8g2_esp32_hal_init() method:

u8g2_esp32_hal_init(u8g2_esp32_hal);

Now move to the configuration of the u8g2 library. Define an u8g2_t variable:

u8g2_t u8g2;

Based on the display you’re using, you have to choose the correct setup function. The parameters for the function are:

  • the pointer to the u8g2_t variable you defined before
  • a constant value that specifies the display rotation
  • two HAL functions to send data on the bus and to delay

The constants for the display roation are:

u8g2-06

and the two HAL functions developed by Kolban are:

  • u8g2_esp32_msg_i2c_cb
  • u8g2_esp32_msg_i2c_and_delay_cb

Here I’m using the setup function for the ssd1306 “noname” controller with the _f prefix that means full framebuffer:

u8g2_Setup_ssd1306_128x64_noname_f(
  &u8g2, U8G2_R0,
  u8g2_esp32_msg_i2c_cb,
  u8g2_esp32_msg_i2c_and_delay_cb);

framebuffer

full framebuffer means that all the data to send to the display are stored in the RAM memory of the microcontroller. This makes easier and faster to work with the display but consumes more RAM. The u8g2 library also supports a page buffer mode to save some memory. A comparison between the different modes, with pros and cons, is available on the library’s wiki.

Finally – if you’re using an I2C display – you have to specify its address (here 0x78):

u8x8_SetI2CAddress(&u8g2.u8x8,0x78);

u8g2, use

After the setup, initialize the display with:

u8g2_InitDisplay(&u8g2);

The display is now in power save mode, to “turn it on” you have to disable this mode with:

u8g2_SetPowerSave(&u8g2, 0);

Now you can call the different methods available to display text, images, shapes. If you’re using the full framebuffer mode, first prepare the buffer in memory and then send it to the display.

Prepare an empty buffer with ClearBuffer():

u8g2_ClearBuffer(&u8g2);

Now for example use the SetFont() and DrawStr() methods to write a string into the buffer with a specific font:

u8g2_SetFont(&u8g2, u8g2_font_timR14_tf);
u8g2_DrawStr(&u8g2, 2,17,"Hello World!");

and finally display the buffer’s content on the display with:

u8g2_SendBuffer(&u8g2);

u8g2-00c

Demo

In the following video I quickly explain how to install the library, how to prepare an image to be displayed and in the end you can see the execution, on my D-duino-32, of the example program available on Github.

App note: Buried capacitive sensors for tamper protection

an_silabs_an528

A short app note from Silicon Labs on burying pads to prevent snopping on keypads. Link here (PDF)

An individual’s financial matters are increasingly electronic in nature and decreasingly interpersonal. As financial institutions replace human interaction with electronic interfaces such as ATMs, the need to make electronic circuits tamper-proof becomes critical. A typical numeric keypad for financial transactions may contain up to hundred or more tamper prevention and detection features. Tamper detection circuits raise alarms and disable functionality, while tamper prevention features are designed to prevent intrusions and breaches. This application note addresses the elimination of copper pads on the accessible top surface of printed circuit boards (PCBs). Burying traces to internal layers of a PCB prohibits electrical contacts from snooping on copper elements within the PC board.

App note: Driving E ink segmented displays

an_renesas_e_ink

Application note on driving E Ink displays from Renesas. Link here (PDF)

E Ink segmented displays are direct drive displays consisting of E Ink Vizplex Imaging Film sandwiched between two electrode layers, the top plane and the backplane, and then encased in an environmental barrier solution to protect the film and the segment electrodes. To get the most from E Ink segmented Displays, you need to be aware of proper design and implementation. Your reward will be sharp, precise images, a pleasant transition from one image to the next, and a long battery life.

SQUIX ESP8266 based e-paper WiFi weather station

image1

Erich Styger documented his experience building Daniel Eichhorn’s e-paper weather station with a custom enclosure:

Using e-paper for a weather station is an ideal solution, as the data does not need to be updated often. By default, the station reaches out every 20 minutes for new data over WiFi and then updates the display. Daniel Eichhorn already has published kits for OLED (see “WiFi OLED Mini Weather Station with ESP8266“) and touch display (see “WiFi TFT Touch LCD Weather Station with ESP8266“). I like them both, but especially the TFT one is very power-hungry and not really designed to work from batteries. What I would like is a station which can run for weeks.

More details at MCU on Eclipse site.

MickMake Mail #20: SAMG53, SAM4S & Anet 3D printer // News

Not much coming in to the MickMake studios this week so I delve in to a 3D printer I received from BangGood a couple of weeks back. SAM4S Xplained Pro I bought a couple of these for a project I’m working on that relies heavily on ultra low power consumption. Continue reading MickMake Mail #20: SAMG53, SAM4S & Anet 3D printer // News

Posted by Mick on MickMake - Live. Learn. Make.

PIC18 four-channel DMX relay controller

image1

Glen Akins has a nice build log on his four channel DMX-controlled relay, that is available on GitHub:

 Halloween was right around the corner and I needed a timer with a bunch of relays to trigger some store-bought props and a fog machine periodically. (Mental note: read fog machine specs carefully—not all come with timer remotes.) My first thought was an Arduino and cheap relay board. Second thought was to build something with a micro and some relays. Third thought was that if I’m going to build something, might as well add DMX and package it up into a neat enclosure. Hence, the four channel DMX-controlled relay project was born.

See the full post on his blog.

A Certificate Authority with OpenSSL

The security of several protocols is implemented using SSL certificates. Usually, those certificates are created by public certificate authorities. If we’re connecting to a website (HTTPS protocol), the browser we’re using must recognize the identity of the CA that signed the server certificate, otherwise an error message is shown:

ca-002

A CA “creates” a new certificate signing it with its own certificate… you can find more than one level, for example the Google certificate (in blu) has been signed by an intermediate CA (green), whose certificate is signed by a root CA (red):

ca-001

To consider the certificate valid, the browser or the operating system must have the certificates of the CAs that signed it in their trusted repository:

ca-003

Today I’ll explain you how to create a CA using an opensource tool, OpenSSL. This CA will be very useful everytime you’ll need an SSL certificate for internal use.

After having installed the OpenSSL tool, create a new folder dedicated to the CA (in my example, MyCA).

Within the new folder, create some empty folders and files:

ca-004

Open the “serial” file and type the value 1000 (this will be the serial number of our first certificate).

Now download from my Github repository the openssl.cnf file and copy it in the MyCA folder. This file contains the whole configuration of the Certificate Authority.

Open the file and change the dir parameter with the path of your CA’s main folder:

ca-005

Now generate the private key of your CA. All the following commands must be issued in the MyCA folder:

openssl genrsa -aes256 -out private/ca.key.pem 4096

You’ll be prompted for a password; it’s very important to note it down because you’ll need it everytime you’ll use the CA.

Now generate the self-signed certificate for the CA:

openssl req -config openssl.cnf -key private/ca.key.pem -new -x509 -days 3650 
 -sha256 -extensions v3_ca -out certs/ca.cert.pem

OpenSSL will ask some information; the most important of which is the Common Name, that is the name which identifies your CA:

ca-006

Let’s now generate client or server certificates using your new CA.

Generating a new certificate starts from a private key, that will be safely stored on the system which will use the certificate (for example on the webserver that will publish the site in HTTPS). Then you have to generate a CSR (certificate signing request) from the key. The CSR is the file that will be signed by the CA to produce the final certificate.

As CA administrator, you can receive the CSR file from the end user; alternatively you can generate it using OpenSSL:

1. generate the private key (RSA algorithm) for your new certificate:

openssl.exe genrsa -out server.key

2. generate the CSR file:

openssl.exe req -new -config openssl.cnf -key server.key -out server.csr

Again you are prompted for some information, including the name (common name) of your server.

Now generate the certificate signing the request:

openssl.exe ca -config openssl.cnf -extensions server_cert 
 -notext -in server.csr -out server.cer

Confirm with two Y (yes) and your new certificate is ready:

ca-007

Building a Midi hat and jukebox using the Raspberry Pi

pi-midi-jukebox-final-600

Dr. Scott M. Baker has published a new build:

I picked up a Roland SC-55 to use with my retrocomputer setup recently, and I figured it would be cool to turn the thing into a standalone midi jukebox, so that no “computer” is required. I also figured this would be relatively easy, using a raspberry pi as the controller to drive the SC-55. My first step was to figure out how to get MIDI out from a raspberry pi. One option would have been to purchase a USB-MIDI adapter. This would have worked, but I really wanted to develop a native raspberry pi MIDI interface rather than using USB. MIDI is a fairly simple interface, and the raspberry pi has built in serial capability, so this ought not to be too difficult.

Project details can be found on Dr. Scott M. Baker’s blog.

Check out the video after the break.

App note: Grid-connected solar microinverter reference design

an_microchip_an1444a

A good read from Microchip on the theory behind inverter design connected to grip power. Link here (PDF)

There are two main requirements for solar inverter systems: harvest available energy from the PV panel and inject a sinusoidal current into the grid in phase with the grid voltage. In order to harvest the energy out of the PV panel, a Maximum Power Point Tracking (MPPT) algorithm is required. This algorithm determines the maximum amount of power available from the PV module at any given time. Interfacing to the grid requires solar inverter systems to abide by certain standards given by utility companies. These standards, such as EN61000-3-2, IEEE1547 and the U.S. National Electrical Code (NEC) 690, deal with power quality, safety, grounding and detection of islanding conditions.