{"id":1099,"date":"2013-12-31T16:02:57","date_gmt":"2013-12-31T16:02:57","guid":{"rendered":"http:\/\/www.interactiveclassroom.net\/?p=1099"},"modified":"2014-01-01T00:58:54","modified_gmt":"2014-01-01T00:58:54","slug":"raspberry-pi-project-lcd-screen","status":"publish","type":"post","link":"https:\/\/www.interactiveclassroom.net\/?p=1099","title":{"rendered":"Raspberry Pi Project: LCD Screen Programmed in Python"},"content":{"rendered":"<p><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/WP_20131230_001.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-1103\" alt=\"LCD Screen\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/WP_20131230_001-300x168.jpg\" width=\"300\" height=\"168\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/WP_20131230_001-300x168.jpg 300w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/WP_20131230_001-1024x576.jpg 1024w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/WP_20131230_001.jpg 1632w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>For Christmas I received a number of &#8220;toys&#8221; to play with from my husband. One of which was a geeky box of goodies for my Raspberry Pi that includes a breadboard, wires and an array of other electronic components including LEDs, resistors and an LCD screen. I immediately decided that the LCD screen was to be my \u00a0first Raspberry Pi Project (as I really can&#8217;t take <em>any<\/em> credit for the <a title=\"Raspberry Pi Controlled Sofa\" href=\"http:\/\/www.interactiveclassroom.net\/?p=1060\" target=\"_blank\">PiSofa<\/a>)<\/p>\n<p>My first job was to \u00a0solder some connection pins onto the LCD screen so I could connect it to the breadboard and \u00a0Pi using jumper wires. I followed <a href=\"http:\/\/www.raspberrypi-spy.co.uk\/2012\/07\/16x2-lcd-module-control-using-python\/\" target=\"_blank\">this guide<\/a> online to get me started but modified some of the wiring slightly. In the guide they used the LCD in 4-bit mode but I changed my wiring to set it up in 8-bit mode instead. I also later discovered that I needed some control over the contrast pin so wired in a potentiometer to allow me to adjust the contrast. This is how my wiring looks:<\/p>\n<div id=\"attachment_1117\" style=\"width: 586px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/wiring1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1117\" class=\" wp-image-1117 \" alt=\"Wiring diagram for LCD screen and Raspberry Pi\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/wiring1-1024x762.png\" width=\"576\" height=\"428\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/wiring1-1024x762.png 1024w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/wiring1-300x223.png 300w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/wiring1.png 1114w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/a><p id=\"caption-attachment-1117\" class=\"wp-caption-text\">Wiring diagram for LCD screen and Raspberry Pi<\/p><\/div>\n<p>To initialise and set up the screen I used <a href=\"http:\/\/joshuagalloway.com\/lcd.html\" target=\"_blank\">another guide<\/a> to gain an understanding of some of the initialisation commands and created a <a href=\"https:\/\/github.com\/geekynicki\/LCDscreen\/blob\/master\/LCDbashScript.sh\" target=\"_blank\">bash script<\/a> to set it all up and check it was working; this revealed a few wiring problems, and was also the point at which I added the potentiometer as the contrast was making it difficult to read my test letter &#8220;A&#8221;.<\/p>\n<p>Now it was time for my main mission which was creating a Python program to control the LCD screen. First I worked on creating a program that simply mimicked the bash script, as this was quite untidy I set to work on reducing the amount of repeated code using functions to set pins using binary. In the original guide I had seen the data was set in hex but I prefer using binary as I understand the logic behind it more than using the hex data. Especially as I essentially just want to send True or False commands to the pins.<\/p>\n<p>By this time I had found an even <a href=\"http:\/\/www.8051projects.net\/lcd-interfacing\/commands.php\" target=\"_blank\">better guide<\/a>\u00a0 that told me what commands could be sent to the screen for initialisation and what they all meant. To initialise my LCD screen I am doing the following:<\/p>\n<ul>\n<li>Setting all pins to output<\/li>\n<li>Setting all pin values to false<\/li>\n<li>Sending the code 38 in hex\/56 decimal &#8211; Setting the LCD to 8-bit and 2 lines<\/li>\n<li>Sending the code 1 in hex\/decimal &#8211; Clearing the LCD display<\/li>\n<li>Sending the code 0C in hex\/12 decimal &#8211; \u00a0Sets the display to on and turns off the cursor<\/li>\n<li>Sending the code 6 in hex\/decimal &#8211; Sets the screen to entry mode<\/li>\n<\/ul>\n<p>Below is how my initialisation function looks. I should note that prior to this I imported the GPIO and time libraries and declared the locations of each of the pins, setting up the data pins as a list\/array.<\/p>\n<div id=\"attachment_1105\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/initialise.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1105\" class=\"wp-image-1105 \" alt=\"LCD Initialisation Sequence\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/initialise.png\" width=\"498\" height=\"275\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/initialise.png 996w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/initialise-300x165.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/a><p id=\"caption-attachment-1105\" class=\"wp-caption-text\">LCD initialisation sequence in Python<\/p><\/div>\n<p>As you can see, this initialisation function calls another function to convert the integer values to binary, which in turn accesses a function that sets these values on the LCD screen. These functions looks like this:<\/p>\n<div id=\"attachment_1108\" style=\"width: 611px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/set-pins-functions.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1108\" class=\" wp-image-1108  \" alt=\"Functions to convert to binary and set the control pins on the LCD screen\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/set-pins-functions.png\" width=\"601\" height=\"243\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/set-pins-functions.png 1202w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/set-pins-functions-300x121.png 300w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/set-pins-functions-1024x414.png 1024w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><p id=\"caption-attachment-1108\" class=\"wp-caption-text\">Functions to convert to binary and set the control pins on the LCD screen<\/p><\/div>\n<p>After the initialisation sequence the screen is ready to display some text, I made a function that reads in a text sequence along with the line number, converts each character to binary then sends this value to my existing &#8216;setPins&#8217; function to write the text to the screen. When actually writing to the screen it is important to set the RS pin to &#8216;data input mode&#8217; by setting it to &#8216;True&#8217;, otherwise it is in &#8216;instruction mode&#8217; and won&#8217;t display anything on the screen. This is how it looks:<\/p>\n<div id=\"attachment_1111\" style=\"width: 568px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/writeText-Function.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1111\" class=\" wp-image-1111 \" alt=\"writeText Function in Python\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/writeText-Function.png\" width=\"558\" height=\"178\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/writeText-Function.png 1116w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/writeText-Function-300x95.png 300w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/writeText-Function-1024x327.png 1024w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><p id=\"caption-attachment-1111\" class=\"wp-caption-text\">Function to write text to the LCD screen in Python<\/p><\/div>\n<p>Once the functions have been created all that is left is to test them out by actually calling them in the code with some test data:<\/p>\n<div id=\"attachment_1120\" style=\"width: 474px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/calling-functions.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1120\" class=\" wp-image-1120 \" alt=\"Calling the Functions\" src=\"http:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/calling-functions.png\" width=\"464\" height=\"94\" srcset=\"https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/calling-functions.png 663w, https:\/\/www.interactiveclassroom.net\/wp-content\/uploads\/2013\/12\/calling-functions-300x60.png 300w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><\/a><p id=\"caption-attachment-1120\" class=\"wp-caption-text\">Calling the functions with some text to test out<\/p><\/div>\n<p>I&#8217;m hoping to update this program with some additional functionality such as scrolling text and maybe transition effects. If you have any other ideas please comment below! For now though, I&#8217;m going to play with some of my <em>other<\/em> Christmas presents, which include\u00a0<a href=\"http:\/\/www.dexterindustries.com\/BrickPi\/\" target=\"_blank\">BrickPi<\/a>, <a href=\"http:\/\/www.makeymakey.com\/\" target=\"_blank\">MakeyMakey<\/a> and <a href=\"http:\/\/www.xbox.com\/en-GB\/xbox-one\/games\/dead-rising-3\" target=\"_blank\">Dead Rising 3<\/a> . . .<\/p>\n<p>The full Python program for the LCD screen can be downloaded from\u00a0<a href=\"https:\/\/github.com\/geekynicki\/LCDscreen\" target=\"_blank\">GitHub<\/a>. Happy coding and Happy New Year! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For Christmas I received a number of &#8220;toys&#8221; to play with from my husband. One of which was a geeky box of goodies for my Raspberry Pi that includes a&#8230; <a href=\"https:\/\/www.interactiveclassroom.net\/?p=1099\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,6,102],"tags":[],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-computing","category-ict","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1099"}],"version-history":[{"count":24,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1130,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=\/wp\/v2\/posts\/1099\/revisions\/1130"}],"wp:attachment":[{"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interactiveclassroom.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}