analog in to 4 bit LCD driver

Hi Dave,

Thanks for the 4-line display tip.

As it stands, does that 4bit library work for your 4-line LCD display without changing the code, if you just run it in 1 or 2 line mode and don't try to access the other positions?

I'm not too worried about the cursorTo() function yet. I imagine a lot of people would comment that function out to save space, anyway. If we can't easily make an across-the-board implementation of it, so be it.

The HD44780 standard is probably as much as I'd have the LCD4Bit library support, for now, and it doesn't seem to cover >2-line displays. People can always send a custom command to change the display mode AFTER lcd.init(), I guess. My understanding is that the only crucial thing that HAS to be right in a 4-bit library is the sequence indicating 4-bit (not 8-bit). If that's wrong then all subsequent commands are incorrectly interpreted.
Do you have a link to your 4-line LCD's datasheet?

One thing I'm keen on is that eventually there's a direct mapping between what's written on a datasheet and what's being done in our init(). It's so much better, for learners in particular, when one can follow that link and understand all the code, line for line. I wasted energy following my component-specific datasheet in developing the 4-bit version of the lib, but I'll come back to the code this week and try to find a definitive source for the HD standard.

LCD-tech aside, one small thing I noted in your code was the extra work you were doing to convert the multidigit decimal number to a string on the lcd.

You can use the standard function atoi() to convert an integer to a string. (That function isn't big, unlike sprintf, which uses about 1.5k!)

  int heat = 451;      
  char heat_str[4] = "   ";  //reserve the string space first
  itoa(heat, heat_str, 10);
  lcd.printIn(heat_str);

  //todo: make sure number can be notated within str length.

This assumes you make a string-printing function (here printIn()) for the lcd. This can simply be a wrapper to multiple calls to your character printing fn. (As seen in the original tutorial).

Neill

Hey Neill,
I haven't tried your 4 bit library yet. The newer init code I posted above works fine for either a 2 or 4 line display. A quick glance at the excellent links you have on the LCD's processor's internal init functions leads me to believe that it probably isn't necessary to do a hard init of an LCD unless you want it to do a mode other than its native format (ie getting a 2 line display to only do 1 line). I don't have a 1 line display so I can't check that configuration. Thanks for the tip on the integer to string function. One thing I do enjoy about the Arduino is actually having to worry about keeping code lean and mean! Reminds me of the "good" old days with my Timex Sinclair :wink:

I could see people either using the "clear screen" or "reset to home" commands depending on what they want to do. "reset to home" sometimes looks nicer because it doesn't have the slight flicker that the "clear screen" function has.

Another interesting thing I found this weekend, after writing a little mac app to talk to the Arduino and set stuff on the LCD (if anyone's interested I'd be happy to post it) is that the serial(USB) port only seems to take in 65 bytes max at any one time? Is this a limitation of the hardware or the serial library?

At this rate, I might not get a chance to mess around with this until the weekend, but I've got plenty of stuff to read and try, thanks to you. Good show.

Dave

i gave me a try on an LCD display today. but i failed :cry:
the display just got damn hot and nothing else. guess it's broken now, so i will buy a new one and try again. :-/
i need the LCD for a present that has to be ready until first days of december, so i am kind of under pressure :wink:

just wanted to say: AWESOME THREAD!! :slight_smile:

Thanx for your wiki link neillzero :slight_smile:

just wondered what LCD display I should get to start with.....

Hi Westbam,

Where are you? In the UK I recently bought a maplin "N27AZ" 16x2 with a backlight - expensive at 10 pounds. That's what I tested the 4-bit library against during development. Note that these seem to be web-only components at maplin now.

I plan to make a short tutorial about a simple device I made which incorporates this display (but we all make plans...)

If size is a factor, I'd be tempted to try to make do with a 1x8, as the 16 character displays are significantly longer than your arduino board. Then again, it's nice to have a bit more space on the display for debugging a lot of stuff (when serial is tied up).

A backlight is going to drain a battery more quickly, but normally you can decide not to wire it up.

I'd get something cheap for your first build. Then you'll learn what you can live with and what you're really missing.

Make sure it is HD44780-compatible.

I was able to get the LiquidCrystal library to work okay with this:

Since it only works with 1 of the 2 lines, I wanted to try the LCD4Bit library instead.
(and of course I'd like to use less pins)

The example doesn't work - I haven't changed any of the wiring but looking at the code,
it seems that it uses the same pines as LiquidCrystal.

Anyone else used this specific display or am I out of luck with LCD4Bit?

Thanks for any info,
Rob

Hi Rob, I wrote the 4-bit library, so would be keen to help you get it to work. I've only tested it so far on two devices (and they were from the same family).

I think maybe you've not tied RW low. There are two things to watch for when using this library:

  1. get the pins right (In your case, I don't think this is the problem)
  2. tie RW low

1. Get the pins right:
I intentionally kept the pins the same between the LiquidCrystal library and 4BitLCD library, so if you have your circuit working with LiquidCrystal library, you should have to make only two changes:

  • unplug the four unused databus lines DB0, DB1, DB2, DB3.
  • tie the LCD's RW input to ground (see point #2 below).

Here are the default arduino digital pins the 4bit library expects you to use. They can be found in LCD4Bit.cpp.

int RS = 12;
int RW = 11; //irrelevant if USING_RW is false.
int Enable = 2;
//4-bit data bus should be a sequence of pins
int DB[] = {7, 8, 9, 10}; //wire these to DB4~7 on LCD.

Ideally, when someone creates an instance of LCD4Bit, they should be able to specify the pins they want to use. Early days.

2. Tie RW low.
I wrote the 4-bit library for people who want to save on arduino pins. As most applications only need to write data to the LCD and never read, we can save another pin by hard-wiring the LCD RW input to low (or whatever indicates a write-operation in your LCD data sheet). The LCD4bit library expects this wiring and won't try to control the RW pin (by default). You can disable this behaviour in LCD4bit.cpp by finding the line
int USING_RW = false;
and setting it to true.

Making changes to the library
If you make changes in LCD4Bit.cpp, be sure to delete LCD4Bit.o before recompiling your arduino sketch - this will force recompilation of the library.

GDM1602K datasheet
By the way, looks like there's a more comprehensive data sheet for that product here:
http://www.xmocular.com/pdf/GDM1602K.pdf
not that you should need it.

What else to try
If the above ideas don't help, try using glasspusher or massimo's 4-bit code, too. This would give us the confidence that we can get your display working somehow, and I'll look at the 4bit library initialisation code again as I have a feeling it's not perfect. If we can get the LCD4Bit library tested successfully on many devices, we should be able to distribute it with the arduino IDE alongside the main LiquidCrystal library, making this much easier for all the other arduino users.

All comments here apply to LCD4Bit library v0.1 (16th Oct 2006). YMMV with other versions.

General LCD documentation on the arduino playground:
http://www.arduino.cc/playground/Code/LCD

good luck for now!
neill

hey neill,

thanks a lot for the info - sorry it took so long to take another stab at it.
i definitely did not have RW tied low so i made that change and your random fruit example is working now!
you can add this device to your HCL i guess. :slight_smile: (at least, it works with one of the two display lines)

also, being new to datasheets (and microcontrollers, and electronics, and...), i'm having a hard time figuring out how to get the backlighting for this thing to work. the datasheet is sort of confusing to me - it seems like I can use pins 1&2 OR 15&16 OR the two pads on the side of the display?

also, it seems to say that it wants 4.2V for the backlight. i had read somewhere that i could use the ~.7V voltage drop of pretty much any diode to get me in that range from our normal 5V. my multimeter is saying that i'm at 5.06V without a diode and 4.8V with the diodes i've tried - a little too high? maybe i've already fried the backlighting because i think i may have given it the 4.8V while i was playing around - i remember seeing backlighting for a moment at some point. :slight_smile:

thanks again for the help,
rob

Hi Rob,
Great!
So it was really my fault for not documenting that tie-low requirement. Ouch. Sorry for the wasted time. I've just now improved the documentation with a wiki page here:
http://www.arduino.cc/playground/Code/LCD4BitLibrary
Please feel free to add corrections and improvements.

Regarding the backlight questions, I haven't used one of those displays but have a look at landon's useful post in this recent thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1164517397/3#3

neill

your library is excelent,but I have a little question, how can I visualize the ADC in Decimal format?, Now I can see the result but only in ASCII code, how can I translate it to Decimal?

thanks fpr your answer and exuse mefor my bad english

greetings from Colombia !

:stuck_out_tongue: I forgot another question...

I need to modified the library in order to use the LCD from 2 to 8 pin.. but when I make the changes it doesn`t work if I delete the .o file it doesn´t apear again...

thanks for the answer

Hi Alvaro,
If I understand your first question correctly, you want to print your number in decimal on the LCD.

See the section "Printing Numbers" on the LCD wiki page:
http://www.arduino.cc/playground/Code/LCD

Remember that the LCD library print() functions accept single ascii characters or strings of characters. So first you have to convert your number into a string representation. The wiki section above shows how to do this using the itoa() function.

It might make sense for the LCD library to provide another function, say printNumber(int) which took an integer and printed its string representation in decimal.

neillzero

First off, kudo's for the instructions in the 4bit playground page. If I had been paying attention to the datasheet I found for my display, I'd have got it working the first time :slight_smile: (For some reason the pin numbering is ass-backwards on my LCD). Still, I have a small question. For now, the random fruit example works perfectly. Single line, scrolling, all the bits. But, if I enable the 2nd line and the commented out 'score' bit the display stays empty. No noise, no nothing. Backlight comes on, but no character appear. Am I missing something, or have my earlier attempts killed just the second line? The display is a pc1602f, which should be hd44780 compatible.

(excuse me for lifting this old thread from the depths of oblivion)

Thanks!
Andre

thanx a lot!

Hello Neillzero.

Congratulation by LCD4bit lib. I'm using and it's very very custom.

But, I have a suggestion:

Currently, I formatted my computer, but the pins had changed the lib in itself. cpp.

So when I installed again my system and upload to arduino, my display doesn't worked.

So again I edit in your CPP the pins.

You should create methods to set the number of pins.

Thanks

Hey Neilzero..

Ive adapted your code into my little program, but can't find out how to make it show the temperature as 26,1 as oposed to the 261 format it is in now ?!?! Im sure you have a solution, I just can't see it since i've only been into arduino programming for about 4 days now :-/

My code is at the bottom.

  int heat = 451;      

char heat_str[4] = "   ";  //reserve the string space first
 itoa(heat, heat_str, 10);
 lcd.printIn(heat_str);

//todo: make sure number can be notated within str length.




My code..


#include <LCD4Bit.h>

LCD4Bit lcd = LCD4Bit(2);
int temperaturePin = 2;    // select the input pin for the temperature

void setup() {
 lcd.init();
}

void loop() {

lcd.clear();
 lcd.printIn("Vandtemperatur :");
 delay(1000);

int i, val = 0;
 
 for(i = 0; i < 20; ++i)
 {
      val += analogRead(temperaturePin);    // read the value from the sensor
      delay(50);
 }
 
 val /= 4.06;      // conversion value to millivolts
  if(val > 28 * 10)   // temperature in deg C times 10, since we're measuring to tenths of a degree
    digitalWrite(13,HIGH);
  else
    digitalWrite(13,LOW);
   
    int heat = val;      
 char heat_str[5] = "    ";  //reserve the string space first
 itoa(val, heat_str, 10);

lcd.cursorTo(2, 5);  //line=2, x=6.
 lcd.printIn(heat_str);
 delay(1000);
 }




Thanks... Martin

neillzero,
Thanks so much for the trouble shooting help! I've been sitting here looking over the wiring over and over and over, wondering what could be wrong.
Turns out, once I delete LCD4bit.o and recompiled the code, it worked perfectly! I'm glad I found your post, maybe this should be in the wiki? Maybe it is and I missed it!
Thanks again!

My 2 playing with 4 bits LCD...

I always use LCD4Bits library...but i build it with a new LCD WH1602, and do not work...
in the Lib note this:

// ########## pin assignment ##########
int RS = 12; // Register Select
int RW = 11; // Read/Write
int En = 2; // Enable

//DB should be an unseparated group of pins - because of lazy coding in push_nibble()
int DB[] = {7, 8, 9, 10}; // DB4 .. DB7

That on the default lib, my Lib:
// ########## pin assignment ##########
int RS = 3; // Register Select
int RW = 11; // Read/Write
int En = 2; // Enable

//DB should be an unseparated group of pins - because of lazy coding in push_nibble()
int DB[] = {8, 5, 7, 6}; // DB4 .. DB7 :o

So this is not working... could be the pin assignment?

Best Regards!
Frank

Hey Neilzero..

Ive adapted your code into my little program, but can't find out how to make it show the temperature as 26,1 as oposed to the 261 format it is in now ?!?! Im sure you have a solution, I just can't see it since i've only been into arduino programming for about 4 days now :-/

Solow, read the attached post and you will get it.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1218132924/8#8

Hey,
im using this lcd http://www.sparkfun.com/commerce/product_info.php?products_id=709
and i cant get it to work, i followed your post Arduino Playground - LCD4BitLibrary
is it my lcd isnt compatible or am i doing something wrong?
Thanks