Skip to content

Commit c81e944

Browse files
authored
Add files via upload
0 parents  commit c81e944

17 files changed

+592
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Metadata-Version: 2.1
2+
Name: DIYables_MicroPython_LCD_I2C
3+
Version: 0.1.1
4+
Summary: MicroPython Library for LCD I2C display, created by DIYables
5+
Home-page: https://diyables.io
6+
Author: DIYables
7+
Author-email: [email protected]
8+
Classifier: Programming Language :: Python :: Implementation :: MicroPython
9+
Classifier: License :: OSI Approved :: MIT License
10+
Classifier: Operating System :: OS Independent
11+
Requires-Python: >=3.6
12+
Description-Content-Type: text/markdown
13+
License-File: LICENSE
14+
15+
## MicroPython LCD I2C Library - DIYables_MicroPython_LCD_I2C
16+
This MicroPython library is designed for any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit... to work with the LCD I2C. It is created by DIYables to work with DIYables LCD I2C, but also work with other brand LCD I2C. Please consider purchasing [LCD I2C 16x2](https://www.amazon.com/dp/B0CM63P8VM) and [LCD I2C 20x4](https://www.amazon.com/dp/B0BXKKBZND) from DIYables to support our work.
17+
18+
19+
![LCD I2C](https://diyables.io/images/products/lcd-i2c.jpg)
20+
21+
22+
23+
Product Link
24+
----------------------------
25+
* [LCD I2C 16x2](https://diyables.io/products/lcd-i2c-16x2-blue-background)
26+
* [LCD I2C 20x4](https://diyables.io/products/lcd-20x4-display-i2c-interface)
27+
28+
29+
Features
30+
----------------------------
31+
* Works with any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit...
32+
33+
Available Examples
34+
----------------------------
35+
* main.py
36+
37+
38+
39+
Tutorials
40+
----------------------------
41+
* [Raspberry Pi Pico - LCD I2C 16x2](https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c)
42+
* [Raspberry Pi Pico - LCD I2C 20x4](https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-20x4)
43+
* [ESP32 MicroPython - LCD I2C 16x2](https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-lcd-i2c)
44+
* [ESP32 MicroPython - LCD I2C 20x4](https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-lcd-20x4)
45+
46+
47+
48+
References
49+
----------------------------
50+
* [MicroPython LCD I2C Library](https://newbiely.com/tutorials/micropython/micropython-lcd-i2c-library)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
LICENSE
2+
MANIFEST.in
3+
README.md
4+
setup.py
5+
DIYables_MicroPython_LCD_I2C/DIYables_MicroPython_LCD_I2C.py
6+
DIYables_MicroPython_LCD_I2C/__init__.py
7+
DIYables_MicroPython_LCD_I2C.egg-info/PKG-INFO
8+
DIYables_MicroPython_LCD_I2C.egg-info/SOURCES.txt
9+
DIYables_MicroPython_LCD_I2C.egg-info/dependency_links.txt
10+
DIYables_MicroPython_LCD_I2C.egg-info/top_level.txt
11+
examples/main.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DIYables_MicroPython_LCD_I2C
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
"""
2+
This MicroPython library is designed for any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit... to work with the LCD I2C. It is created by DIYables to work with DIYables LCD I2C, but also work with other brand LCD I2C. Please consider purchasing products from DIYables to support our work.
3+
4+
Product Link:
5+
- [LCD I2C 16x2](https://diyables.io/products/lcd-i2c-16x2-blue-background)
6+
- [LCD I2C 20x4](https://diyables.io/products/lcd-20x4-display-i2c-interface)
7+
8+
9+
Copyright (c) 2024, DIYables.io. All rights reserved.
10+
11+
Redistribution and use in source and binary forms, with or without
12+
modification, are permitted provided that the following conditions
13+
are met:
14+
15+
- Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
18+
- Redistributions in binary form must reproduce the above copyright
19+
notice, this list of conditions and the following disclaimer in the
20+
documentation and/or other materials provided with the distribution.
21+
22+
- Neither the name of the DIYables.io nor the names of its
23+
contributors may be used to endorse or promote products derived from
24+
this software without specific prior written permission.
25+
26+
THIS SOFTWARE IS PROVIDED BY DIYABLES.IO "AS IS" AND ANY EXPRESS OR
27+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29+
DISCLAIMED. IN NO EVENT SHALL DIYABLES.IO BE LIABLE FOR ANY DIRECT,
30+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36+
POSSIBILITY OF SUCH DAMAGE.
37+
"""
38+
39+
from machine import I2C, Pin
40+
from time import sleep_ms
41+
42+
class LCD_I2C:
43+
# LCD Commands
44+
LCD_CLR = 0x01
45+
LCD_HOME = 0x02
46+
47+
LCD_ENTRY_MODE = 0x04
48+
LCD_ENTRY_INC = 0x02
49+
LCD_ENTRY_SHIFT = 0x01
50+
51+
LCD_ON_CTRL = 0x08
52+
LCD_ON_DISPLAY = 0x04
53+
LCD_ON_CURSOR = 0x02
54+
LCD_ON_BLINK = 0x01
55+
56+
LCD_MOVE = 0x10
57+
LCD_MOVE_DISP = 0x08
58+
LCD_MOVE_RIGHT = 0x04
59+
60+
LCD_FUNCTION = 0x20
61+
LCD_FUNCTION_8BIT = 0x10
62+
LCD_FUNCTION_2LINES = 0x08
63+
LCD_FUNCTION_10DOTS = 0x04
64+
LCD_FUNCTION_RESET = 0x30
65+
66+
LCD_CGRAM = 0x40
67+
LCD_DDRAM = 0x80
68+
69+
LCD_RS_CMD = 0
70+
LCD_RS_DATA = 1
71+
72+
LCD_RW_WRITE = 0
73+
LCD_RW_READ = 1
74+
75+
def __init__(self, i2c, i2c_addr, num_lines, num_columns):
76+
self.i2c = i2c
77+
self.i2c_addr = i2c_addr
78+
self.num_lines = num_lines
79+
self.num_columns = num_columns
80+
self._current_state = 0x00
81+
self._backlight = 0x08
82+
83+
sleep_ms(20)
84+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
85+
sleep_ms(5)
86+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
87+
sleep_ms(1)
88+
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
89+
sleep_ms(1)
90+
self.hal_write_init_nibble(self.LCD_FUNCTION)
91+
sleep_ms(1)
92+
93+
self.init_lcd()
94+
95+
def hal_write_init_nibble(self, nibble):
96+
byte = ((nibble >> 4) & 0x0F) << 4
97+
self.i2c.writeto(self.i2c_addr, bytes([byte | self.LCD_RS_CMD | self._backlight]))
98+
self.hal_pulse_enable(byte)
99+
100+
def hal_backlight_on(self):
101+
self._backlight = 0x08
102+
self.i2c.writeto(self.i2c_addr, bytes([self._current_state | self._backlight]))
103+
104+
def hal_backlight_off(self):
105+
self._backlight = 0x00
106+
self.i2c.writeto(self.i2c_addr, bytes([self._current_state | self._backlight]))
107+
108+
def hal_write_command(self, cmd):
109+
self._write_byte((cmd & 0xF0) | self.LCD_RS_CMD)
110+
self._write_byte(((cmd << 4) & 0xF0) | self.LCD_RS_CMD)
111+
if cmd <= 3:
112+
sleep_ms(5)
113+
114+
def hal_write_data(self, data):
115+
self._write_byte((data & 0xF0) | self.LCD_RS_DATA)
116+
self._write_byte(((data << 4) & 0xF0) | self.LCD_RS_DATA)
117+
118+
def _write_byte(self, byte):
119+
self.i2c.writeto(self.i2c_addr, bytes([byte | self._backlight]))
120+
self.hal_pulse_enable(byte)
121+
122+
def hal_pulse_enable(self, byte):
123+
self.i2c.writeto(self.i2c_addr, bytes([byte | 0x04 | self._backlight]))
124+
self.i2c.writeto(self.i2c_addr, bytes([byte & ~0x04 | self._backlight]))
125+
126+
def init_lcd(self):
127+
self.display_on()
128+
self.clear()
129+
self.set_entry_mode()
130+
131+
def clear(self):
132+
self.hal_write_command(self.LCD_CLR)
133+
self.hal_sleep(3)
134+
135+
def show_cursor(self):
136+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR)
137+
138+
def hide_cursor(self):
139+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
140+
141+
def blink_cursor_on(self):
142+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR | self.LCD_ON_BLINK)
143+
144+
def blink_cursor_off(self):
145+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | self.LCD_ON_CURSOR)
146+
147+
def display_on(self):
148+
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
149+
150+
def display_off(self):
151+
self.hal_write_command(self.LCD_ON_CTRL)
152+
153+
def set_entry_mode(self, inc=True, shift=False):
154+
entry_mode = self.LCD_ENTRY_MODE
155+
if inc:
156+
entry_mode |= self.LCD_ENTRY_INC
157+
if shift:
158+
entry_mode |= self.LCD_ENTRY_SHIFT
159+
self.hal_write_command(entry_mode)
160+
161+
def backlight_off(self):
162+
self.hal_backlight_off()
163+
164+
def backlight_on(self):
165+
self.hal_backlight_on()
166+
167+
def home(self):
168+
self.hal_write_command(self.LCD_HOME)
169+
self.hal_sleep(3)
170+
171+
def set_cursor(self, col, line):
172+
addr = col & 0x3F
173+
if line & 1:
174+
addr += 0x40
175+
if line & 2:
176+
addr += 0x14
177+
self.hal_write_command(self.LCD_DDRAM | addr)
178+
179+
def print(self, string):
180+
for char in string:
181+
self.hal_write_data(ord(char))
182+
183+
def custom_char(self, location, charmap):
184+
self.hal_write_command(self.LCD_CGRAM | ((location & 7) << 3))
185+
self.hal_sleep(5)
186+
for i in range(8):
187+
self.hal_write_data(charmap[i])
188+
self.home()
189+
190+
def print_custom_char(self, location):
191+
self.hal_write_data(location)
192+
193+
def hal_sleep(self, time_ms):
194+
sleep_ms(time_ms)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# DIYables_MicroPython_LCD_I2C/__init__.py
2+
from .DIYables_MicroPython_LCD_I2C import LCD_I2C

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright (c) 2024, DIYables.io. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions
5+
are met:
6+
7+
- Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
- Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
- Neither the name of the DIYables.io nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY DIYABLES.IO "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL DIYABLES.IO BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include LICENSE
2+
include README.md
3+
graft examples
4+
global-exclude __pycache__
5+
global-exclude *.py[co]

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## MicroPython LCD I2C Library - DIYables_MicroPython_LCD_I2C
2+
This MicroPython library is designed for any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit... to work with the LCD I2C. It is created by DIYables to work with DIYables LCD I2C, but also work with other brand LCD I2C. Please consider purchasing [LCD I2C 16x2](https://www.amazon.com/dp/B0CM63P8VM) and [LCD I2C 20x4](https://www.amazon.com/dp/B0BXKKBZND) from DIYables to support our work.
3+
4+
5+
![LCD I2C](https://diyables.io/images/products/lcd-i2c.jpg)
6+
7+
8+
9+
Product Link
10+
----------------------------
11+
* [LCD I2C 16x2](https://diyables.io/products/lcd-i2c-16x2-blue-background)
12+
* [LCD I2C 20x4](https://diyables.io/products/lcd-20x4-display-i2c-interface)
13+
14+
15+
Features
16+
----------------------------
17+
* Works with any hardware plaform that supports MicroPython such as Raspberry Pi Pico, ESP32, Micro:bit...
18+
19+
Available Examples
20+
----------------------------
21+
* main.py
22+
23+
24+
25+
Tutorials
26+
----------------------------
27+
* [Raspberry Pi Pico - LCD I2C 16x2](https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-i2c)
28+
* [Raspberry Pi Pico - LCD I2C 20x4](https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-lcd-20x4)
29+
* [ESP32 MicroPython - LCD I2C 16x2](https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-lcd-i2c)
30+
* [ESP32 MicroPython - LCD I2C 20x4](https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-lcd-20x4)
31+
32+
33+
34+
References
35+
----------------------------
36+
* [MicroPython LCD I2C Library](https://newbiely.com/tutorials/micropython/micropython-lcd-i2c-library)

0 commit comments

Comments
 (0)