Arduino OLED display

One obvious Arduino UI option that I had previously overlooked was a small OLED display device. While they have a small screen size these are inexpensive devices and easy to communicate with using the I2C 2 wire interface.

Installing libraries

I used the IDE library manager to look for suitable software to run an SSD1306 OLED display. I chose the Adafruit option as that included provision for a 128 x 32 device and the I2C interface. The installation prompted me to include a GFX and BusIO library as they were dependencies. I knew I wanted a GFX library to manage some basic graphics.

Testing

Then connected a good old basic Arduino Uno to the board using 4 jumper wires - 2 for power and the 2 for the I2C interface.


Then I loaded the appropriate Adafruit supplied example program and watched it happily run through a great demonstration of the display's capabilities. The library supports a wide range of graphics drawing options alongside simple text.

Next steps

The next step for this little OLED screen was something a bit more ambitious. I wanted to connect the OLED display to an Arduino MKR 1010 (the Uno of the modern era in my view) and use it to display the current time and weather forecast.

I knew I could get a local forecast from the UK Met Office using their DataPoint service. You have to sign up with the (free) service to get a data key and then browse the site for details of the data codes used and to select one or more location codes. The service will supply 3 hourly forecasts for a specified location for up to 5 days ahead. To pick the most relevant forecast from the data received you are going to have to know the current time and date - but that dovetailed nicely with my desire to show the current time on the OLED display along with the forecast. In turn, that added some processing to set the Arduino board's real time clock (RTC) from an Internet timeserver to the project.

This project spawned a number of individual posts here highlighting some experimental steps along the way.

  1. How to obtain an IP address for a given URL
  2. How to scroll some long text across a narrow OLED screen 
  3. How to check if a daylight saving adjustment should be made to local time.
  4. How to calculate the day of the week for a given date.

All of the above posts include a demonstration program.


A couple of other useful code snippets turned up in completing the project. Primarily these are a demonstration of the use of sprintf() to concatenate a sequence of string and numeric values and store the result in a char array.


char forecastBuff[100];
void setWeatherText() { clearAnyString(forecastBuff, 100); char degSymb[] = "\0\0"; degSymb[0] = 0xf8; char txt[] = "Forecast: %s Temp: %d%sC feels: %d%sC Chance of precipitation: %d%% Wind: %s %dmph"; sprintf(forecastBuff, txt, forecast.type, forecast.temp, degSymb, forecast.feels, degSymb, forecast.pp, forecast.wDir, forecast.gusts); }
void clearAnyString(char *cArray, int len) { memset(cArray, 0, len); }

The pattern for the final string is defined by the txt[] char array.
  • %s acts as a placeholder for a string
  • %d acts as a placeholder for an integer value
  • %% simply escapes a % symbol when you actually want it in the final text.
  • all other characters are passed to the final string unchanged.
The degSymb[] char array is used to insert a  ° (degree symbol) as a string using the character value 248 (see code page below). This is a workaround to include a character not supported by the Arduino IDE.

The clearAnyString() function was used in multiple places within the code to pre-set char arrays to null values.

Code Page

Worth noting that the Adafruit library for this little OLED screen includes support for code page 437 symbols alongside the usual ASCII set (code page 437 is sometimes known as extended ASCII). Check out the optional extras here. This allowed me to use the little degree symbol (eg. 30°C) in the scrolling weather forecast text.

Caution

One note of caution. An OLED screen has many excellent properties. It is bright and text is easy to read. However they are not suitable for continuous display. The screen will slowly fade of left running for days at a time. Best used for intermittent use - perhaps in response to an event - and shutdown between uses. 





Comments

Popular posts from this blog

Unicode output from an Arduino.

Arduino Regular Expressions

Arduino: How to scroll long text on OLED screen