OLED Screen

OLED Display

In this example we will demonstrate how to take several onboard readings from the SODAQ Mbili and display those readings using an OLED Display. This example also demonstrates how to interface with I2C components using the specific libraries which are provided for those components.

Here we will take a temperature reading from the DS3231 chip and display that reading on the OLED Display. The DS3231 chip provides a range of functionality including a real time clock, an interrupt timer, and a temperature sensor. Here we will only look at taking the temperature reading. We will explore the clock and timing functions in another example.

In addition to the temperature reading we also read and display the battery voltage level using a reading from one of the analog pins. For further details of the voltage reading refer to the JST section of the schema: SODAQ Mbili Schema.

I2C Protocol Theory

The I2C is a hardware protocol that is supported by the SODAQ Mbili and other Arduino compatible boards. It used for communicating with the SODAQ Mbili DS3231 chip as well as any components connected to the Grove I2C socket.

The Arduino Wire library (which comes included with the Arduino IDE) provides the functionality for the I2C protocol. However, in most cases you will interface with I2C components via their own specific libraries. Here we will use the Sodaq_DS3231 library to interface with the onboard DS3231 chip, and the SeeedGrayOLED library to interface with the OLED Display.

Required Components400px-Grove_-_touch_sensor_Photo

  • SODAQ Mbili Board
  • 0.5W Solar Panel
  • Battery Pack
  • Grove OLED Display 0.96″
  • 1x Grove Cable (any length)

Required Libraries

  • Wire
  • Sodaq_DS3231
  • SeeedGrayOLED

Library Installation

First download and install the SeeedGrayOLED library from here: Seeed Gray OLED Library.
You will want to download the bundled zip file. For instructions on how to install and use Arduino libraries please refer to this guide: Arduino Libraries

Note: The specific component used in the example is the Grove OLED Display 0.96″. Other displays will require their own specific libraries.

The Wire library comes pre-installed with the Arduino IDE. Additionally, the Sodaq_DS3231library is included with the SODAQ Mbili files that you have already installed.

If necessary, refer to Section 2 of the Getting Started guide for details on where to download from and how to install the SODAQ Mbili files.

Hardware Setup

You should refer to both the board diagram and Grove sockets page for additional information.

  1. First, plug the OLED Display into the Grove I2C socket.
  2. Then, plug the 0.5W solar panel and the 1A LiPo battery into their respective sockets.

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the SODAQ Mbili board, and then unplug the USB cable from the computer when it has completed the upload.

Sketch Code

//Include the necessary libraries
#include <Wire.h>
#include <Sodaq_DS3231.h>
#include <SeeedGrayOLED.h>

//These constants are used for reading the battery voltage
#define ADC_AREF 3.3
#define BATVOLTPIN A6
#define BATVOLT_R1 4.7
#define BATVOLT_R2 10

void setup()
{
   //Start the I2C protocol
    Wire.begin();

   //Initialize the DS3231 
    rtc.begin();

   //Initialise the Seeed Gray OLED display
   SeeedGrayOled.init();  

   //Setup the OLED Display
   SeeedGrayOled.clearDisplay(); 
   SeeedGrayOled.setNormalDisplay();
   SeeedGrayOled.setVerticalMode();
}

void loop()
{
    //Read the temperature and display it on the OLED
    rtc.convertTemperature();  
    int temp = rtc.getTemperature();

    //Read the voltage and display it on the OLED
    int mv = getRealBatteryVoltage() * 1000.0;

    //Display the temperature reading
    SeeedGrayOled.setTextXY(0,0);
    SeeedGrayOled.putString("Temp=");
    SeeedGrayOled.putNumber(temp);
    SeeedGrayOled.putChar('c');

    //Display the voltage reading
    SeeedGrayOled.setTextXY(1,0);
    SeeedGrayOled.putString("Volts=");
    SeeedGrayOled.putNumber(mv);
    SeeedGrayOled.putString("mv");
}

float getRealBatteryVoltage()
{
    uint16_t batteryVoltageВ =analogRead(BATVOLTPIN);
    return (ADC_AREF / 1023.0) * (BATVOLT_R1 + BATVOLT_R2) / BATVOLT_R2 * batteryVoltage;
}

Sketch Code Breakdown

Library Includes

Here the necessary library files are included in the sketch using the #include compiler directive.

//Include the necessary libraries
#include <Wire.h>
#include <Sodaq_DS3231.h>
#include <SeeedGrayOLED.h>

Globals

Here we define several constants which will be used for reading the voltage level of the battery. ADC_AREF specifies the pin voltage and BATVOLTPIN the analog pin that the reading is taken from. BATVOLT_R1 and BATVOLT_R2 specify the resistors used on the voltage reading circuit. For further details refer to the JST section of the schema: SODAQ Mbili Schema.

//These constants are used for reading the battery voltage
#define ADC_AREF 3.3
#define BATVOLTPIN A6 
#define BATVOLT_R1 4.7
#define BATVOLT_R2 10

setup()

Here we first initialize the I2C protocol with a call to Wire.begin(). Then we initialize the OLED display and the DS3231 using calls to SeeedGrayOled.init() and rtc.begin(). Several more methods from the Seed Gray OLED library are then called in order to clear the OLED Display and setup its display mode.

void setup()
{
    //Start the I2C protocol
    Wire.begin();

   //Initialize the DS3231 
    rtc.begin();

   //Initialise the Seeed Gray OLED display
    SeeedGrayOled.init();  

   //Setup the OLED Display
    SeeedGrayOled.clearDisplay(); 
    SeeedGrayOled.setNormalDisplay();
    SeeedGrayOled.setVerticalMode();
}

loop()

First we get the temperature reading. The call to rtc.convertTemperature() instructs the DS3231 to take a immediate temperature reading. The value of that reading is then retreived with a call to rtc.getTemperature(). The user defined method getRealBatteryVoltage() is then called and the returned value is converted to millivolts.

The reading values are then sent to the OLED Display using several methods provided in the Seeed Gray OLED library. The method setTextXY() is used to define the position that any output is written to. The methods putChar(), putString(), and putNumber() all send data to the OLED Display to be displayed at the specified location.

Note: The display position is updated as data is put to the screen. However, if the text overruns the end of the line, it does not continue on the next line but wraps around on the same line.

void loop()
{
    // Read the temperature and display it on the OLED
    rtc.convertTemperature(); 
    int temp = rtc.getTemperature();

    // Read the voltage and display it on the OLED
    int mv = getRealBatteryVoltage() * 1000.0;

   // Display the temperature reading
    SeeedGrayOled.setTextXY(0,0);
    SeeedGrayOled.putString("Temp=");
    SeeedGrayOled.putNumber(temp);
    SeeedGrayOled.putChar('c');

    //Display the voltage reading
    SeeedGrayOled.setTextXY(1,0);
    SeeedGrayOled.putString("Volts=");
    SeeedGrayOled.putNumber(mv);
    SeeedGrayOled.putString("mv");
}

getRealBatteryVoltage()

This method reads an analog signal from the specified analog pin BATVOLTPIN and converts it to a voltage reading using the specified constants for the pin voltage ADC_AREF and the circuit’s resistor values BATVOLT_R1 & BATVOLT_R2. The circuit used here is a voltage divider as the battery voltage is too high to connect directly to the analog pin.

float getRealBatteryVoltage()
{
    uint16_t batteryVoltage = analogRead(BATVOLTPIN);
    return (ADC_AREF / 1023.0) * (BATVOLT_R1 + BATVOLT_R2) / BATVOLT_R2 * batteryVoltage;
}