Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Guide to Setup Raspberry Pi with LCD Display using I2C Backpack

#1
Guide to Setup Raspberry Pi with LCD Display using I2C Backpack

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/11/guide-to-setup-raspberry-pi-with-lcd-display-using-i2c-backpack.jpg" width="550" height="550" title="" alt="" /></div><div><p>Last modified on November 8th, 2020.</p>
<p>Internet of things (IoT) is fascinating. I am predominantly a web applications person. But sensors, LED display, chips, soldering and the collage of software with these “things” is exciting.</p>
<p>The credit card sized Raspberry Pi computer gives all the opportunity to experiment and explore IoT. I wrote <a href="https://phppot.com/php/getting-started-with-iot-using-raspberry-pi-and-php/">getting started with IoT using Raspberry Pi and PHP</a> a while back. Now I thought of extending that and write about my hobby projects that I do with Raspberry Pi.</p>
<p>I have been using <a href="https://www.raspberrypi.org/">Raspberry Pi</a> to,</p>
<ul>
<li>Notify for new emails integrating GMail API.</li>
<li>Show the number of unread emails.</li>
<li>Monitor website uptime and notify if it’s up or down.</li>
<li>Show traffic user count to my website integrating Google Analytics.</li>
<li>Notify if there is a sale in my online shop.</li>
</ul>
<p>Raspberry Pi is my hobby and I thought of sharing with you about these tiny projects. This will be a multi article series. Let us start with how to connect a I2C LCD display with the Raspberry Pi.</p>
<p>Then I will write about all the above items one by one.</p>
<h2>I2C LCD display module</h2>
<p>Widely popular are 16×2(LCD1602) and 20×4 (LCD2004) LCD displays. The below image shows a 20×4 LCD display module.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-12186" src="https://phppot.com/wp-content/uploads/2020/11/20x4-lcd-display-module-550x550.jpg" alt="20x4 LCD Display Module" width="550" height="550" srcset="https://phppot.com/wp-content/uploads/2020/11/20x4-lcd-display-module-550x550.jpg 550w, https://phppot.com/wp-content/uploads/20...00x300.jpg 300w, https://phppot.com/wp-content/uploads/20...50x150.jpg 150w, https://phppot.com/wp-content/uploads/20...module.jpg 600w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p>Three main reasons to choose this type of LCD display for IoT projects using Raspberry Pi or Arduino.</p>
<ol>
<li>Economical to purchase. You can buy a 16×2 LCD module for just $1 USD.</li>
<li>It requires 5v to run and so we can hook it to Raspberry Pi and no dedicated power source required.</li>
<li>Consumes low current. You can keep it on 24×7 with backlight on without burning your purse.</li>
</ol>
<h3>Why I2C backpack?</h3>
<p>These LCD modules have parallel interface. That will not be convenient to connect multiple pins to the chip and use them in parallel mode.</p>
<p>I2C is a serial bus developed by Philips. So we can use I2C communication and just use 4 wires to communicate. To do this we need to use an I2C adapter and solder it to the display.</p>
<p>I2C uses two bidirectional lines, called SDA (Serial Data Line) and SCL (Serial Clock Line) with 5V standard power supply requirement a ground pin. So just 4 pins to deal with.</p>
<p>When you buy the LCD module, you can purchase LCD, I2C adapter separately and solder it. If soldering is not your thing, then it is better to buy the LCD module that comes with the I2C adapter backpack with it.</p>
<p><img loading="lazy" class="alignnone size-full wp-image-12189" src="https://phppot.com/wp-content/uploads/2020/11/2004lcd-with-i2c-adapter.jpg" alt="2004LCD with I2C Adapter" width="550" height="340" srcset="https://phppot.com/wp-content/uploads/2020/11/2004lcd-with-i2c-adapter.jpg 550w, https://phppot.com/wp-content/uploads/20...00x185.jpg 300w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p>The above image is backside of a 2004 LCD module. The <strong>black thing is the I2C adapter</strong>. You can see the four pins <strong>GND, VCC, SDA and SCL</strong>. That’s where the you will be connecting the Raspberry Pi.</p>
<p>The square blue thing with plus mark is used for adjusting the brightness of the display. The blue jumper is used to switch on or off the backlight.</p>
<p>Switching off the backlight may not be necessary. If you switch off, the characters will be barely visible. You can keep it on 24×7, it will not eat up power as it is a low power consumption device.</p>
<h2>Logic converter</h2>
<p>Raspberry Pi GPIO pins are natively of 3.3V. So we should not pull 5v from Raspberry Pi. The I2C LCD module works on 5V power and to make these compatible, we need to shift up the 3.3V GPIO to 5V. To do that, we can use a logic level converter.</p>
<p>Reference: <a href="https://www.raspberrypi.org/documentation/faqs/#pi-power-gpioout">https://www.raspberrypi.org/documentation/faqs/#pi-power-gpioout</a></p>
<p><img loading="lazy" class="alignnone size-full wp-image-12193" src="https://phppot.com/wp-content/uploads/2020/11/logic-level-converter.jpg" alt="Logic Level Converter" width="500" height="500" srcset="https://phppot.com/wp-content/uploads/2020/11/logic-level-converter.jpg 500w, https://phppot.com/wp-content/uploads/20...00x300.jpg 300w, https://phppot.com/wp-content/uploads/20...50x150.jpg 150w" sizes="(max-width: 500px) 100vw, 500px"></p>
<p>This is an important point and this is where a lot of beginners fail out. You may find some conflicting information in the Internet pages.</p>
<p>You might see RPIs connected directly to a 5V devices, but they may not be pulling power from RPI instead supplying externally. Only for data / instruction RPI might be used. So watch out, you might end up frying the LCD module or the RPI itself.</p>
<h2>Required hardware list</h2>
<ul>
<li>Raspberry Pi (I have used Zero W and you can use any model you have).</li>
<li>LCD display with I2C adapter backpack (I have used 20×4, you can use as per your choice).</li>
<li>3.3V to 5V bi-directional logic level converter</li>
<li>Breadboard</li>
<li>Jumper wires</li>
</ul>
<h3>Pre-requisite</h3>
<ul>
<li>Micro SD card with Raspbian OS</li>
<li>5.1V power supply (Official power adapter recommended)</li>
<li>Memory card reader</li>
</ul>
<h3>Why official power adapter?</h3>
<p>Why am I recommending the official power adapter! There is a reason to it. The cheap mobile adapters though guarantee a voltage, they do not provide a steady voltage. That may not be required in charging a cellphone device but not in the case of Raspberry Pi. That is the main reason, a USB keyboard or mouse attached does not get detected. They may not get sufficient power. Either go for an official power adapter or use the best branded one you know.</p>
<h2>Headless</h2>
<p>I have a headless setup. I am doing SSH from my MAC terminal and use VIM as editor. VNC viewer may occasionally help but doing the complete programming / debugging may not be comfortable. If you do not prefer SSH way, then you will need a monitor to plug-in to Raspberry Pi.</p>
<p><img loading="lazy" class="alignnone size-full wp-image-12191" src="https://phppot.com/wp-content/uploads/2020/11/headless-raspberry-pi.jpg" alt="Headless Raspberry Pi" width="550" height="342" srcset="https://phppot.com/wp-content/uploads/2020/11/headless-raspberry-pi.jpg 550w, https://phppot.com/wp-content/uploads/20...00x187.jpg 300w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Raspberry Pi LCD I2C Circuit diagram</h2>
<p>I have used a breadboard, logic level converter, 20×4 LCD display module with I2C backpack and Raspberry Pi Zero W in the circuit diagram.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-12192" src="https://phppot.com/wp-content/uploads/2020/11/raspberry-pi-lcd2004-i2c-circuit-550x384.jpg" alt="Raspberry Pi LCD2004 I2C Circuit" width="550" height="384" srcset="https://phppot.com/wp-content/uploads/2020/11/raspberry-pi-lcd2004-i2c-circuit-550x384.jpg 550w, https://phppot.com/wp-content/uploads/20...00x209.jpg 300w, https://phppot.com/wp-content/uploads/20...68x536.jpg 768w, https://phppot.com/wp-content/uploads/20...6x1071.jpg 1536w, https://phppot.com/wp-content/uploads/20...ircuit.jpg 1755w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p>3.3V GPIO of Raspberry Pi is converted using a logic level converter to 5V to be compatible for the LCD display.</p>
<h2>Raspberry Pi setup with LCD display module using I2C backpack</h2>
<p><a href="https://phppot.com/wp-content/uploads/2020/11/raspberry-pi-with-i2c-lcd-setup.jpg"><img loading="lazy" class="alignnone size-large wp-image-12194" src="https://phppot.com/wp-content/uploads/2020/11/raspberry-pi-with-i2c-lcd-setup-550x401.jpg" alt="Raspberry Pi with I2C LCD Setup" width="550" height="401" srcset="https://phppot.com/wp-content/uploads/2020/11/raspberry-pi-with-i2c-lcd-setup-550x401.jpg 550w, https://phppot.com/wp-content/uploads/20...00x219.jpg 300w, https://phppot.com/wp-content/uploads/20...68x560.jpg 768w, https://phppot.com/wp-content/uploads/20...-setup.jpg 1024w" sizes="(max-width: 550px) 100vw, 550px"></a></p>
<h2>Programming</h2>
<p>As you know my language of choice to build website is PHP. But for IoT with Raspberry Pi, let us use Python. Reason being availability of packages and that will save ton of effort. Low level interactions via serial or parallel interface is easier via Python.</p>
<h3>I2C</h3>
<p>We need the below two tools for working with I2C. So install it by running the following command in the RPI terminal.</p>
<pre class="prettyprint">sudo apt-get install python-smbus i2c-tools</pre>
<h3>Enable I2C</h3>
<pre class="prettyprint">sudo raspi-config</pre>
<p>The above command opens the Raspberry Pi configuration in the terminal. Under ‘Interfacing Options’, activate I2C.</p>
<pre class="prettyprint">sudo vi /etc/modules</pre>
<p>Add the following two lines at the end of the file and save it.</p>
<pre class="prettyprint">i2c-bcm2708
i2c-dev</pre>
<p>Then restart Raspberry Pi.</p>
<pre class="prettyprint">sudo reboot</pre>
<h3>Test I2C</h3>
<p>To check if the I2C is properly connected and detected.</p>
<pre class="prettyprint">sudo i2cdetect -y 1</pre>
<p>This will return a matrix with ’27’ highlighted, that means you I2C is detected and it is the address.</p>
<h3>Install PIP</h3>
<pre class="prettyprint">curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py<br>python3 get-pip.py</pre>
<h3>LCD Library</h3>
<p>We need a library to interact with the LCD module. There are many Python libraries available. I prefer <a href="https://rplcd.readthedocs.io/en/stable/">RPLCD</a>.</p>
<pre class="prettyprint">sudo pip install RPLCD</pre>
<p>This will install the RPLCD library.</p>
<h2>Raspberry Pi LCD Hello World Python program</h2>
<p class="code-heading">LCDHelloWorld.py</p>
<p>Following code imports the RPLCD library. Then initializes the LCD instance. Then print the “Hello World” string followed by new line. Then another two statements. Then a sleep for 5 seconds and switch off the LCD backlight. Finally, clear the LCD screen.</p>
<pre class="prettyprint lang-python"># Import LCD library
from RPLCD import i2c # Import sleep library
from time import sleep # constants to initialise the LCD
lcdmode = 'i2c'
cols = 20
rows = 4
charmap = 'A00'
i2c_expander = 'PCF8574' # Generally 27 is the address;Find yours using: i2cdetect -y 1 address = 0x27 port = 1 # 0 on an older Raspberry Pi # Initialise the LCD
lcd = i2c.CharLCD(i2c_expander, address, port=port, charmap=charmap, cols=cols, rows=rows) # Write a string on first line and move to next line
lcd.write_string('Hello world')
lcd.crlf()
lcd.write_string('IoT with Vincy')
lcd.crlf()
lcd.write_string('Phppot')
sleep(5)
# Switch off backlight
lcd.backlight_enabled = False # Clear the LCD screen
lcd.close(clear=True) </pre>
<p>My next article in this series will be on how to integrate the GMail API and notify for new mails.</p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/iot/guide-to-setup-raspberry-pi-with-lcd-display-using-i2c-backpack/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2020/11/...-backpack/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016