element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum How do I add a Liquid Crystal Display to my current project
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 37 replies
  • Answers 2 answers
  • Subscribers 410 subscribers
  • Views 4134 views
  • Users 0 members are here
  • x
  • 1
  • lcd_display
  • arduino
  • 16
Related

How do I add a Liquid Crystal Display to my current project

Former Member
Former Member over 12 years ago

Hello,

 

I recently completed, with some assistance from Coder27, Bill Abbot, Tony, and Mark Beckett the first part of my project ( http://www.element14.com/community/thread/23967?start=0&tstart=0 )

Now the second part - I am wanting to add a small 16 x 1 LCD to finish up my project (see coding below that I will be using)

I would like the LCD to simply show which direction a servo is pointing (as in degrees, such as 0°, 90°, 180°, 270°, or 360° or anywhere in between 0° to 360°)

Please be advised: I do know that arduino 1.0.4 has the LCD Scroll example, but I am sure I dont need the entire code they have.  Also, my LCD is only 16 x 1, not x 2.

Thanks,

~Anna

 

******** BEGINNING OF CODE *************

#include <Servo.h>

 

const int pan1 = 3;     // first servo

const int tilt1 = 5;    // second servo

const int pan2 = 9;     // third servo

const int tilt2 = 11;    // fourth servo

 

const int potpan1 = A0; // Joystick 1 Vertical signal

int val0;

const int pottilt1 = A1;   // Joystick 1 Horizontal signal

int val1;

const int potpan2 = A5;   // Joystick 1 Select signal

int val2;

const int pottilt2= A3;   // Joystick 2 Vertical signal

int val3;

 

int servoVal[4];        // variable to read the value from the analog pin

 

Servo mypan1;                                  // create servo object to control a servo

Servo mytilt1;    // create servo object to control a servo

Servo mypan2;                                  // create servo object to control a servo

Servo mytilt2;    // create servo object to control a servo

 

void setup()

{

 

  // Servo 

  mypan1.attach(pan1);  // attaches the servo

  mytilt1.attach(tilt1);  // attaches the servo

  mypan2.attach(pan2);  // attaches the servo

  mytilt2.attach(tilt2);  // attaches the servo

 

  // Inizialize Serial

  Serial.begin(57600);

}

 

void loop()

{

 

  outputPotentiometers();           // Read and output joystick values

 

  val0 = analogRead(potpan1);            // reads the value of the potentiometer (value between 0 and 1023)

  val0 = map(val0, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)

  mypan1.write(val0);                  // sets the servo position according to the scaled value

 

  //Serial.print("Value of PWM 3:   ");

  //Serial.println(val0);

  //Serial.print("\t");

 

  val1 = analogRead(pottilt1);            // i added

  val1 = map(val1, 0, 1023, 0, 179);     // i added

  mytilt1.write(val1);                  // i added 

 

  //Serial.print("Value of PWM 5:   ");

  //Serial.println(val1);

  //Serial.print("\t");

 

  val2 = analogRead(potpan2);            // i added

  val2 = map(val2, 0, 1023, 0, 179);     // i added

  mypan2.write(val2);                  // i added

 

  //Serial.print("Value of PWM 6:   ");

  //Serial.println(val2);

  //Serial.print("\t");

 

 

  val3 = analogRead(pottilt2);            // i added

  val3 = map(val3, 0, 1023, 0, 179);     // i added

  mytilt2.write(val3);                  // i added

 

  delay(20);                                      

}

 

void outputPotentiometers()     // Display Joystick Values

{

  Serial.print("Pot Pan 1 on ~3:     ");

  Serial.print(analogRead(potpan1));

  Serial.print("\t");

  Serial.print("     Pot Tilt 1 on ~5:     ");

  Serial.print(analogRead(pottilt1));

  Serial.print("\t");

  Serial.print("     Pot Pan 2 on ~9:     ");

  Serial.print(analogRead(potpan2));

  Serial.print("\t");

  Serial.print("     Pot Tilt 2 on ~11:     ");

  Serial.println(analogRead(pottilt2));

  Serial.print("\t");

 

}

 

 

 

********** END OF CODE *************

  • Sign in to reply
  • Cancel

Top Replies

  • ntewinkel
    ntewinkel over 12 years ago +1 suggested
    Here's a good example to start with: http://arduino.cc/en/Tutorial/LiquidCrystal You'd have to change the setup line to be lcd.begin(16,1) for your smaller display (and assuming the lcd lib supports it…
  • mcb1
    mcb1 over 12 years ago in reply to billabott +1 verified
    Annasta Your explanation does give us some clues where your learnng lies, and how you have come to be involved, so thanks. You may have noticed that there are many people asking for help, and a lot are…
  • Former Member
    Former Member over 12 years ago in reply to billabott +1
    If I might add a bit to WATB's reply, we get all sorts of questions here, and the kind of answer you want to give differs based on the motivation for the question. If someone is asking for help with what…
  • ntewinkel
    0 ntewinkel over 12 years ago in reply to Former Member

    Hi Annasta,

     

    You could simplify things by creating the strings for top and bottom line completely and then just printing those out on the display.

     

    I happened to still have my 16x2 display hooked up, so I tried the following, and it works nicely. You'd need to do this for both the top and the bottom line of course (my top line just says "hello, world!", which is from the example).

     

     

    char messagebuffer[17];  // 16 characters plus the null to terminate the string

     

    void setup() {

      // set up the LCD's number of columns and rows:

      lcd.begin(16, 2);

      // Print a message to the LCD.

      lcd.print("hello, world!");

     

      int t1 = 25;

      int t2 = 42;

      sprintf(messagebuffer, "T1 = %d, T2 = %d", t1, t2);  // Create the whole line at once. The %d bits get replaced by the values.

     

      lcd.setCursor(0, 1);

      lcd.print(messagebuffer);

    }

     

     

    To ensure that your display doesn't show leftover characters from a previous display you either have to make sure the message is always 16 characters, or set up a blank string of 16 spaces and print that to top and bottom lines to clear the display before showing your real messages.

     

    Hope that helps.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to ntewinkel

    Hi Nico -

    I was just looking over the example code File / Example / Liquid Crystal / Hello World

    It states that this

    "Demonstrates the use a 16x2 LCD display.  The LiquidCrystal

    library works with all LCD displays that are compatible with the

    Hitachi HD44780 driver. There are many of them out there, and you

    can usually tell them by the 16-pin interface."

     

    My liquid crystal dispaly is a serial LCD.

     

    To continue, I know you have been attempting to help me, but I really need the Kid glove treatment when it comes to programing.

    You stated:

    "I happened to still have my 16x2 display hooked up, so I tried the following, and it works nicely. You'd need to do this for both the top and the bottom line of course (my top line just says "hello, world!", which is from the example)."

     

    How did you hook it up?

    What worked nicely?

    Where do I begin to place this code?

     

    Sorry, but thats me.  I am trying.  But tossing a bunch of new code at me, is only causing me frustration image

     

    Would love your assistance, but I need to be spoonfed to understand. 

    Heck, toss in some code, and make errors.  I maybe able to figure them out on my own....

     

    ~Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to Former Member

    Annasta

    Have you tried the corrections I suggested?

     

    I think Nico was offering you an alternative way of sending the message, rather than the multi step method the original example you used.

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to ntewinkel

    Well, congratulations on the purchase of sparkfun.com's RTL-09877.  That is awesome image and do you know why?  Because we now have access to its data sheet and will be able to help to a maximum degree possible (as time allows).  https://www.sparkfun.com/datasheets/LCD/SerLCD_V2_5.PDF

     

    Did you know you need some male headers to make that connector on the serial LCD easy to use with the breadboard and ultimately a shield?  Oopsie, the quickstart says (Tip: if you want to connect the display to a breadboard, tin the ends of the wires to make them easier to insert into the breadboard holes. To tin wire, strip about 1/4", and put some solder on the bare wire to make it stiffer).

    So I assume that means there is only one connector and it gets attached to one of the LCD headers.  IMHO, they should have put connectors on both ends of the wiring harness.  Oh, well, pick your pins and solder it onto the shield board.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to billabott

    Hi Bill -

    Yeppers, I picked up the lcd and also I have headers here at the house already, they came with a package of female dupont wires....

    I also did get a proto shield:  http://arduino.cc/en/Main/ArduinoProtoShield

    Cost was $3.99, oh and on top of that...I purchased another Arduino UNO board for $12.99 (microcenter in troy michigan) http://www.microcenter.com/product/392614/Arduino_Uno_Rev_3 they ran a sale, 1 left when I got there.

     

    As far as the Serial LCD goes, I was informed that a novice would find this to be much easier to use.  I would have preferred to save some money and purchase the non serial unit, but......I figured the LCD was a secondary to the servos turning...so, I picked that up instead.

     

    ~Anna

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to Former Member

    I love shopping at Microcenter and Fry's, but the nearest ones to me are in Atlanta, GA. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to mcb1

    Hi Mark - this is long image

    I am working on it right now.  I am adding a few lines and will be reposting with different line code #'s.  I think its much easier to follow someone when line code #'s are used.  You will see that those #'s now start with line 10 and increase by 10 each line.

     

    I did, with your assistance, realize I didn’t change my copy paste code on the line

    “sending an integer value to the display at line 61 and again at line 69” yes I forgot to change that to p1 in line 69

    Those lines are now: 610 and 690

     

     

    “Shouldn't you be sending t1string and t2string from lines 50, 52 ??”

    T1 and P1 will work together as do T2 and P2

     

     

    "PLUS you are setting the cursor at the 16th postion (they count from 1) and then giving it a 4 digit number.  You should be setting it at position 4 for the t1, t2 values and position 12 for the p1, p2 values"

     

     

    1ST part: 16th position answer:

    According to what I looked up prior to posting:

     

    Position    1       2      3       4       5      6      7       8       9     10    11     12     13     14    15    16

    line 1128129130131132133134135136137138139140141142143
    line 2192193194195196197198199200201202203204205206207

     

     

     

     

    So with that, I would want the cursor to move on line 1 to position 135 thus allowing me to utilize 1-135 for T1, then allowing the cursor to begin again at position 9 (136)

    The same would be for line 2 (T2 and P2) with the exception the numbers would be 199 and 207 respectively

     

    2nd part: The 4 digit number is not a 4 digit #, but more lets say characters.  Being that a circle can only pan or tilt 360°

    I want to have enough room between the T’s to accommodate the max characters (3) with the ° sign in the max 4th position, then leaving a space between

    In this example I will be using the --- as a space

     

    Example:                                 T1: 90°--P1:45°--

                                                    T2:45°--P2:180°-

     

    Or                                           

    T1: 180°-P1:150°-

                                                    T2:160°-P2:90°--

     

     

    As for the encoder thing…No not going to do that.  I understand how the compass bearing works, and I plan on utilizing that.

     

     

    ~Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago

    updated code:

    // SparkFun Serial LCD example 2
    // Format and display fake RPM and temperature data

    // This sketch is for Arduino versions 1.0 and later
    // If you're using an Arduino version older than 1.0, use
    // the other example code available on the tutorial page.

    // Use the softwareserial library to create a new "soft" serial port
    // for the display. This prevents display corruption when uploading code.
    #include <SoftwareSerial.h>

    // Attach the serial display's RX line to digital pin 2
    SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused)

    void setup()
    {
      mySerial.begin(9600); // set up serial port for 9600 baud
      delay(500); // wait for display to boot up

      mySerial.write(254); // cursor to beginning of first line
      mySerial.write(128);

      //mySerial.write("RPM:            "); // clear display + legends
      //mySerial.write("TEMP:           ");
      mySerial.write("T1:     P1:     "); // clear display + legends
      mySerial.write("T2:     P2:     ");
    }

    //int temp, rpm;
    //char tempstring[10], rpmstring[10]; // create string arrays
    int t1, p1, t2, p2;
    //char t1[8], p1[8], t2[8], p2[8]; // create string arrays
    char t1string[8];
    char p1string[8];
    char t2string[8];
    char p2string[8];
    ;

    void loop()
    {
      //temp = random(1000); // make some fake data
      //rpm = random(10000);
      t1 = random(1000); // make some fake data
      p1 = random(1000);
      t2 = random(1000); // make some fake data
      p2 = random(10000);

      //sprintf(tempstring,"%4d",rpm); // create strings from the numbers
      //sprintf(rpmstring,"%4d",temp); // right-justify to 4 spaces
      sprintf(t1string,"%4d",t1); // create strings from the numbers
      sprintf(p1string,"%4d",p1); // right-justify to 4 spaces
      sprintf(t2string,"%4d",t2); // create strings from the numbers
      sprintf(p2string,"%4d",p2); // right-justify to 4 spaces

      //mySerial.write(254); // cursor to 7th position on first line
      //mySerial.write(134);
      mySerial.write(254); // cursor to 7th position on first line
      mySerial.write(143);

      //mySerial.write(rpmstring); // write out the RPM value
      mySerial.write(t1); // write out the RPM value

      //mySerial.write(254); // cursor to 7th position on second line
      //mySerial.write(198);
      mySerial.write(254); // cursor to 7th position on second line
      mySerial.write(207);

      //mySerial.write(tempstring); // write out the TEMP value
      mySerial.write(p1); // write out the TEMP value
    // lines below have been added to accomidate t2 and p2
      //mySerial.write(254);
      //mySerial.write(134);
      mySerial.write(254); //
      mySerial.write(198);

      //mySerial.write(rpmstring); // write out the RPM value
      mySerial.write(t2); // write out the RPM value

      //mySerial.write(254); // cursor to 7th position on first line
      //mySerial.write(134);
      mySerial.write(254); // cursor to 7th position on first line
      mySerial.write(200);

      //mySerial.write(rpmstring); // write out the RPM value
      mySerial.write(p2); // write out the RPM value

      delay(1000); // short delay
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to Former Member

    Annasta

    I give up explaining....

     

    Try this then compare it to your original code and see what is different.

     

     

     

    {

      mySerial.begin(9600); // set up serial port for 9600 baud

      delay(500); // wait for display to boot up

     

      mySerial.write(254); // cursor to beginning of first line

      mySerial.write(128);

     

      mySerial.write("T1:     P1:     "); // clear display + legends

      mySerial.write("T2:     P2:     ");

    }

     

    int t1, p1, t2, p2;

     

    char t1string[8];

    char p1string[8];

    char t2string[8];

    char p2string[8];

     

    void loop()

    {

     

      t1 = 90;   //fixed value

      p1 = 45;   //fixed value

      t2 = 45;    //fixed value

      p2 = 180;   //fixed value

     

     

      sprintf(t1string,"%4d",t1); // create strings from the numbers

      sprintf(p1string,"%4d",p1); // right-justify to 4 spaces

      sprintf(t2string,"%4d",t2); // create strings from the numbers

      sprintf(p2string,"%4d",p2); // right-justify to 4 spaces

     

     

      mySerial.write(254); // cursor to 4th position on first line

      mySerial.write(131);

      mySerial.write(t1string); // write out the t1 value AFTER it was made into a string

     

      mySerial.write(254); // cursor to 11th position on first line

      mySerial.write(138);

      mySerial.write(p1string); // write out the p1 value AFTER it was made into a string

     

      mySerial.write(254); // cursor to 4th position on second line

      mySerial.write(195);

      mySerial.write(t2string); // write out the t2 value AFTER it was made into a string

     

      mySerial.write(254); // cursor to 11th position on second line

      mySerial.write(202);

      mySerial.write(p2string); // write out the p2 value AFTER it was made into a string

     

      delay(1000); // short delay

    }

     

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 12 years ago in reply to mcb1

    I just realized that there is a SerLCD library that might make life easier:

     

    http://playground.arduino.cc/Code/SerLCD

     

    The sample they include is a bit much, but you should be able to run the regular LiquidCrystal example with a minor modification like this:

     

    #include <serLCD.h>

     

    // Set pin to the LCD's rxPin

    int pin = 2;

     

    serLCD lcd(pin);

     

    void setup() {

     

      lcd.clear();

      // Print a message to the LCD.

      lcd.print("hello, world!");

    }

     

    void loop() {

    // set the cursor to column 0, line 1

      // (note: line 1 is the second row, since counting begins with 0):

      lcd.setCursor(0, 1);

      // print the number of seconds since reset:

      lcd.print(millis()/1000);

    }

     

     

    Then you could sprintf into String buffers, and do something like this:

     

      lcd.setCursor(0, 0);

      lcd.print("Top Line");

      lcd.setCursor(0, 1);

      lcd.print("Bottom Line");

     

    I'm sorry I don't have more time to be more detailed - sort of trying to help while getting my own work done!

     

    I recommend trying this as a new separate test sketch - don't change what you already have. That's in case it doesn't work as advertised, plus it also helps to get an understanding of things first.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube