Categories

Espruino and I2C OLED display

Just recently I spotted various I2C OLED displays on sale at reasonable prices and fancied trying to connect these up one of my Espruino.

Being relatively small size, requiring only 2 connections SDA and SCL from the Espruino but still having good text and graphical capabilities I snapped a couple up cheaply on the net. Here is a picture of the OLED display I bought, these are common on many sites at the moment.

If you want to learn more about OLED technology I recommend you read – https://en.wikipedia.org/wiki/OLED

 

i2c_oled_lcd

 

Connecting the OLED display proved straightforward enough as the display can take standard 3v3 and GND and as its an I2C device on my Espruino I hooked up B7 and B6 which are SDA and SCL respectively

 

Connection and Layout

Pin Label Espruino PIN I2C Function Notes
GND Ground Ground 0V
VCC 3v3 Power Regulated 3v3 supply.
SDA B7 SDA Serial data in
SCL B6 SCL  I2C clock

 

Here is a sample layout diagram

 

espruino-and-oled_bb

 

Code

require("SSD1306");
 
function start(){
 //10px
 g.setFontVector(10);
 // write some text
 g.drawString("Hello",0,0);
 // write to the screen
 g.setFontVector(20);
 g.drawString("World",0,10);
 g.flip(); 
}
 
// I2C
I2C1.setup({scl:B6,sda:B7});
var g = require("SSD1306").connect(I2C1, start);

 

 

Links
0.96 Inch White SPI OLED Display Module 12864 LED For Arduino 2.7 x 2.8cm Resolution 128 x 64

1.3 inch oled white display with 4PIN 4-SPI(default)/I2C Drive IC SH1106

AM2301 and OLED arduino example

In this example we will connect a AM2301 sensor to an Arduino and display the output on an 128×32 I2C OLED display

The AM2301  is a wired version of the DHT21, in a large plastic body.

AM2301

AM2301

Specifications:

  • Type: AM2301
  • Accuracy resolution: 0.1
  • Measurement range: 0-100%RH
  • Temperature measurement range: -40℃ ~ +80℃
  • Humidity measurement precision: ±3%RH
  • Temperature measurement precision: ±0.5℃

This Sensor has 3 wires, connect these wires to your Arduino like this

Sensor Arduino
Red +5V
Black GND
Yellow Digital I/O

OLED Connection

Connect Vin to 5V.
Connect GND to ground
Connect SDA to A4
Connect SCL to A5

 

Code

The code uses the adafruit library for the DHT11 and other variants

Library – https://github.com/adafruit/DHT-sensor-library

OLED library was from Adafruit

Libary – https://github.com/adafruit/Adafruit_SSD1306

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include "DHT.h"
 
#define DHTPIN 2 // modify to the pin we connected
#define DHTTYPE DHT21 // AM2301 
#define OLED_RESET 4
 
Adafruit_SSD1306 display(OLED_RESET);
DHT dht(DHTPIN, DHTTYPE);
 
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
 
void setup() 
{ 
 Serial.begin(57600);
 dht.begin();
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
}
 
 
void loop() 
{
 // Clear the buffer.
 display.clearDisplay();
 
 // text display tests
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 float hum = dht.readHumidity();
 float temp = dht.readTemperature();
 // check if returns are valid, if they are NaN (not a number) then something went wrong!
 if (isnan(temp) || isnan(hum)) 
 {
 Serial.println("Failed to read from DHT");
 } 
 else 
 {
 display.print("Temperature: ");
 display.print(temp);
 display.print(" c");
 display.setCursor(0,10);
 display.print("Humidity: ");
 display.print(hum);
 display.display();
 delay(2000);
 }
}

Output

All going well you should see output like the following on your OLED display

AM2301 and OLED output

AM2301 and OLED output

 

Links

You can pick up one of these breakout/modules for under $8

 

0.96″ Inch I2C IIC Serial 128X64 OLED LCD

BME280 and OLED example

In this example we will connect a BME280 sensor to an Arduino and display the output on an 128×32 I2C OLED display

The BME280 is a humidity sensor features an extremely fast response time which supports performance requirements for emerging applications such as context awareness, and high accuracy over a wide temperature range. The pressure sensor is an absolute barometric pressure sensor with features exceptionally high accuracy and resolution at very low noise. The integrated temperature sensor has been optimized for very low noise and high resolution. It is primarily used for temperature compensation of the pressure and humidity sensors, and can also be used for estimating ambient temperature.

The best way to use the device is a module like the one below

bme280 module

bme280 module

I found this schematic for a breakout online for the bme280, you’ll see in the sparkun image above they are using 4k7’s rather than 10K

Sparkfun has all of teh design files for their module available from https://github.com/sparkfun/SparkFun_BME280_Breakout_Board , I also find these interesting, so worth taking a look

You can use this module in either SPI or I2C modes, in this case we went for SPI mode, here is the wiring for our module to our arduino Uno

Connect Vin to 5V.
Connect GND to ground
Connect the SCK pin to Digital #13
Connect the SDO pin to Digital #12
Connect the SDI pin to Digital #11
Connect the CS pin Digital #10

OLED Connection

Connect Vin to 5V.
Connect GND to ground
Connect SDA to A4
Connect SCL to A5

Here is a schematic drawn up in fritzing

Arduino bme280 oled

Arduino bme280 oled

 

Code

The code uses the sparkfun library, I tried 2 others and couldn’t get them to compile. There are several examples available. I removed some code from a basic example

Library – https://github.com/sparkfun/SparkFun_BME280_Arduino_Library

OLED library was from Adafruit

Libary – https://github.com/adafruit/Adafruit_SSD1306

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include "SparkFunBME280.h"
 
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
 
//Global sensor object
BME280 mySensor;
 
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
 
void setup() 
{ 
 Serial.begin(57600);
 //BME280 sensor setup
 mySensor.settings.commInterface = SPI_MODE;
 mySensor.settings.chipSelectPin = 10;
 
 //Operation settings
 mySensor.settings.runMode = 3; //Normal mode
 mySensor.settings.tStandby = 0;
 mySensor.settings.filter = 0;
 mySensor.settings.tempOverSample = 1;
 mySensor.settings.pressOverSample = 1;
 mySensor.settings.humidOverSample = 1;
 
 Serial.print("Starting BME280... result of .begin(): 0x");
 delay(10); //BME280 requires 2ms to start up.
 Serial.println(mySensor.begin(), HEX);
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
 
}
 
 
void loop() 
{
 // Clear the buffer.
 display.clearDisplay();
 
 // text display tests
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.print("Temperature: ");
 display.print(mySensor.readTempC(), 2);
 display.print(" c");
 display.setCursor(0,10);
 display.print("Pressure: ");
 display.print(mySensor.readFloatPressure(), 2);
 display.setCursor(0,20);
 display.print("Altitude: ");
 display.print(mySensor.readFloatAltitudeMeters(), 2);
 display.print(" m");
 display.display();
 delay(2000);
}

 

Output

 

All going well you should see output like the following on your OLED display

bme280 on OLED output

bme280 on OLED output

 

Links

You can pick up one of these breakout/modules for under $8
BME280 Embedded high-precision barometric pressure sensor module

0.96″ Inch I2C IIC Serial 128X64 OLED LCD

Nucleo-F334 and OLED display

This is an example of connecting a 128×64 I2C OLED display to a NUCLEO-F334R8.

Vcc – 3.3v
Gnd – Any Gnd
SDA – D14
SCL – D15

here is a picture of the OLED I used, there are many variants of this

128X64 OLED LCD LED DISPLAY MODULE

128X64 OLED LCD LED DISPLAY MODULE

Schematics and Layout

 

Nucleo and OLED_bb

Nucleo and OLED_schem

Code

This example requires the Adafruit_Gfx library to be imported into your project. Since this is an I2C display and its a 128×64 I had to modify the rawHeight parameter in the Adafruit_SSD1306.h code as follows

Here is an export of the code and libraries

Nucleo_OLED_zip_nucleo_f334r8

Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)

to

Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 64, uint8_t rawWidth = 128)

#include "mbed.h"
#include "Adafruit_SSD1306.h"
 
DigitalOut myLed(LED1);
I2C i2c(I2C_SDA, I2C_SCL);
 
Adafruit_SSD1306_I2c oled (i2c, D4);
 
int main() 
{
int tickTime = 1000;
oled.begin(SSD1306_SWITCHCAPVCC);
wait_ms(200);
oled.splash();
oled.display();
wait(2);
while(1) 
{
myLed = !myLed;
wait_ms(tickTime);
}
}

You should see the adafruit logo on your display if everything works OK
Links
STM32 Board Nucleo NUCLEO-F334R8 STM32F334R8 STM32 Development Board Integrate ST-LINK/V2-1 Debugger/Programmer Support Arduino

0.96″ Inch Yellow and Blue I2c IIC Serial 128×64 Oled LCD Oled LED Module for Arduino Display 51 Msp420 Stim32 SCR