Display on SSD1306/SSD1315 OLED with Uno R4 WiFi & Nano 33 BLE using U8g2 library

This video show displaying on 0.96 inch 128x64 SSD1306/SSD1315 OLED with NoLogo Uno R4 WiFi & Nologo Nano 33 BLE nRF52840 using U8g2 library.


Library:

Install U8g2 Library in Arduino IDE's Library Manager.

Connection:


	Connection between SSD1306/SSD1315 OLED and Uno R4 WiFi/Nano 33 BLE
	notice: Uno R4 WiFi operate on 5V
		Nano 33 BLE operate on 3.3V
		The operating voltage of my OLED board is 3.3~5V
		* Check operating voltage of your own OLED board!
			
	OLED	Uno R4 WiFi	Nano 33 BL
	------------------------------
	GND	GND		GND
	VCC	+5V		+3V3
	SCL	D19/SCL		A5/P0.02
	SDA	D18/SDA		A4/P0.31
	

Arduino Code:

i2c_scanner.ino, scan connected I2C devices and print its I2C address.
#include <Wire.h>

void setup()
{
  Wire.begin();

  delay(1000);
  Serial.begin(115200);
  while (!Serial);
  delay(1000);

  Serial.println("\nI2C Scanner");
  delay(2000);
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

nologo_oled.ino
/*  
  Exercise run on Nologo Nano 33 BLE/UNO R4 WiFi,
  to display on I2C OLED (SSD1306)
  using U8g2 library (https://github.com/olikraus/u8g2).
*/

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

String board = "unknown board";

void u8g2_prepare(void) {
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}

void u8g2_box_frame(uint8_t a) {
  u8g2.drawStr( 0, 0, "drawBox");
  u8g2.drawBox(5,10,20,10);
  u8g2.drawBox(10+a,15,30,7);
  u8g2.drawStr( 0, 30, "drawFrame");
  u8g2.drawFrame(5,10+30,20,10);
  u8g2.drawFrame(10+a,15+30,30,7);
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  delay(1000);

  Serial.println("- Start - ");
  u8g2.begin();
  u8g2_prepare();

  /*
    This exercise target to run on Nologo Nano 33 BLE/UNO R4 WiFi
    - in Nano 33 BLE, board name defined in BOARD_NAME
    - in UNO R4 WiFi, board name defined in USB_NAME

  */

  #ifdef BOARD_NAME
    board = BOARD_NAME;
    Serial.println("BOARD_NAME: " + String(BOARD_NAME));
  #endif
  #ifdef USB_NAME
    board = USB_NAME;
    Serial.println("USB_NAME: " + String(USB_NAME));
  #endif

  Serial.println("board: " + board);

  int width = u8g2.getWidth();
  int height = u8g2.getHeight();
  Serial.println("getWidth(): " + String(u8g2.getWidth()));
  Serial.println("getHeight(): " + String(u8g2.getHeight()));
  Serial.println("getDisplayWidth(): " + String(u8g2.getDisplayWidth()));
  Serial.println("getDisplayHeight(): " + String(u8g2.getDisplayHeight()));

  u8g2.clearBuffer();
  u8g2.drawFrame(0, 0, width, height);
  u8g2.setCursor(5, 5);
  u8g2.print(board);
  u8g2.sendBuffer();

  delay(1000);
  String strSize = String(width) + " x " + String(height);
  u8g2.setCursor(5, 15);
  u8g2.print(strSize);
  u8g2.sendBuffer();

  delay(3000);
  u8g2.clearBuffer();
  u8g2.sendBuffer();
  for(int y=0; y<5; y++){
    for(int x=0; x<width; x++){
      u8g2.drawPixel(x, y);
      u8g2.sendBuffer();
    }
  }
  for(int y=5; y<30; y++){
    for(int x=0; x<width; x++){
      u8g2.drawPixel(x, y);
    }
    u8g2.sendBuffer();
  }
  for(int y=30; y<height; y++){
    for(int x=0; x<width; x++){
      u8g2.drawPixel(x, y);
    }
  }
  u8g2.sendBuffer();
  
  delay(1000);
  u8g2.clearBuffer();
  u8g2.sendBuffer();
  for(int y=0; y<height; y++){
    for(int x=0; x<width; x++){
      u8g2.drawPixel(x, y);
    }
  }
  u8g2.sendBuffer();

}

void loop() {
  
}


nologo_Serial_Terminal.ino
/*
 * In this exercise, the OLED act as a terminal (U8X8LOG).
 * Read from Serial and display on I2C SSD1306 OLED (128x64),
 * using U8g2 library.
 * Tested on Nologo Nano 33 BLE/Uno R4 WiFi.
*/

#include <Arduino.h>
#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif

// U8x8 Contructor
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); 	      

// setup the terminal (U8X8LOG) and connect to u8g2 for automatic refresh of the display
// The size (width * height) depends on the display 

#define U8LOG_WIDTH 16
#define U8LOG_HEIGHT 8
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8X8LOG u8x8log;

void setup(void)
{
  Serial.begin(115200);
  u8x8.begin();
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  
  u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  u8x8log.setRedrawMode(1);		// 0: Update screen with newline, 1: Update screen for every char  

  u8x8log.println("Read from Serial and display on I2C SSD1306 OLED using U8g2 library.");

  Serial.println("\nEnter something and Send");
}


void loop(void) {

  while (Serial.available() > 0) {
    String term_in = Serial.readString();
    Serial.print(term_in);
    u8x8log.print(term_in);
  }
  
  delay(100);
}

related:
Display on SSD1306/SSD1315 OLED with Uno R4 WiFi & Nano 33 BLE using Adafruit SSD1306 library


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator