Thursday, December 17, 2015

Control LCD display using shift registers (74HC595N)

This post is pretty much a continuation of my post using shift registers in Arduino projects .Please have a look at it first if you haven’t read it since I assume you are already familiar with how shift registers work and how to use them in a project. Therefore I'm not going to explain how the shift register chip works in this post. As you might already know I was trying to build a robot during my last summer vacation. Unfortunately I had to postpone it  due to lack of technical knowledge and lack of time to learn them .but I'm having a long summer break after another successful year at university and hopefully this time I think I’ll be able to finish what I started. This project is also a side project of that project. My goal was to build an obstacle avoiding robot using Arduino platform. I wanted to use a LCD display to display the distance to an obstacle in real time since it helps a lot in debugging when your Arduino is not connected to the computer and you don’t have access to the serial input readings. But one major problem with that was the number of I/O pins the display required in order to do that. An LCD display wanted at least 6 I/O pins. I had other components like a servo motor (1 data pin) ultrasound distance (2 data pins) measuring unit and my motor controlling unit (4 data pins)  and I couldn't use 0th pin of Arduino board since it deals with the communications of the board . So I ran out of pins. Then this 74HC595N shift register IC came for the rescue. The advantage of using shift register was connecting LCD to Arduino through shift register cost me only 3 pins. That was a huge advantage when it comes to working with micro controllers. However it wasn’t straight forward since the Arduino LiquidCrystal library is not compatible with using shift registers. So I had to find a way to go through it. Fortunately I was able to find this article about how to achieve this.  This is done by using a technology called Serial Peripheral Interface (SPI) which uses a synchronous serial data protocol to communicate with 74HC595 IC. In here Arduino micro controller acts as the master device and shift register acts as the slave device. LCD display is connected to the outputs of the Shift register IC. Then SPI.h and a modified version of conventional LiquidCrystal.h header files can be used to write programs to use LCD display.

The following schematic shows the connection of the components and the connection required are listed below. You should keep in mind to use only compatible LCD displays (Such as Hitachi HD44780) with the LiquidCrystal.h header file.


                      IC Pins (1-8)
                  LCD Display pins
1(Q1)
4(RS)
2
-
3
6 (E)
4-7
11-14
8
GND


LCD Display  Pin
Connection
1
GND
2
+5v
3
10K trimpot variable leg
5
GND
7-10
-
15
+5V through 220 ohms resistor
16
GND

IC pins 9-16
Arduino I/O pins
9
-
10
+5V
11(Sh_CP/SPI clock signal)
13
12(ST_CP/Latch Pin)
9
13
GND
14 (DS/Serial Data input)
11
15
-
16
+5V

Remember to connect the fixed legs of the trimpot (Trimmer/variable potentiometer) to +5V and GND.
The following image is a schematic of the circuit.
Image retrieved from http://42bots.com

After connecting all the components We can replace the old LiqudCrystal.h file with new header file which supports use of shift register. Please go through the following steps to setup the necessary files.
  1. Download this new version of the LiquidCrystal.h from here.
  2. Close Arduino IDE if you are currently using it.
  3. Find the directory where the library folder is located. It should be inside the directory where you install Arduino IDE.
  4. Replace the older version of header file with the newer one.( keep of copy of the older one as a backup )
  5. Open Arduino IDE and Go to Files à Examples à LiquidCrystal and select Hello World SPI.
. If you can see an Arduino sketch it means the new SPI supporting header file has been recognised by the IDE and you are ready to go. Just connect your Arduino board and upload the sketch to board and run it. If you can see the output “Hello world” on your LCD all good (you can replace this to anything you like in the sketch). If it’s not working please check your connections first. Most people (including me) sometimes make wrong connections that causes malfunction or not working at all. Sometimes you might see some random characters on screen . Press reset button few times to get of this and run your code. Now you can use your LCD with Arduino just using 3 pins and saving 3 more precious pins for other components.


Please go to following links since those web pages have lots of useful details about this.

If you need my sketch you can have it from here.










2 comments:

  1. Good work malli... Seems interesting... So you completed one of the building block of your robot project...

    Like to mention few things .....
    When using Arduino platform, it is easy to do the development without much effort. But as a undergraduate it is better to do some projects from scratch also. Because arduino hide all the complexities of embedded development from you. But when you go to industry you have to face those complexities.

    As a example I think when using the SPI communicationation in Arduino, you do not want to learn about slave clock phase, slave clock polarity and maximum possible SPI clock speeds...etc. These things are essential when developing real industrial products after you graduate. So this the best time to learn these basics of SPI communication.

    Some times you may already know these fundamentals. But if not, it is better to write some SPI driver for a device such as LCD display, accelerometer ..etc from your own to run on a micro controller like Atmege. SPI is very important protocol , which use in embedded systems. If we have several devices to connect to a micro controller, we can connect all of them to SPI bus of the micro controller and using seperate GPIO, we can select the relevant one. Some slave devices use QUAD SPI eg. Flash Chips which need high speed data communication. They use 4 pins to Rd/Wr data. so using two clocks we can send one byte.

    All the best !!!

    ReplyDelete
    Replies
    1. Thank you aiyya for your valuable advices. I always try to learn something new when doing these projects .These are mainly for just fun but I always learn new things in embedded systems when I do them. It's true that Arduino and other platforms hides a lot hardcore embedded stuff from you make your life a lot easier which is not very good when it comes to learning. As you might know I'm studying Electrical power engineering.So I don't think I'll be using any of these in industry but it's not an excuse to not to learn these technologies in detail. Definitely I'll learn about SPI in detail and try to do some projects. Thank you very much for spending your valuable time to read and comment. I really appreciate that.

      Delete