Notes About Working with Various Arduino & Netduino Microcontroller Boards

Thursday, November 1, 2012

SainSmart 12864 Graphic LCD Display & the Arduino Uno

The SainSmart 12864 128x64 graphic blue LCD display (available from Amazon for less than $20) doesn't come with any documentation, but the u8glib (Universal Graphics Library for 8 Bit Embedded Systems by Oliver Kraus) makes it easy to use via an SPI connection to the Arduino Uno R3. (Note: This does not work with the Arduino Due.)

 To use an SPI connection, use the pins in the hooded connection on the module. Make sure the switch on the other end of the board is set to SPI (not LPT). (The other switch controls the backlight.)

Note (9/7/2013):  With a slight modification, this example also works with the Arduino Leonardo or Arduino Due.  See the section near the bottom of the post.


Connections: 


LCD Module ---> Arduino UNO 
(Top Row) 
       SID ---> D11 
        CS ---> D12 
       SCK ---> D13

(Bottom) 
       GND ---> GND 
       --- ---> --- 
       VCC ---> V5

Code:


The following code just draws a couple basic shapes, but it shows how the library works.

#include <U8glib.h>

U8GLIB_ST7920_128X64 u8g(13, 11, 12, U8G_PIN_NONE);         

void draw(void) {
  u8g.drawCircle(20, 20, 10);
  u8g.drawDisc(30, 30, 10);
  u8g.drawLine(60, 30, 90, 50);
  u8g.drawBox(50, 50, 20, 10);
  u8g.drawFrame(70, 10, 10, 30);
}

void setup(void) { }

void loop(void) {
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  // rebuild the picture after some delay
  delay(500);
}


See the documentation in the wiki for more on the functionality of the library.

Arduino Due or Arduino Leonardo:


Note: As noted in the discussion below, with the Arduino Due or Leonardo, be sure to use ver. 1.11 (or later) of u8glib and use the following constructor (replaces 2nd line of code):

U8GLIB_ST7920_128X64_1X u8g(13, 11, 12);

18 comments:

  1. Thanks, Brad. This blog is the best get-started resource I found for the 12864. I just bought one on Amazon and you get me going right away, out of the box.

    I took the liberty of referencing your blog on Amazon. Thanks for your contribution.

    ReplyDelete
    Replies
    1. Glad to hear that you found it useful.

      Delete
    2. Thanks again for the quickstart. I've figured out the u8glib now - pretty powerful.

      Question: have you figured out a way to turn the 12864 backlight on/off from the software? It would be nice to be able to control that light programatically.

      Delete
    3. Hi Thor -

      Using the SPI interface (as in the example), I don't see any way that you can. It looks like LCD pins 15 & 16 are connected to the BL switch, since there are only 14 pins for the parallel interface, so I don't think you could get it to work with the parallel interface, either.

      Delete
  2. Thanks, Brad. You're cool. I am having fun with this display - appreciate the help.

    ReplyDelete
  3. In got the display light up but Arduino code don't verify.
    It says "U8G_PIN_NONE' was not declared in this scope"
    Please advise.
    Thank you.
    -cl

    ReplyDelete
    Replies
    1. Hi Carlos -

      I've tried the current version of the u8glib (ver. 1.13) and a version from last year (ver. 1.09, which I used for this post), but I cannot reproduce the error you are receiving. I am using Arduino IDE ver. 1.5.1r2 with an Uno. Which versions are you using?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Working now but is there more info to further program the display?

      Delete
    4. Hi Carlos -

      The best documentation for the library is on the u8glib wiki: https://code.google.com/p/u8glib/wiki/u8glib?tm=6.

      Delete
  4. Finally worked after downloading the library a few more times. I have no idea why? - Thanks for the tutorial.

    ReplyDelete
  5. Hmm, so how would this differ for use with the Due? Thank you for the write up

    ReplyDelete
    Replies
    1. Hi Incognition -

      It looks like there was some work on the u8glib library last spring with ver. 1.11 to get some support for the Arduino Due (https://code.google.com/p/u8glib/), but I haven't tried it lately. It is definitely worth looking into.

      --Brad


      Delete
    2. Hi Incognition -

      Using ver. 1.13 of the u8glib, I've been able to get the code to work with the Arduino Due using the following constructor:

      U8GLIB_ST7920_128X64_1X u8g(13, 11, 12);

      Delete
  6. This works for the Mega1280/2560 switching 11-13 with 50-52 too. See http://arduino.cc/en/Reference/SPI. Cheers

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. Works exactly as described on a Nano - perfect introduction to get the screen up and running for the first time - thank you!

    ReplyDelete