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.
- How to obtain an IP address for a given URL
- How to scroll some long text across a narrow OLED screen
- How to check if a daylight saving adjustment should be made to local time.
- How to calculate the day of the week for a given date.
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);
}
- %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.
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
Post a Comment