Newhaven Products > OLEDs

NHD-0216AW-IB3 with Arduino Uno Code example?

(1/2) > >>

mprowe:
Hi,

As the subject says, I am hoping that someone can offer me an example of using this display via the I2C interface from the Arduino Uno.
I know that there is an example for the Mega. And for the Uno using the SPI interface. Neither of which, I have been able to convert.

Additionally,  can I hard-wire the /res line low? Or does it need to be toggled during a setup phase?

Regards, Martin

Saurabh_B:
Hello,

You can find the code for this display here: https://github.com/NewhavenDisplay/NHD_US2066

This code will work for both the Arduino Uno and Mega.

mprowe:

--- Quote from: Saurabh_B on May 04, 2017, 04:14:08 PM ---This code will work for both the Arduino Uno and Mega.

--- End quote ---

Thank you for the advice,

Unfortunately it does not work for me?
If I try and compile the example sketch "NHD_0216AW_XB3", yes it will compile without errors for the MEGA,
but when I change my Board to UNO, it will not?

As, it seems, I can not attach a text file with the full compilation output, I offer only the first error seen & hope it provides sufficient clues:


--- Code: ---/home/mprowe/Arduino/libraries/NHD_US2066/NHD_US2066.cpp: In function 'void command(unsigned char)':
/home/mprowe/Arduino/libraries/NHD_US2066/NHD_US2066.cpp:14:15: error: 'PORTA' was not declared in this scope
               PORTA = c;
--- End code ---

Sorry that my skills do not allow me to debug this on my own. So any pointers will be most welcome.

Regards, Martin

Chris O.:
Edit:
Please do not use this use the one on Reply #5

library by Phil Grant 2013  www.gadjet.co.uk
OLedI2C library http://gadjetsblog.blogspot.com/2013/09/oled-1602-display-and-library-for.html

Perhaps this will be of help.
Been long time since I used this Library, createChar function contributed by me.

keywords.txt / OLedI2C.cpp / OLedI2C.h
The 3 files should be in %your arduino dir%\libraries\OLedI2C\
The directory name must match the library name for libraries to work.

NOTE: this is set up for 04x20 oled now.
Look for this in OLedI2C.cpp perhaps you can edit that line for 02x16.
 int row_offsets[] = { 0x00, 0x20, 0x40, 0x60 };// **************THIS WAS THE CHANGE FOR 4 LINES.

keywords.txt

--- Code: ---#######################################
# Syntax Coloring Map For OLedI2C
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

OLedI2C KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

init KEYWORD2
sendCommand KEYWORD2
sendString KEYWORD2
sendData KEYWORD2
clearLcd KEYWORD2
cursPos KEYWORD2
setCursor KEYWORD2
createChar KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
--- End code ---


OLedI2C.cpp

--- Code: ---// 1602-OLED-Arduino-Library/OLedI2C.cpp
// keywords.txt / OLedI2C.cpp / OLedI2C.h
// The 3 files should be in %your arduino dir%\libraries\OLedI2C\
// The directory name must match the library name for libraries to work.

/*
This is my first Library to make writing to the OLED 1602
Display much easier, the display is based on the SSD1311.
The display has 16 characters and 2 lines.
The library is far from complete and may be prone to errors.
Feedback welcome, visit www.gadjet.co.uk
Phil Grant 2013
Scrolling contributed by Nathan Chantrell http://nathan.chantrell.net/
Updated 06/11/2013 to include the cursPos data in the sendString function
sendString("string", col, row)
*/
#include "OLedI2C.h"

// NEW
//#include <stdio.h>
//#include <string.h>
//#include <inttypes.h>
//#include "Arduino.h"

#include "Wire.h"
#define OLED_Address 0x3c
#define OLED_Command_Mode 0x80
#define OLED_Data_Mode 0x40

OLedI2C::OLedI2C(){}
OLedI2C::~OLedI2C(){}


void OLedI2C::init() {
   // *** I2C initial *** //
 delay(100);
 sendCommand(0x2A); // **** Set "RE"=1 00101010B
 sendCommand(0x71);
 sendCommand(0x5C);
 sendCommand(0x28);

 sendCommand(0x08); // **** Set Sleep Mode On
 sendCommand(0x2A); // **** Set "RE"=1 00101010B
 sendCommand(0x79); // **** Set "SD"=1 01111001B

 sendCommand(0xD5);
 sendCommand(0x70);
 sendCommand(0x78); // **** Set "SD"=0  01111000B

 sendCommand(0x08); // **** Set 5-dot, 3 or 4 line(0x09), 1 or 2 line(0x08)

 sendCommand(0x06); // **** Set Com31-->Com0  Seg0-->Seg99

 // original:
 // **** Set OLED Characterization *** //
 // sendCommand(0x2A);  // **** Set "RE"=1
 // sendCommand(0x79);  // **** Set "SD"=1
 // **** CGROM/CGRAM Management *** //
 // sendCommand(0x72);  // **** Set ROM
 // sendCommand(0x00);  // **** Set ROM A and 8 CGRAM

 // **** CGROM/CGRAM Management *** //
 sendCommand(0x72);     // **** Set ROM
 sendData(B00000011);     // **** Set ROM A and 8 CGRAM This selects CGROM A, 00000000 selects CGROM C
 // **** Set OLED Characterization *** //
 sendCommand(0x2A);     // **** Set "RE"=1
 sendCommand(0x79);     // **** Set "SD"=1

 sendCommand(0xDA); // **** Set Seg Pins HW Config
 sendCommand(0x10);   

 sendCommand(0x81);  // **** Set Contrast
 sendCommand(0xFF);

 sendCommand(0xDB);  // **** Set VCOM deselect level
 sendCommand(0x30);  // **** VCC x 0.83

 sendCommand(0xDC);  // **** Set gpio - turn EN for 15V generator on.
 sendCommand(0x03);

 sendCommand(0x78);  // **** Exiting Set OLED Characterization
 sendCommand(0x28);
 sendCommand(0x2A);
 //sendCommand(0x05); // **** Set Entry Mode
 sendCommand(0x06); // **** Set Entry Mode
 sendCommand(0x08); 
 sendCommand(0x28); // **** Set "IS"=0 , "RE" =0 //28
 sendCommand(0x01);
 sendCommand(0x80); // **** Set DDRAM Address to 0x80 (line 1 start)

 delay(100);
 sendCommand(0x0C);  // **** Turn on Display
 }
 
 // void OLedI2C::cursPos(uint8_t col, uint8_t row)
 // {
 // int row_offsets[] = { 0x00, 0x40 };
 // sendCommand(0x80 | (col + row_offsets[row]));
 // }

 void OLedI2C::cursPos(uint8_t col, uint8_t row)
 {
 int row_offsets[] = { 0x00, 0x20, 0x40, 0x60 };// **************THIS WAS THE CHANGE FOR 4 LINES
 sendCommand(0x80 | (col + row_offsets[row])); //was 0x80
 }
 
 // NEW 03-23-2015
 // cloumn, row / setCursor
  void OLedI2C::setCursor(uint8_t col, uint8_t row)
 {
  int row_offsets[] = { 0x00, 0x20, 0x40, 0x60 };// **************
 sendCommand(0x80 | (col + row_offsets[row]));
 }
 
void OLedI2C::clearLcd()
{
sendCommand(0x01);
}

void OLedI2C::lcdOff()
{
sendCommand(0x08);  // **** Turn on Off
}

void OLedI2C::lcdOn()
{
sendCommand(0x0C);  // **** Turn on On
}

 void OLedI2C::sendCommand(unsigned char command)
{
    Wire.beginTransmission(OLED_Address); // **** Start I2C
    Wire.write(OLED_Command_Mode);      // **** Set OLED Command mode
    Wire.write(command);
    Wire.endTransmission();                  // **** End I2C
      delay(10);
}
void OLedI2C::sendFloat(float digit, uint8_t dec, uint8_t nad, uint8_t col, uint8_t row)
{
char line[10];//Ten characters, I hope that's enough
dtostrf(digit,dec,nad,line);//Convert the float value to a string
sendString(line, col, row);

}

 void OLedI2C::setContrast(unsigned char contrast) // contrast as 0x00 to 0xFF
{
     //Set OLED Command set
sendCommand(0x2A);
sendCommand(0x79);

sendCommand(0x81);  // Set Contrast
sendCommand(contrast); // send contrast value
sendCommand(0x78);  // Exiting Set OLED Command set
   sendCommand(0x28);

}

void OLedI2C::sendString(const char *String, uint8_t col, uint8_t row)
{
  cursPos(col, row);
  unsigned char i=0;
  while(String[i])
  {
    sendData(String[i]);      // *** Show String to OLED
    i++;
  }
}
void OLedI2C::sendData(unsigned char data)
{
  Wire.beginTransmission(OLED_Address);  // **** Start I2C
  Wire.write(OLED_Data_Mode);      // **** Set OLED Data mode
  Wire.write(data);
  Wire.endTransmission();                     // **** End I2C
}
void OLedI2C::scrollString(char* message, byte row, unsigned int time)//written by Nathan Chantrell http://nathan.chantrell.net/
{
 char buffer[16];
 for (byte i=0;i<strlen(message)+16;i++) {
  byte pos = i+1;
  for (byte j=0;j<16;j++) {
   if ((pos<16)||(pos>strlen(message)+15)) { // pad and trail with blank spaces
     buffer[j]=' ';
   }
   else buffer[j]=message[pos-16];
   pos++;
  }
 //cursPos(0,row); removed by PG
 sendString(buffer, 0, row); //Edited by PG tho include the cursor pos within the sendString command
 delay(time);
 }
}

// NEW 03-31-2015
// Allows us to fill the first 8 CGRAM locations
// with custom characters
void OLedI2C::createChar(uint8_t location, uint8_t charmap[])
{
   location &= 0x7;            // we only have 8 locations 0-7
   
   sendCommand(0x40 | (location << 3));
   delayMicroseconds(30);
   
   for (int i=0; i<8; i++)
   {
      sendData(charmap[i]);      // call the virtual write method
      delayMicroseconds(40);
   }
}
--- End code ---

OLedI2C.h

--- Code: ---// 1602-OLED-Arduino-Library/OLedI2C.h

/*
OLedI2C.h
OLED 1602 Library for the I2C
OLED display from WIDE.HK
Also my first attempt at a Library
Phil Grant Sept 2013
www.gadjet.co.uk
Scrolling contributed by Nathan Chantrell http://nathan.chantrell.net/
Updated 06/11/2013 to include the cursPos data within the sendString function.
*/
#ifndef OLedI2C_h
#define OLedI2C_h
#include "Arduino.h"
#include "Wire.h"

class OLedI2C {
public:
OLedI2C();
~OLedI2C();
void init();
void sendCommand(unsigned char command);
void sendString(const char *String, uint8_t col, uint8_t row);
void sendFloat(float digit, uint8_t dec, uint8_t nad, uint8_t col, uint8_t row);
void sendData(unsigned char data);
void clearLcd();
void cursPos(uint8_t, uint8_t); // cloumn, row
void setCursor(uint8_t, uint8_t); // NEW 03-23-2015 / cloumn, row / setCursor
void scrollString(char* message, byte row, unsigned int time); //written by Nathan Chantrell http://nathan.chantrell.net/
void lcdOff();
void lcdOn();
void setContrast(unsigned char contrast); // contrast should be the hex value between 0x00 and 0xFF
void createChar(uint8_t, uint8_t[]); // NEW 03-23-2015 / Allows us to fill the first 8 CGRAM locations with custom characters
};
#endif
--- End code ---

Example

--- Code: ---// I2C Wire Library Newhaven Display NHD-0420CW OLED TEST

// Wire Library, This library allows you to communicate with I2C / TWI devices
// Board           I2C / TWI pins
// Uno, Ethernet   A4 (SDA), A5 (SCL)
// Mega2560        20 (SDA), 21 (SCL)
// Leonardo         2 (SDA),  3 (SCL)
// Due             20 (SDA), 21 (SCL), SDA1, SCL1
//Teensy 3.0/3.1   18 (SDA), 19 (SCL),Back Side 30 (SDA1), 29 (SCL1)

#include <Wire.h> 

#define I2C_ADDR 0x3c  // Define I2C Address, 0x3c or 0x3d
#include <OLedI2C.h> 
OLedI2C LCD;

// custom font arrays
byte newChar1[8] = {
  B11011,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B10101
};

byte newChar2[8] = {
  B10000,
  B01000,
  B00101,
  B00100,
  B00100,
  B00101,
  B01000,
  B10000
};

byte newChar3[8] = {
  B10001,
  B01010,
  B01110,
  B00100,
  B00100,
  B01110,
  B01010,
  B10001
};

byte newChar4[8] = {
  B11111,
  B11111,
  B11111,
  B00100,
  B00100,
  B11111,
  B11111,
  B11111
};

byte newChar5[8] = { // 5TH custom font
  B10101,
  B10000,
  B10110,
  B00001,
  B00000,
  B10001,
  B01010,
  B00000
};

void setup() {
  Wire.begin(); // Start Wire Library
  LCD.init(I2C_ADDR); // initialize the lcd with I2C Address
//delay(1000);
  // Store custom font data in 420CW Oled
  LCD.createChar(0, newChar1);
  LCD.createChar(1, newChar2);
  LCD.createChar(2, newChar3);
  LCD.createChar(3, newChar4);
  LCD.createChar(4, newChar5);
  LCD.createChar(5, newChar5);
  LCD.createChar(6, newChar5);
  LCD.createChar(7, newChar5);

   
   LCD.sendString("SETUP ()", 0, 0);    //("String", col, row)
   delay(1000);
}

void loop() {
  //////////// set Contrast test 1 ////////////////
  LCD.sendString("set Contrast test DW", 0, 0);    //("String", col, row)
    for (int Contrast = 255; Contrast > 0; Contrast = Contrast-- / 2 ) {
    LCD.setContrast(Contrast); // Set Contrast (Minimum allowed 0) 0~255
    LCD.setCursor(9,1); //(col,row)
    LCD.print(Contrast);
    LCD.print("    "); //clear
    delay(300); //delay not necessary as its sending a lot of Commands wich has delay inside the lib.
   
  }
  delay(2000); // 2sec. delay
 
  LCD.clearLcd();
  LCD.sendString("set Contrast test UP", 0, 0);    //("String", col, row)
  for (int Contrast = 0; Contrast < 510; Contrast = Contrast++ * 2 ) {
    LCD.setContrast(Contrast); // Set Contrast (Minimum allowed 0) 0~255
    LCD.cursPos(9,1); //(col,row)
    LCD.print(Contrast);
    LCD.print("    "); //clear
    delay(300); //delay not necessary as its sending a lot of Commands wich has delay inside the lib.
  }
  delay(2000); // 2sec. delay
  LCD.clearLcd();
 
  // Double Height(4-line) Test
  LCD.sendString("Double Height(4x20)", 0, 0);
  LCD.sendString("Test", 8, 1);
  delay(2000); // 2sec. delay
  LCD.setDoubleH(1);
  LCD.sendString("MODE 1", 0, 2);
  delay(1000); // 1sec. delay
  LCD.setDoubleH(2);
  LCD.sendString("MODE 2", 0, 2);
  delay(1000); // 1sec. delay
  LCD.setDoubleH(3);
  LCD.sendString("MODE 3", 0, 1);
  delay(1000); // 1sec. delay
  LCD.sendString("      ", 0, 1); // clear MODE 3 from Oled
 
  LCD.setDoubleH(4);
  LCD.sendString("MODE 4", 0, 2);
  delay(1000); // 1sec. delay
  LCD.setDoubleH(0); //back to 4x20
  LCD.sendString("MODE 0 (4-line)", 0, 2);
  delay(3000); // 3sec. delay
  LCD.clearLcd();

  //////////// Print custom font Using Wire.h Library test 2 ////////////////
  LCD.setDoubleH(3); // MODE 1
  LCD.sendString("Print custom font / Wire Library", 0, 0);    //("String", col, row)
  LCD.sendString("custom font -->", 0, 3);
  delay(3000); // 1sec. delay
  LCD.setDoubleH(0);
  delay(1000); // 1sec. delay
  LCD.cursPos(16,3); //(col,row) / set up position 
  // Test  / Print custom font 5 using Wire.h Library
  Wire.beginTransmission(I2C_ADDR); // start condition + Define I2C Address where the US2066 is, and set  R/W# bit to logic 0 write mode.
  // Print custom font
  Wire.write(B01000000); // control byte or the data byte, Co and D/C# bits following by six 0s.
  //Wire.write(B00000011); //DEC 3 // print custom font 4  [8bit (hi bit 0000/low bit 0011
  Wire.write(4); //DEC 4 / 5th custom font (Zero based array indexing.....)
  Wire.endTransmission(); // stop condition / 7) The write mode will be finished when a stop condition is applied.
  delay(3000); // 3sec. delay
  LCD.clearLcd();
  delay(1000); //1sec. delay

  //////////// Print 2 custom font Using LCD.sendData test 3 ////////////////
  LCD.sendString(" Print two custom   fonts / LCD.sendData", 0, 0);    //("String", col, row)
  LCD.cursPos(2,2); //(col,row)
  LCD.print("---> ");
  delay(1000); // 1sec. delay
  LCD.sendData(0); //Print custom font 1 (Zero based array indexing.....)
  LCD.print(" <---> ");
  delay(1000); // 1sec. delay
  LCD.sendData(char(1)); //Print custom font 2
  delay(3000); // 3sec. delay
  LCD.clearLcd();
  delay(1000);
 
  //LCD.scrollString("test", 2, 100);

  //////////// command test ////////////////
  /*
  sendCommand(0x2A);//function set (extended command set)
  sendCommand(0x71); //function selection A
  sendData(0x00);    //disable internal VDD regulator (2.8V I/O). data(0x5C) = enable regulator (5V I/O)
  sendCommand(0x28); //function set (fundamental sendCommand set)
  sendCommand(0x08); //display off, cursor off, blink off
  sendCommand(0x2A); //function set (extended command set)
  sendCommand(0x79); //OLED command set enabled
  sendCommand(0xD5); //set display clock divide ratio/oscillator frequency
  sendCommand(0x70); //set display clock divide ratio/oscillator frequency
  sendCommand(0x78); //OLED command set disabled
  sendCommand(0x09); //extended function set (4-lines) / last bit=1 : 3-line or 4-line display mode
  sendCommand(0x06); //COM SEG direction
  sendCommand(0x72); //function selection B
  sendData(B00000000);   //ROM CGRAM selection
  sendCommand(0x2A); //function set (extended command set)
  sendCommand(0x79); //OLED command set enabled
  sendCommand(0xDA); //set SEG pins hardware configuration
  sendCommand(0x10); //set SEG pins hardware configuration
  sendCommand(0xDC); //function selection C
  sendCommand(0x00); //function selection C
  sendCommand(0x81); //set contrast control
  sendCommand(0x7F); //set contrast control
  sendCommand(0xD9); //set phase length
  sendCommand(0xF1); //set phase length
  sendCommand(0xDB); //set VCOMH deselect level
  sendCommand(0x40); //set VCOMH deselect level
  sendCommand(0x78); //OLED command set disabled
  sendCommand(0x28); //function set (fundamental command set)
  sendCommand(0x01); //clear display
  sendCommand(0x80); //set DDRAM address to 0x00
  sendCommand(0x0C); //display ON

  delay(150);
  sendCommand(0x0C);  // **** Turn on Display
  delay(150);*/

 
 
 
 
}

--- End code ---

mprowe:
Thank you Chris,

Think I have installed the libraries & example correctly. But....

Isn't there always? Anyway, this is what I get (abridged) when I try to compile:

--- Code: ---
/home/mprowe/Arduino/libraries/OLedI2C/OLedI2C.h:22:8: note: void OLedI2C::init()
   void init();
        ^
/home/mprowe/Arduino/libraries/OLedI2C/OLedI2C.h:22:8: note:   candidate expects 0 arguments, 1 provided

--- End code ---

This is the faulting line (should I2C_ADDR be there?):

--- Code: ---  LCD.init(I2C_ADDR); // initialize the lcd with I2C Address
--- End code ---
There is also a call to LCD.print (line 98)  which is not in the library.

Regards, Martin

Navigation

[0] Message Index

[#] Next page

Go to full version