micro:bit and OLED display example

micro:bit and OLED display example

This example uses an OLED display these typically come in a couple of different sizes 128×32 and 128×64, this particular example will use the I2C connection from the Micro:bit to the display. There are a couple of libraries that make life easier.

Lets look at a typical oled display

These will come in useful for various projects for example displaying the date and time or maybe temperature readings from a sensor

Connection

Pin Label Microbit PIN I2C Function Notes
GND Ground Ground 0V
VCC Power Power Regulated 5V supply.
SDA SDA / P20 SDA Serial data in
SCL SCL / P19 SCL  I2C clock

This layout shows a 128×32 connected to the LOLIN32, 128×64 I2C devices would be the same

 

microbit and oled

microbit and oled

 

Code

This example uses the https://github.com/adafruit/Adafruit_SSD1306/archive/master.zip and https://github.com/adafruit/Adafruit-GFX-Library/archive/master.zip , there are several built in examples. I have modified one just to display text as further examples will write text to a display

 

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
 
Adafruit_SSD1306 display(OLED_RESET);
 
void setup()   
{                  
  Serial.begin(9600);   
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)  // init done  
  display.clearDisplay();  // text display tests  
  display.setTextSize(1);  
  display.setTextColor(WHITE);  
  display.setCursor(0,0);  
  display.println("Hello, world!");  
  display.setTextColor(BLACK, WHITE); // &#039;inverted&#039; text  
  display.println(3.141592);  
  display.setTextSize(2);  
  display.setTextColor(WHITE);  
  display.print("0x");   
  display.println(0xDEADBEEF, HEX);  
  display.display();  
  display.clearDisplay();
}
 
void loop() 
{  
}

 

Link

1Pcs 128X64 OLED LCD LED Display Module For Arduino 0.96″ I2C IIC SPI Serial new original

Microbit and HDC1080 humidity and temperature sensor example

The HDC1080 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The HDC1080 operates over a wide supply range, and is a low cost, low power alternative to competitive solutions in a wide range of common applications. The humidity and temperature sensors are factory calibrated.

Features
Relative Humidity Accuracy ±2% (typical)
Temperature Accuracy ±0.2°C (typical)
Excellent Stability at High Humidity
14 Bit Measurement Resolution
100 nA Sleep Mode Current

 

Connection

 

 MIcrobit connection  Module connection
 3v3 3v3
 GND  GND
 SDA – 20  SDA
 SCL – 21  SCL

 

Code

I needed to modify the   https://github.com/closedcube/ClosedCube_HDC1080_Arduino library, otherwise there was compilation errors

You need to change Wire.write(0x00); to Wire.write((byte)0x00); – Modified HDC1080 library

This is the default example

 

#include <Wire.h>
#include "ClosedCube_HDC1080.h"
 
ClosedCube_HDC1080 hdc1080;
 
void setup()
{
Serial.begin(9600);
Serial.println("ClosedCube HDC1080 Arduino Test");
 
// Default settings:
// - Heater off
// - 14 bit Temperature and Humidity Measurement Resolutions
hdc1080.begin(0x40);
 
Serial.print("Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
 
printSerialNumber();
 
}
 
void loop()
{
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(3000);
}
 
void printSerialNumber() {
Serial.print("Device Serial Number=");
HDC1080_SerialNumber sernum = hdc1080.readSerialNumber();
char format[12];
sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
Serial.println(format);
}

 

 

 

Output

Open the serial monitor window and you should expect to see something like this

ClosedCube HDC1080 Arduino Test
Manufacturer ID=0x54T=23.06C, RH=51.07%
T=23.06C, RH=51.07%
T=23.08C, RH=51.05%
T=23.08C, RH=51.15%
T=23.10C, RH=51.15%
T=23.11C, RH=51.15%
T=23.11C, RH=51.05% ClosedCube_HDC1080_Arduino-master

 

Links

1PC 2.7 V to 5.5 V HDC1080 high precision temperature and humidity sensor humidity temperature module

http://www.ti.com/lit/gpn/hdc1080

http://www.ti.com/lit/pdf/snau189

Micro:bit and bme280 environmental sensor example

The BME280 is an integrated environmental sensor developed specifically for mobile applications where size and low power consumption are key design constraints. The unit combines individual high linearity, high accuracy sensors for pressure, humidity and temperature in an 8-pin metal-lid 2.5 x 2.5 x 0.93 mm³ LGA package, designed for low current consumption (3.6 μA @1Hz), long term stability and high EMC robustness.

The 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 BME280 supports a full suite of operating modes which provides the flexibility to optimize the device for power consumption, resolution and filter performance.”

Applications

– Context awareness, e.g. skin detection, room change detection

– Fitness monitoring / well-being

– Warning regarding dryness or high temperatures

– Measurement of volume and air flow

– Home automation control

– Control heating, ventilation, air conditioning (HVAC)

– Internet of things

– GPS enhancement (e.g. time-to-first-fix improvement, dead reckoning, slope detection)

– Indoor navigation (change of floor detection, elevator detection)

– Outdoor navigation, leisure and sports applications

– Weather forecast

– Vertical velocity indication (rise/sink speed)

 

Connection

 

microbit and bme280

microbit and bme280

 

Code:

No libraries required

 

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// BME280
// This code is designed to work with the BME280_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Humidity?sku=BME280_I2CS#tabs-0-product_tabset-2
 
#include<Wire.h>
 
// BME280 I2C address is 0x76(108)
#define Addr 0x76
 
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial communication, set baud rate = 9600
Serial.begin(9600);
}
 
void loop()
{
unsigned int b1[24];
unsigned int data[8];
unsigned int dig_H1 = 0;
for(int i = 0; i < 24; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((136+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 24 bytes of data
if(Wire.available() == 1)
{
b1[i] = Wire.read();
}
}
 
// Convert the data
// temp coefficients
unsigned int dig_T1 = (b1[0] & 0xff) + ((b1[1] & 0xff) * 256);
int dig_T2 = b1[2] + (b1[3] * 256);
int dig_T3 = b1[4] + (b1[5] * 256);
 
// pressure coefficients
unsigned int dig_P1 = (b1[6] & 0xff) + ((b1[7] & 0xff ) * 256);
int dig_P2 = b1[8] + (b1[9] * 256);
int dig_P3 = b1[10] + (b1[11] * 256);
int dig_P4 = b1[12] + (b1[13] * 256);
int dig_P5 = b1[14] + (b1[15] * 256);
int dig_P6 = b1[16] + (b1[17] * 256);
int dig_P7 = b1[18] + (b1[19] * 256);
int dig_P8 = b1[20] + (b1[21] * 256);
int dig_P9 = b1[22] + (b1[23] * 256);
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(161);
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 1 byte of data
if(Wire.available() == 1)
{
dig_H1 = Wire.read();
}
 
for(int i = 0; i < 7; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((225+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 7 bytes of data
if(Wire.available() == 1)
{
b1[i] = Wire.read();
}
}
 
// Convert the data
// humidity coefficients
int dig_H2 = b1[0] + (b1[1] * 256);
unsigned int dig_H3 = b1[2] & 0xFF ;
int dig_H4 = (b1[3] * 16) + (b1[4] & 0xF);
int dig_H5 = (b1[4] / 16) + (b1[5] * 16);
int dig_H6 = b1[6];
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control humidity register
Wire.write(0xF2);
// Humidity over sampling rate = 1
Wire.write(0x01);
// Stop I2C Transmission
Wire.endTransmission();
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control measurement register
Wire.write(0xF4);
// Normal mode, temp and pressure over sampling rate = 1
Wire.write(0x27);
// Stop I2C Transmission
Wire.endTransmission();
 
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select config register
Wire.write(0xF5);
// Stand_by time = 1000ms
Wire.write(0xA0);
// Stop I2C Transmission
Wire.endTransmission();
 
for(int i = 0; i < 8; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((247+i));
// Stop I2C Transmission
Wire.endTransmission();
 
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
 
// Read 8 bytes of data
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
}
 
// Convert pressure and temperature data to 19-bits
long adc_p = (((long)(data[0] & 0xFF) * 65536) + ((long)(data[1] & 0xFF) * 256) + (long)(data[2] & 0xF0)) / 16;
long adc_t = (((long)(data[3] & 0xFF) * 65536) + ((long)(data[4] & 0xFF) * 256) + (long)(data[5] & 0xF0)) / 16;
// Convert the humidity data
long adc_h = ((long)(data[6] & 0xFF) * 256 + (long)(data[7] & 0xFF));
 
// Temperature offset calculations
double var1 = (((double)adc_t) / 16384.0 - ((double)dig_T1) / 1024.0) * ((double)dig_T2);
double var2 = ((((double)adc_t) / 131072.0 - ((double)dig_T1) / 8192.0) *
(((double)adc_t)/131072.0 - ((double)dig_T1)/8192.0)) * ((double)dig_T3);
double t_fine = (long)(var1 + var2);
double cTemp = (var1 + var2) / 5120.0;
double fTemp = cTemp * 1.8 + 32;
 
// Pressure offset calculations
var1 = ((double)t_fine / 2.0) - 64000.0;
var2 = var1 * var1 * ((double)dig_P6) / 32768.0;
var2 = var2 + var1 * ((double)dig_P5) * 2.0;
var2 = (var2 / 4.0) + (((double)dig_P4) * 65536.0);
var1 = (((double) dig_P3) * var1 * var1 / 524288.0 + ((double) dig_P2) * var1) / 524288.0;
var1 = (1.0 + var1 / 32768.0) * ((double)dig_P1);
double p = 1048576.0 - (double)adc_p;
p = (p - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((double) dig_P9) * p * p / 2147483648.0;
var2 = p * ((double) dig_P8) / 32768.0;
double pressure = (p + (var1 + var2 + ((double)dig_P7)) / 16.0) / 100;
 
// Humidity offset calculations
double var_H = (((double)t_fine) - 76800.0);
var_H = (adc_h - (dig_H4 * 64.0 + dig_H5 / 16384.0 * var_H)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * var_H * (1.0 + dig_H3 / 67108864.0 * var_H)));
double humidity = var_H * (1.0 - dig_H1 * var_H / 524288.0);
if(humidity > 100.0)
{
humidity = 100.0;
}
else if(humidity < 0.0)
{
humidity = 0.0;
}
 
// Output data to serial monitor
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
Serial.print("Pressure : ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Relative Humidity : ");
Serial.print(humidity);
Serial.println(" RH");
delay(1000);
}

 

 

Output

In the serial monitor

Temperature in Celsius : 34.20 C
Temperature in Fahrenheit : 93.56 F
Pressure : 903.94 hPa
Relative Humidity : 0.00 RH
Temperature in Celsius : 34.51 C
Temperature in Fahrenheit : 94.11 F
Pressure : 893.48 hPa
Relative Humidity : 0.00 RH
Temperature in Celsius : 33.74 C
Temperature in Fahrenheit : 92.73 F
Pressure : 919.16 hPa
Relative Humidity : 0.00 RH

 

Links

BME280 Digital Sensor Temperature Humidity Barometric Pressure Sensor Module I2C SPI 1.8-5V

MICRO:BIT and I2C LCD example

In this example we will interface to an I2C LCD using our MICRO:BIT. Now these I2C LCD’s consist of 2 parts usually an HD44780 16×2 LCD and an I2C backpack which connects to the LCD exposing the standard power and I2C pins.

This is a typical module you can buy, you can see the backpack which is obviously on the back of the LCD

i2c lcd
i2c lcd

We will now look at the connections and a simple layout

Layout

Connect the pins as follows:

MICRO:BIT LCD1602
GND GND
3v3 VCC
SDA/21 SDA
SCL/22 SCL

 

 

 

Code

You will need an updated I2C LCD library, the original one I couldn’t get to work but this one does seem to work –

LiquidCrystal_I2C-master

You will need to import this into the IDE as usual

Now my example below required the I2C address to be changed to 0x3F, a lot of the example I have looked at are 0x27

 

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
 
void setup()
{
  lcd.init(); // initialize the lcd
 // Print a message to the LCD.
 lcd.backlight();
 lcd.setCursor(0,0);
 lcd.print("Hello world");
 lcd.setCursor(1,1);
 lcd.print("MICROBIT");
 }
 
void loop()
{
 
}

 

All going well you should see the messages in the code on your LCD display

 

Link

You can pick up one these modules for under $3

1pcs Blue Display IIC/I2C/TWI/SPI Serial Interface 1602 16X2 LCD Module