Introduction: LCD Shifter for Arduino

The original idea was to create a library that simplify the use of IC 74HC595 between Arduino and other hardware. In this Instructable I will share this to you using as example the control of a 16x2 LCD.
The example will show on the LCD the seconds that has passed since Arduino was restarted.

I hope it will be useful to you.

What do you need for THIS example?
- Arduino
- Arduino IDE installed
- LCD
- One IC 74HC595
- One 4.7Kohm resistor or similar
- One "104" capacitor
- Wires!

Step 1: Place the Library Under Arduino Folder

I've named the library "ShiftOut". It goes under %arduino-directory%/hardware/libraries

This one is the library that I've programmed. Comments are welcome.

Step 2: LCD Library

The second library needed is the one that communicate to the LCD. I've used this one and not the one that came with Arduino because it's an initialisation bug.

It's based on www.slashdev.ca/arduino-lcd-library/ and has the necessary changes to integrate the ShiftOut Library that I made.

This must be uncompressed under %arduino-directory%/hardware/libraries too.



Step 3: Open Arduino IDE

Now it's time to write the code. Open Arduino IDE and write this:

#include <Lcd.h>
#include
<ShiftOut.h>

ShiftOut sOut(8, 12, 11, 1);
Lcd lcd = Lcd(16, FUNCTION_4BIT | FUNCTION_2LINE | FUNCTION_5x11, &sOut);
 
void setup()
{
  lcd.set_ctrl_pins(CTRLPINS(1,2,3)); // RS->1, RW->2, E->3
  lcd.set_data_pins(_4PINS(4,5,6,7)); // D4->4, D5->5, D6->6, D7->7
 
  lcd.setup();
 
  lcd.clear();
}
 
void loop()
{
  lcd.home();
  lcd.print((long)millis() / 1000);
}

This simple sketch shows on the LCD the seconds that has passed since Arduino was restarted.


Step 4: Compilation

It's important that the libraries are copied before Arduino IDE is open. Otherwise the compilation could fail.

If everything was OK, you could connect Arduino to a 74HC595 and this one to a LCD following the schematic images diagrammed using Fritzing.

The connection should be as follow:

Step 5: Run the Sketch on Arduino

If everything is connected right, you should see the counting seconds on the LCD.





Step 6: Conclusion

I hope this library will be useful for someone. It's to me because the Arduino code gets simple and nice, without filling it with collateral coding messing the sketch main purpose.

Regards!

Step 7: Bonus Track: Another Example

Here is Arduino using ShiftOut to control two seven segment displays in cascade:




More info could be found here: http://gusps.blogspot.com/

Comments

author
Struggler made it!(author)2012-03-12

Hi,
Having problems compiling this script example.
I might have missed something vital?
Have the libraries saved in the location specified.
Using Arduino UNO R3 and 1.0 IDE

Here are the error messages...
Its probably something simple on my part but can't work out what it is.

Any help appreciated.

Thanks
Struggler

C:\arduino-1.0\libraries\Lcd\Lcd.cpp:26:27: error: WConstants.h: No such file or directory
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::send(uint8_t, uint8_t)':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:65: error: 'delay' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::set_enable(uint8_t)':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:113: error: 'delayMicroseconds' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::pulse_enable()':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:119: error: 'HIGH' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:120: error: 'delayMicroseconds' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:121: error: 'LOW' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::check_bf()':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:131: error: 'INPUT' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:136: error: 'HIGH' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:137: error: 'digitalRead' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:138: error: 'LOW' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:148: error: 'OUTPUT' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::setup()':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:174: error: 'OUTPUT' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:180: error: 'OUTPUT' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:186: error: 'delay' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:195: error: 'delayMicroseconds' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:206: error: 'delayMicroseconds' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::dWrite(uint8_t, uint8_t)':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:348: error: 'digitalWrite' was not declared in this scope
C:\arduino-1.0\libraries\Lcd\Lcd.cpp: In member function 'void Lcd::pMode(uint8_t, uint8_t)':
C:\arduino-1.0\libraries\Lcd\Lcd.cpp:355: error: 'pinMode' was not declared in this scope

author
GusPS made it!(author)2012-03-12

Hi. It seems that the installation have some issues. Does other projects work well?
If "yes", maybe this instructions are not compatible with Arduino UNO R3. I remember to did this with Arduino 0015 or 0017. Try to use it instead.

Maybe other user can confirm if this works on Arduino UNO IDE.

I'll do the tests one of these days and tell you if it worked.

Thanks.

author
Dominion-Network made it!(author)2012-03-02

Thanks to your instructable (This one) I've managed to follow it to the letter and I've produced this.

Thank you

Am-OUGACMAEiXKL.jpg large.jpg
author
GusPS made it!(author)2012-03-02

Oww!!... Thanks!. I really apreciate your comment. Makes me willing to do another instructable ;p

Regards!...

author
marker4455 made it!(author)2012-01-27

The Code provided up above gives me an error, while compiling/verifying. Please Help.

author
marker4455 made it!(author)2012-01-27

I meant in Step 3, of this tutorial.

author
GusPS made it!(author)2012-02-01

Hi marker4455. Which error do you get?

author
agodinhost made it!(author)2012-01-01

I've Forgot to mention that I'm using arduino 0022.

author
GusPS made it!(author)2012-01-01

I guess that the IDE version doesn't matter in this case.

Can you explain the problem with some more details?
Thanks.

author
agodinhost made it!(author)2012-01-01

I've built a simple header for this project and everything seems to be fine, it works perfect. However, when using ONLY the 5v power supply, it don't work out - in this case the arduino is not connected to the PC. I saw your comment about the initialization bug in the original liquid crystal library and I'm wondering if it's not the cause of this issue ...

Please?

author
GusPS made it!(author)2012-01-01

But, are you powering Arduino with an external 9v and using 5v regulated from Arduino?

author
Dominion-Network made it!(author)2011-12-23

Just wondering if you could do a more advanced version of this tutorial, say for example hooking up a keypad to the LCD display.

author
msanchez16 made it!(author)2011-08-06

Hello, please correct the first line
wich says: #include < Lcd.h>;
must says: #include WITHOUT THE SPACE :)

Thanks for your work

sorry for my english

author
GusPS made it!(author)2011-11-25

Done!
Thanks for telling....

author
Dominion-Network made it!(author)2011-11-24

I'm trying to follow this instructable, but you've forgotten to list the wire diagram, what pin connects to what wire on the LCD screen?

I would love to include this in a huge project I currently have planned, but without the pin layout I will have to guess at it.

author
GusPS made it!(author)2011-11-24

Hi.
It's on the 4th step, 2nd image.

author
Dominion-Network made it!(author)2011-11-25

I managed to get it to work, thanks for taking the time and effort to help me, when I do my big project I'll acknowledge you in the credits

author
GusPS made it!(author)2011-11-24

The image is from left to right, pin1 to last pin on the LCD. Look for the comment below about the type of LCD i've used.
Let me know if that help you...

author
robot797 made it!(author)2010-05-03

is it posible to use custem characters with this librarie
cus i cant get it to work....

author
GusPS made it!(author)2010-05-03

I haven't tried it.

I remember that the original lib can do that. Check www.slashdev.ca/arduino-lcd-library/

author
robot797 made it!(author)2010-05-03
i just found out how to do that
example code below

but now a new problem accurs
when i use the funktion "FUNCTION_2LINE"
the tekst becomes fage and vaded
how to fix it



/*
 * Define custom characters for creating an animation
 */
#include <Lcd.h>
#include <ShiftOut.h>


ShiftOut sOut(8,12,11,2);

Lcd lcd = Lcd(16, FUNCTION_4BIT, &sOut);

int potPin = 0;    // select the input pin for the potentiometer
int val = 100;       // variable to store the value coming from the sensor
                            //set to 100 for when there

uint8_t smile[] = {0x00,0x0A,0x00,0x11,0x0E,0x00,0x00,0x00};
uint8_t stripe[] = {0x13,0x06,0x0C,0x19,0x13,0x06,0x0C,0x19};
uint8_t stripe1[] = {0x19,0x13,0x06,0x0C,0x19,0x13,0x06,0x0C};
uint8_t stripe2[] = {0x0C,0x19,0x13,0x06,0x0C,0x19,0x13,0x06};
uint8_t stripe3[] = {0x06,0x0C,0x19,0x13,0x06,0x0C,0x19,0x13};

 


void setup()
{
 

  // set pin assignment
  lcd.set_ctrl_pins(CTRLPINS(1,2,3)); // RS->1, RW->2, E->3
  lcd.set_data_pins(_4PINS(4,5,6,7)); // D4->4, D5->5, D6->6, D7->7
 
  lcd.setup(); // setup arduino and initialize LCD
    lcd.clear();
  // define the custom characters
  lcd.define_char(0, smile);
  lcd.define_char(1, stripe);
    lcd.define_char(2, stripe1);
      lcd.define_char(3, stripe2);
        lcd.define_char(4, stripe3);


}

void loop()
{
  val = analogRead(potPin);
  lcd.home();
  lcd.print((char)1);   
  delay(val);
  lcd.clear();
    lcd.print((char)2); 
      delay(val);
  lcd.clear();
      lcd.print((char)3);
      delay(val);
  lcd.clear();
      lcd.print((char)4);
      delay(val);
  lcd.clear();

}

author
robot797 made it!(author)2010-04-18

i feel like 2 things a nerd and a idiot

how do i put tekst on the second line?

author
robot797 made it!(author)2010-04-19

never mind found it out

lcd.move_to(0, 2);

that is the code

lcd.move_to(0 collum , 2 row);

author
Lenny24 made it!(author)2010-02-13

Hi.

Im Living in Germany and I cant find an 74HC595.
Could i use your ShiftOut Library with a 4014?
Its' an 8-Bit Shift Register,too. I Think the Connection-Layout is a Little bit different.


author
robot797 made it!(author)2010-04-18

order a few in holland or on ebay

i found some that work

and it is like 50 km over the german border

author
no0x made it!(author)2010-02-13

I don't know but on Ebay you could find 10 pcs of 74HC595 for about 3 USD (2,2 €) fdp in

author
Lenny24 made it!(author)2010-02-13

Thanks.

author
NXTreme made it!(author)2010-04-02

Just a word of warning. Lots of times the electronic parts on eBay come from China or something and actually aren't what they advertise. Here's a link that sort of explains what I'm saying. Dangerous Fakes

Even the Chumby had problems.

author
GusPS made it!(author)2010-02-13

I'm not sure. Check the Input Voltage and the layout. Send me a link to datasheet. I don't know what is that component.

author
Lenny24 made it!(author)2010-02-14

The Pin Layout isnt the same.

http://www2.produktinfo.conrad.com/datenblaetter/150000-174999/172707-da-01-en-MC_14014_CD_4014_MC_14021_CD4021.pdf

author
GusPS made it!(author)2010-02-14

No, it won't work. You need a Serial In, Parallel out.
Alternatives are: MC54/74HC164, 74LS595 and CD4094.

Check the layout though.

author
Lenny24 made it!(author)2010-02-14

Thanks.

author
robot797 made it!(author)2010-04-12

i have 2 questions

1: what is the type of lcd you use
3:what happened to 2
4:could a second 74HC595 be added to drive led

so i mean a lcd and 4 or 7 leds?

author
GusPS made it!(author)2010-04-12

1) It's a 16x2, and has a label on back that reads "WINSTAR ELWH1602C-YGB-STK"
2) ?????
3) Yes, it will work. The main purpose was simplify the use of PINs beside a 595.

author
robot797 made it!(author)2010-04-12

so when i conect a second 595 on the way like this
http://www.arduino.cc/en/Tutorial/ShiftOut

then i could program some leds in it

that would be cool
i am building the starting chip for a turbine engine

but the data panel (it shows if evrything is working) would have 10 cables if you did not show me your way 

thank you

author
GusPS made it!(author)2010-04-13

Yes, it will work.
You'll have to adapt some code, but if u are a programmer will not have any problem.
You can see this video that is about connecting 2 595 in cascade and using the ShiftOut Library I made:
http://www.youtube.com/watch?v=dCAitY5223c

Here is the base code. It's not a reflect of the video, but it's a similar routine:

#include <ShiftOut.h>

ShiftOut Reg1(8,12,11,2);

void setup()
{
  delay(1000);
}

void loop()
{
  for (int j = 0; j < 16; j++)
  {
    Reg1.digitalWrt(j, HIGH);
    delay(100);
    Reg1.digitalWrt(j, LOW);
  }

  for (int j = 15; j >= 0; j--)
  {
    Reg1.digitalWrt(j, HIGH);
    delay(100);
    Reg1.digitalWrt(j, LOW);
  }
}

I hope it's clear for you. If not, just let me know and I'll help you.

Regards.

author
robot797 made it!(author)2010-04-18

i made a small script that uses an lcd- 1 button-4 led's-2 595's
and here is it's script

#include <Lcd.h>
#include <ShiftOut.h>
#include "Servo.h"

Servo gasservo;

ShiftOut sOut(8,12,11,2);
Lcd lcd = Lcd(16, FUNCTION_4BIT | FUNCTION_2LINE | FUNCTION_5x11, &sOut);

int switchPin = 4;              // switch is connected to pin 2

 

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
    lcd.set_ctrl_pins(CTRLPINS(1,2,3)); // RS->1, RW->2, E->3
  lcd.set_data_pins(_4PINS(4,5,6,7)); // D4->4, D5->5, D6->6, D7->7
 
  lcd.setup();
 
  lcd.clear();
  gasservo.attach(5);
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
     if (lightMode == 3) { //  if its waving,
                lightMode = 0;           // turn light off!
              }
   }
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // all-off
  lcd.clear();
  lcd.home();
  lcd.print("off");
    sOut.digitalWrt(9, LOW);
        sOut.digitalWrt(10, LOW);
            sOut.digitalWrt(11, LOW);
                sOut.digitalWrt(12, LOW);

  }

  if (lightMode == 1) { // all-on
  lcd.clear();
  lcd.home();
  lcd.print("on");
    sOut.digitalWrt(9, HIGH);
        sOut.digitalWrt(10, HIGH);
            sOut.digitalWrt(11, HIGH);
                sOut.digitalWrt(12, HIGH);
  }

  if (lightMode == 2) { // blinking
  lcd.clear();
    lcd.home();
  lcd.print("blink");
    sOut.digitalWrt(9, HIGH);
        sOut.digitalWrt(10, HIGH);
            sOut.digitalWrt(11, HIGH);
                sOut.digitalWrt(12, HIGH);
    delay(100);
    sOut.digitalWrt(9, LOW);
        sOut.digitalWrt(10, LOW);
            sOut.digitalWrt(11, LOW);
                sOut.digitalWrt(12, LOW);
    delay(100);
  }
  if (lightMode == 3)  { // "wave"
  lcd.clear();
      lcd.home();
  lcd.print("wave");
    sOut.digitalWrt(12, LOW);
    sOut.digitalWrt(9, HIGH);
    delay(50);
    sOut.digitalWrt(9, LOW);
    sOut.digitalWrt(10, HIGH);
    delay(50);
    sOut.digitalWrt(10, LOW);
    sOut.digitalWrt(11, HIGH);
    delay(50);
    sOut.digitalWrt(11, LOW);
    sOut.digitalWrt(12, HIGH);
    delay(50);
    sOut.digitalWrt(12, LOW);
  }   
}
 

author
robot797 made it!(author)2010-04-18

could you draw a normal diagram
i have an hard time decivering your diagrams now

author
GusPS made it!(author)2010-04-18

Have you tried opening the Fritzing file?

author
GusPS made it!(author)2010-04-18

Here is the link: http://www.megaupload.com/?d=NA9UXM4M

author
dzo made it!(author)2010-04-02

Tnx for this. It really helped me a lot.

author
no0x made it!(author)2010-03-02

Hi, it works very well, thanks !

I've some stupid question like the last time lol :
ShiftOut sOut(8, 12, 11, 1);
Why the PIN1 is define ?

And the library is very good but have you got some function for scrolling etc ?

author
GusPS made it!(author)2010-03-02

 "1" is the number of 74HC595 in cascade. You could use it to join varios in cascade an have more "out" PINs.

The library doesn't have scrolling or other features support. 

More info in: http://GusPS.blogspot.com

author
no0x made it!(author)2010-03-04

Thanks a lot !

So for a 8 bit connection we can do something like that ?

"
  ShiftOut sOut(2, 3, 4, 2);
  Lcd lcd = Lcd(20, FUNCTION_8BIT | FUNCTION_2LINE | FUNCTION_5x11, &sOut);
  lcd.set_ctrl_pins(CTRLPINS(1,2,3));  // RS->1, RW->2, E->3
  lcd.set_data_pins(_8PINS(8,9,10,11,4,5,6,7)); // D4->4, D5->5, D6->6, D7->7, D1->8, D2->9, D3->10, D->11
"

And to define new characters :

"
byte newChar[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };

void setup() {
lcd.set_ctrl_pins(CTRLPINS(1,2,3));   lcd.set_data_pins(_8PINS(8,9,10,11,4,5,6,7));
lcd.setup();
lcd.clear();
lcd.home();
lcd.define_char(0, newChar);
lcd.print(0);
}
"

?

author
GusPS made it!(author)2010-03-04

Well... I haven't test it with 8 bit, but It seems it would work.
Have you tired it?
Post your experiences so other can benefit too.

Thanks! 

author
no0x made it!(author)2010-03-04

So for the 8bit its works good with the connection on the following picture
(from http://www.arduino.cc/en/Tutorial/ShiftOut)

But I can't see any differences between 4bit and 8bit, we can create new characters in both mode and the refresh time seem to be the same.

To create new characters, the following code didn't work :
lcd.define_char(0, newChar);
lcd.print(0);
I've just "0" on the lcd display

We have to do this :
lcd.define_char(0, newChar);
lcd.print((char)0);

ShftOut_Schm2.gif
author
vsergione made it!(author)2010-02-28

Thanks a lot man for this tutorial. I've waisted about half a day trying to get Arduino to write to an LCD through the 74HC595 and without too much success. I guess I was too tired to do it right.

Nevertheless it is just great this library and, at least for myself, I will be eternally grateful :)

Keep up the good work!

author
GusPS made it!(author)2010-02-28

Thanks!. I'm glad it is usefull!

author
no0x made it!(author)2010-02-13

That's look nice, but what the value of the capacitor (in Farrad) ?
And what the use of this wire (On following image) ?
Sorry for the questions but I'm not an electronic king (and sorry for my english too)

F7BERRHG1ZGO2UY.MEDIUM.jpg
author
GusPS made it!(author)2010-02-13

Lenny was right.
The Cap is for decoupling. I've used 0.0000001 farads. (The common one labeled '104').
Red wire: 5V power to LCD.