Page 1 of 2

Serial Graphic LCD 128x64 Arduino Code

Posted: Sun Mar 21, 2010 12:30 pm
by hopper1068
Code: Select all
#include <NewSoftSerial.h>

// Graphic Serial LCD 128x64
// Arduino Pin 8 (RX) to LCD Pin TX (NOT USED, NOT NECESSARY)
// Arduino Pin 9 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you're powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd
NewSoftSerial lcdSerialPort(8, 9);

void setup() {
  setBaudRate(9600);

  clearScreen();

  // Set a dim display
  setBackgroundBrightness(1);

  // Note how by default it prints at upper left
  print("hello");

  // Note that 0,0 is at lower left side of display
  setX(5);
  setY(10);
  print("world");

  // Draw a big X
  drawLine(0, 0, 127, 63, 1);
  drawLine(127, 0, 0, 63, 1);

  // Draw a centered circle
  drawCircle(63, 31, 31, 1);

  // Draw a box in the center
  drawBox(31, 15, 95, 47, 1);
} 

void loop()
{
}

void print(char *data){
  lcdSerialPort.print(data);
}

void clearScreen(){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x00,BYTE); 
}

void demo(){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x04,BYTE); 
}

void toggleSplashScreen(){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x13,BYTE); 
}

void setBackgroundBrightness(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x02,BYTE);
  lcdSerialPort.print(value,BYTE);
}

void setBaudRate(long value){
  // Get the internal reference for this baud rate
  char *ref = " ";
  if(value == 4800) 
    ref = "1";
  else if(value == 9600)
    ref = "2";
  else if(value == 19200)
    ref = "3";
  else if(value == 38400)
    ref = "4";
  else if(value == 57600)
    ref = "5";
  else if(value == 115200)
    ref = "6";
  else
    return;

  // Since it often rolls back to 115200, try setting it via 115200 first
  lcdSerialPort.begin(115200);
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x07,BYTE);
  lcdSerialPort.print(ref);

  // Now change the serial port to the desired rate, and set it again.
  lcdSerialPort.begin(value);
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x07,BYTE);
  lcdSerialPort.print(ref);
}

void setX(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x18,BYTE);
  lcdSerialPort.print(value,BYTE);
}

void setY(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x19,BYTE);
  lcdSerialPort.print(value,BYTE);
}

void setPixel(byte state){
  lcdSerialPort.print(0x50,BYTE);
  lcdSerialPort.print(0x40,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x0C,BYTE);
  lcdSerialPort.print(startX,BYTE);
  lcdSerialPort.print(startY,BYTE);
  lcdSerialPort.print(endX,BYTE);
  lcdSerialPort.print(endY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawCircle(byte startX, byte startY, byte radius, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x03,BYTE);
  lcdSerialPort.print(startX,BYTE);
  lcdSerialPort.print(startY,BYTE);
  lcdSerialPort.print(radius,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x0F,BYTE);
  lcdSerialPort.print(topLeftX,BYTE);
  lcdSerialPort.print(topLeftY,BYTE);
  lcdSerialPort.print(bottomRightX,BYTE);
  lcdSerialPort.print(bottomRightY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x05,BYTE);
  lcdSerialPort.print(topLeftX,BYTE);
  lcdSerialPort.print(topLeftY,BYTE);
  lcdSerialPort.print(bottomRightX,BYTE);
  lcdSerialPort.print(bottomRightY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Mon Nov 15, 2010 9:26 pm
by checkers1811
I see that this is an older post, but is there a chance that the author is still able to respond with the hardware setup that was used?

Exactly which LCD display was this software written for?

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Tue Nov 16, 2010 6:14 am
by hopper1068

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Thu May 05, 2011 4:21 pm
by Laminar
Dredging up an old post, but I can't for the life of me find a solid example of someone using the 128x64 Serial-enabled GLCD with an Arduino. Apparently people are replacing the Sparkfun firmware but until I get an AVR programming solution I'd like to mess around a bit. This code is the closest I can come to plug-and-play code that I can modify and play around with to teach myself how this all works.

I'm using a Duemilanove (serial ports are 0 (RX) and 1 (TX)).

Can someone add some more comments to the code so I know what's going on? I have the screen connected to the Arduino as described and substituted "NewSoftSerial lcdSerialPort(0, 1);" into the code to account for the location of my serial port. I have the NewSoftSerial library installed into my Arduino/libraries folder.

I've tried the NewSoftSerialTest sketch as well and I see nothing after the Sparkfun logo splash.

I'm sure there's just a stupid mistake that I'm making that can be easily rectified.

FWIW, this Hello World script does work.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Thu May 05, 2011 5:15 pm
by Laminar
Making progress. This code:
// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott

#include <Servo.h>
#include <NewSoftSerial.h>

Servo myservo; // create servo object to control a servo
 
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
 
void setup()

  Serial.begin(115200);
  setBackgroundBrightness(1);
  setX(5);
  setY(10);
  print("world");
  myservo.attach(9); // attaches the servo on pin 9 to the servo object


NewSoftSerial lcdSerialPort(0, 1);

void clearScreen(){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x00,BYTE);
}

void print(char *data){
  lcdSerialPort.print(data);
}

void setBackgroundBrightness(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x02,BYTE);
  lcdSerialPort.print(value,BYTE);
}
 
void setX(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x18,BYTE);
  lcdSerialPort.print(value,BYTE);
}

void setY(byte value){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x19,BYTE);
  lcdSerialPort.print(value,BYTE);
}

void setPixel(byte state){
  lcdSerialPort.print(0x50,BYTE);
  lcdSerialPort.print(0x40,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x0C,BYTE);
  lcdSerialPort.print(startX,BYTE);
  lcdSerialPort.print(startY,BYTE);
  lcdSerialPort.print(endX,BYTE);
  lcdSerialPort.print(endY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawCircle(byte startX, byte startY, byte radius, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x03,BYTE);
  lcdSerialPort.print(startX,BYTE);
  lcdSerialPort.print(startY,BYTE);
  lcdSerialPort.print(radius,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x0F,BYTE);
  lcdSerialPort.print(topLeftX,BYTE);
  lcdSerialPort.print(topLeftY,BYTE);
  lcdSerialPort.print(bottomRightX,BYTE);
  lcdSerialPort.print(bottomRightY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  lcdSerialPort.print(0x7C,BYTE);
  lcdSerialPort.print(0x05,BYTE);
  lcdSerialPort.print(topLeftX,BYTE);
  lcdSerialPort.print(topLeftY,BYTE);
  lcdSerialPort.print(bottomRightX,BYTE);
  lcdSerialPort.print(bottomRightY,BYTE);
  lcdSerialPort.print(state,BYTE);
}

void loop()

  val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val); // sets the servo position according to the scaled value
  Serial.print(val);
  Serial.print("-");
  clearScreen();
  delay(15);

Semi-works. It prints the following:

Image

I don't see any of the NewSoftSerial funcitons working. Thoughts?

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Thu May 05, 2011 7:05 pm
by hopper1068
Ports 0 and 1 are the ports that Arduino's built-in Serial library uses. Your code that calls Serial.print(val) works only because you are connected to pins 0 and 1. The problem with that, is that everytime you want to update your code, you have to disconnect the LCD from 0 and 1 or it interferes with the upload from the Arduino IDE.

To get around this, we use NewSoftSerial. It is very similar to the built-in Serial, except that you can use pins other than 0 and 1.

So....
1. Move your NewSoftSerial lcdSerialPort(0, 1) to some other pins like NewSoftSerial lcdSerialPort(8, 9). Move your LCD wires to 8 and 9 also.
2. Replace your Serial.print(val) statement with lcdSerialPort.print(val);

Now you can update your code without disconnecting the LCD. And you can take advantage of the convenience functions that I created (drawBox(), drawCircle(), etc.)
Additionally, you can send debugging output to Serial.print() and you can see them in the Arduino IDE's serial port monitor.

Note that there's been a lot of activity in the comments under the product. You might read through them here: http://www.sparkfun.com/products/9351

I got it up and running pretty quick. If memory serves, there was something quirky about the baud rate. Check into that if it doesn't work.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 06, 2011 7:16 am
by Laminar
hopper1068 wrote:Ports 0 and 1 are the ports that Arduino's built-in Serial library uses. Your code that calls Serial.print(val) works only because you are connected to pins 0 and 1. The problem with that, is that everytime you want to update your code, you have to disconnect the LCD from 0 and 1 or it interferes with the upload from the Arduino IDE.

To get around this, we use NewSoftSerial. It is very similar to the built-in Serial, except that you can use pins other than 0 and 1.
Thanks for the explanation here, this makes sense. I had been disconnecting the TX line per a recommendation read on the Sparkfun product comments, now I know how to get around that.
So....
1. Move your NewSoftSerial lcdSerialPort(0, 1) to some other pins like NewSoftSerial lcdSerialPort(8, 9). Move your LCD wires to 8 and 9 also.
2. Replace your Serial.print(val) statement with lcdSerialPort.print(val);

Now you can update your code without disconnecting the LCD. And you can take advantage of the convenience functions that I created (drawBox(), drawCircle(), etc.)
Additionally, you can send debugging output to Serial.print() and you can see them in the Arduino IDE's serial port monitor.
Is there a reason that the clearScreen() in my code doesn't work?
Note that there's been a lot of activity in the comments under the product. You might read through them here: http://www.sparkfun.com/products/9351

I got it up and running pretty quick. If memory serves, there was something quirky about the baud rate. Check into that if it doesn't work.
I've been through those comments and the comments on the standalone backpack a few times, most people recommend SummoningDark's firmware and there's not a ton of discussion using the screen as-is.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 06, 2011 2:39 pm
by hopper1068
Is there a reason that the clearScreen() in my code doesn't work?
Are you seeing any output? I just now took my original code from this thread, hooked up the LCD as described in the comments of that code, and it worked first try. You should see a big X, circle, box and hello world on the display.

I forgot that you need to power the LCD with 6-7vdc. Powering everything off of the Arduino pins (and therefore the USB line) won't work (only 5vdc). That's why I pump 7vdc into the Arduino's external power, and then power the LCD off of the Raw or Vin pin next to Gnd (which spits out whatever voltage you're pumping in the external power jack).

The datasheet (http://www.sparkfun.com/datasheets/LCD/ ... LCD-v2.pdf) has a lot of good info on this board.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 06, 2011 4:25 pm
by Laminar
The whole example code is working for me (screen powered by the Arduino). It leaves a cursor at the end of "world."

Image

If I wanted to make some part of the screen constantly update with the value read from a pot, how would I do that? Do I need to redraw everything on the screen or can I update a small portion of the screen at once?

Huge thanks for all of the help so far.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 06, 2011 4:55 pm
by hopper1068
You're doing awesome, Lam. I'm at dinner so can't test but suspect you could do a gotoXY and print each time without clearing.

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 06, 2011 5:44 pm
by hopper1068
Back home. Forgot about the wierd x y coordinate system in this LCD. 0,0 starts at the lower left (vice upper left).

Here's an example program that is closer to your requirements. Replace my variable "x" with your pot reading code. You should see a number about mid-screen incrementing and resetting to 0.



#include <NewSoftSerial.h>

// Graphic Serial LCD 128x64
// Arduino Pin 8 (RX) to LCD Pin TX (NOT USED, NOT NECESSARY)
// Arduino Pin 9 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you're powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd
NewSoftSerial lcdSerialPort(8, 9);
int x = 0;
char buf[6];

void setup() {
setBaudRate(9600);

clearScreen();

// Set a dim display
setBackgroundBrightness(1);
}

void loop()
{
clearScreen();
setX(55);
setY(50);

// Convert integer x into a char array so we can pass it into print(char *);
itoa(x, buf, 10);

print(buf);

x = x+100;
if(x > 10000){
x = 0;
}

delay(200);
}

void print(char *data){
lcdSerialPort.print(data);
}

void clearScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x00,BYTE);
}

void demo(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x04,BYTE);
}

void toggleSplashScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x13,BYTE);
}

void setBackgroundBrightness(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x02,BYTE);
lcdSerialPort.print(value,BYTE);
}

void setBaudRate(long value){
// Get the internal reference for this baud rate
char *ref = " ";
if(value == 4800)
ref = "1";
else if(value == 9600)
ref = "2";
else if(value == 19200)
ref = "3";
else if(value == 38400)
ref = "4";
else if(value == 57600)
ref = "5";
else if(value == 115200)
ref = "6";
else
return;

// Since it often rolls back to 115200, try setting it via 115200 first
lcdSerialPort.begin(115200);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);

// Now change the serial port to the desired rate, and set it again.
lcdSerialPort.begin(value);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
}

void setX(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x18,BYTE);
lcdSerialPort.print(value,BYTE);
}

void setY(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x19,BYTE);
lcdSerialPort.print(value,BYTE);
}

void setPixel(byte state){
lcdSerialPort.print(0x50,BYTE);
lcdSerialPort.print(0x40,BYTE);
lcdSerialPort.print(state,BYTE);
}

void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0C,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(endX,BYTE);
lcdSerialPort.print(endY,BYTE);
lcdSerialPort.print(state,BYTE);
}

void drawCircle(byte startX, byte startY, byte radius, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x03,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(radius,BYTE);
lcdSerialPort.print(state,BYTE);
}

void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0F,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}

void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x05,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Sun May 08, 2011 8:10 pm
by hopper1068
Any luck, Laminar?

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Thu May 12, 2011 6:54 pm
by Sabesto
Have a look here: viewtopic.php?f=8&t=28317. Its a new library meant to go with this firmware: http://sourceforge.net/projects/serialglcd/

It will make the display work as it should, and with added functions...

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Fri May 13, 2011 4:50 am
by hopper1068
Very nice, Sebasto!

Makes it very easy to work with.

Well done!

Re: Serial Graphic LCD 128x64 Arduino Code

Posted: Sat May 14, 2011 10:31 pm
by Sabesto
hopper1068 wrote:Very nice, Sebasto!

Makes it very easy to work with.

Well done!
Thanks, i moved the library to sourceforge. I have also added a few more functions.

https://sourceforge.net/projects/serialglcdlib/