LCD Screen + Raspberry Pi Model 3 V1.2
I'll tell you how to connect a LCD screen + Raspberry pi
By the way, this is my first project, and I hope you will enjoy this journey with me.
Firstly I'm using this Raspberry Pi model 3 v1.2
It was quite difficult to find how to connect it but here it is, but first the lcd
I'm using the new model without the extra module 12c, because this one has that one already integrated
Having these components, lets to see how connect them:
Even if this is not exacly the lcd which I'm working the connection is the same:
GND to GND
SCL to SCL
SDA to SCA
And 5v to 5v
Follow me in this deep research how I want to know more about how is the communication between them.
And why I2C.
I2C is a protocol easy invented by Philips to communicate devices with a speed between 100Kbits per second to 3.4MHz.
How does it works?
What is the meaning of SCL and SDA:
SCL: System Clock
SDA: System Data
SDA and SCL is a language between two components:
Only the masters devices can to start the communication. And both signal should be in level up. As soon as a master wants to talk, that master send a low signal of SDA while the signal SCL is up.
import drivers
from time import sleep
# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = drivers.Lcd()
# Main body of code
try:
print("Press CTRL + C to stop this script!")
def long_string(display, text='', num_line=1, num_cols=16):
"""
Parameters: (driver, string to print, number of line to print, number of columns of your display)
Return: This function send to display your scrolling string.
"""
if len(text) > num_cols:
display.lcd_display_string(text[:num_cols], num_line)
sleep(1)
for i in range(len(text) - num_cols + 1):
text_to_print = text[i:i+num_cols]
display.lcd_display_string(text_to_print, num_line)
sleep(0.2)
sleep(1)
else:
display.lcd_display_string(text, num_line)
# Example of short string
long_string(display, "Hello World!", 1)
sleep(1)
# Example of long string
long_string(display, "Hello again. This is a long text.", 2)
display.lcd_clear()
sleep(1)
while True:
# An example of infinite scrolling text
long_string(display, "Hello friend! This is a long text!", 2)
except KeyboardInterrupt:
# If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()
Meaning of each line of code:
The meaning of the function:
Definition of the long_string function:
This function is designed to display text strings on an LCD display.
Parameters:
display: Instance of the LCD driver (not provided in this fragment).
text: The text string to display.
num_line: The line number of the LCD screen where the text should be displayed (usually 1 or 2).
num_cols: The number of columns the LCD has, usually 16 or 20.
Behaviour:
Long text:
If the length of the text is greater than the number of columns (num_cols), the text scrolls to display the whole text.
It displays the first part of the text, waits one second, and then scrolls the text to the left, updating the display every 0.2 seconds, until all the text has scrolled across the screen.
Short text:
If the text fits on the screen (i.e. its length is less than or equal to the number of columns), it is displayed all at once without scrolling.
Conditional if len(text) > num_cols::
This block checks if the length of the text (text) is greater than the number of columns (num_cols) that the LCD screen has.
If the length of the text is greater than the number of columns, it means that the text does not fit on a single line of the screen and must be scrolled.
Long Text: Horizontal Scrolling:
First Portion (text[:num_cols]):
Displays on the screen the first portion of the text that fits in the available columns.
Wait 1 second (sleep(1)) for the user to read this first portion.
display.lcd_display_string(...):
This is a call to a method called lcd_display_string belonging to the display object, which is assumed to be an instance of a class that controls the LCD display. This method is the one that actually sends the text to the screen to be displayed.
text[:num_cols]:
This part extracts a portion of the text that has the maximum size of the available columns on the screen.
text[:num_cols] is a Python slicing operation, which selects a substring from the beginning of the text to the num_cols index (excluding the character in num_cols). This means that the first part of the text is taken until the number of columns on the screen is filled.
For example, if text = "Hello, World!" and num_cols = 5, then text[:num_cols] would be "Hello".
num_line:
This is the second argument passed to the lcd_display_string method, indicating on which line of the LCD screen the text should be displayed.
LCD displays typically have 2 or 4 lines, and num_line specifies on which line the text string will be displayed.
The line for i in range(len(text) - num_cols + 1): is part of a for loop in Python, and its purpose is to iterate through the indices that allow long text to be scrolled on an LCD screen, so that all the text can be displayed sequentially.
len(text):
Gets the total length of the text string (text), i.e. how many characters it has.
num_cols:
Represents the number of columns on the LCD screen, or the number of characters that fit on one line of the screen.
len(text) - num_cols + 1:
This calculation determines the number of positions through which the text must scroll for all parts of the text to be displayed on the screen.
Explanation:
len(text) is the total length of the text.
num_cols is the number of characters that can be displayed on the screen simultaneously.
len(text) - num_cols calculates how many additional characters there are beyond those that fit on the screen.
The + 1 is added to include the case where the last portion of the text is exactly num_cols characters.
For example, if the text is 20 characters long (len(text) = 20) and the screen has 16 columns (num_cols = 16), the loop will need to scroll through 20 - 16 + 1 = 5 positions.
range(...):
The range(...) function generates a sequence of numbers from 0 to len(text) - num_cols, which are the indices from which the text scrolling will start.
for i in range(...):
This for loop iterates through each index i in the generated range, allowing, on each iteration, a substring of the text to be selected for display on the screen.
The line text_to_print = text[i:i+num_cols] selects a substring of the original text for display on an LCD screen or other medium, depending on the implementation.
text[i:i+num_cols]:
This is a Python slicing operation that extracts a portion of the text (text).
i is the starting index, determined by the current value of variable i within a loop.
i+num_cols is the ending index (non-inclusive), which means that num_cols characters are selected starting from index i.
The result is a substring starting at index i and extending up to (but not including) index i+num_cols.
text_to_print:
This variable stores the extracted substring, which can then be used, for example, to display it on an LCD screen.
Example:
Suppose that:
text = "Hello, World!"
i = 2 (the current value of i in the loop).
num_cols = 5
Then text[i:i+num_cols] would evaluate to text[2:7], which produces the substring "llo, ". This is because:
The character in position 2 is 'l'.
The character at position 7 is ' '.
The substring includes index 2 through index 6.
Comentarios
Publicar un comentario