Example Code for Arduino-Display String

This article offers a comprehensive guide on displaying strings with Arduino using LCD12864 Shield and U8glib library. It covers necessary hardware and software preparations, wiring configurations, and provides sample code to help you achieve the best display effects. Learn how to configure contrast and rotate screen orientation with recommended settings.

Hardware Preparation

Software Preparation

Other Preparation Work

  • Please config the driving pin using this U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); command.. And notice to enable this command when using the u8glib example codes also.
  • Use "setContrast" to config the contrast as you want. We highly recommend you to setContrast to 0 to get the best display effect.
  • "setRot90/setRot180/setRot270" functions will be helpful to rotate the display direction as you want.Recommend to use setRot180.
  • For more useful lcd driving functions, please check u8glib userreference page.

Sample Code

#include "U8glib.h"

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, CD = 9, RST = 8


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 20, "www.DFRobot.com");
}

void setup(void) {
  u8g.setContrast(0); // Config the contrast to the best effect
  u8g.setRot180();// rotate screen, if required
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}

void loop(void) {
  // picture loop
  u8g.firstPage();
  do {
    draw();
  }
  while( u8g.nextPage() );

  // rebuild the picture after some delay
  delay(500);
}

Was this article helpful?

TOP