Support for I2C LCD 4x20 displays

Discussion related to "under the hood" OpenMV topics.
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 15, 2019 11:01 am

I have a lot of 4 row x 20 character I2C LCD displays kicking around. Are these supported by OpenMV? Personally I don't see why not, but I couldn't find any sample code. I'm considering using one to give status readouts from one of my OpenMV cameras.
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Mon Jul 15, 2019 12:08 pm

I think MicroPython just supports that in general. Since we are a MicroPython board you don't need to look for a driver just for our system:

https://forum.micropython.org/viewtopic.php?t=910
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 15, 2019 3:48 pm

Thanks Nyamekye
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Wed Jul 17, 2019 3:15 pm

I cloned his code samples and copied them to a directory on my Windows PC. I try running a sample program and it can't find the modules lcdapi and i2clcd. There are .py files by those names in the same directory as the test program. How do I get the IDE to find them?



"""Implements a HD44780 character LCD connected via PCF8574 on I2C."""

# The following import was needed on my OpenMV board
from lcd_api import LcdApi

from pyb import I2C, delay, millis
from pyb_i2c_lcd import I2cLcd
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Wed Jul 17, 2019 3:30 pm

You need to copy those files from the package you downloaded to the OpeMV Cam's disk. The IDE is just a text Editor. The camera is what actually compiles and runs the code.
Nyamekye,
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Wed Jul 17, 2019 4:35 pm

The camera can only see modules on its own disk. So, when you import stuff they need to be on the camera.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Thu Jul 18, 2019 2:17 pm

His install requires smbus. I am running the IDE on my Windows 10 machine. How do I install smbus?
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Thu Jul 18, 2019 2:33 pm

Hi, all the libraries needed should be in the source folder you downloaded. Copy them to the OpenMV Cam disk.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Thu Jul 18, 2019 3:05 pm

smbus isn't part of the source. Here is what is in there. The lcd1 and lcd2 pictures show what is in the source once downloaded. The openmv_disk is what I copied over to the camera. When I try to run 12c_lcd_test.py, I get the message ImportError: no module named 'smbus'. Also, if you look at his setup.py file, it has this in it.


from distutils.core import setup

setup(
name='python_lcd',
version='0.1.0',
author='Dave Hylands',
author_email='dhylands@gmail.com',
packages=['lcd'],
scripts=[],
url='https://github.com/dhylands/python_lcd/',
license='LICENSE',
description='Python library for HD44780 compatible character based LCDs.',
long_description=open('README.md').read(),
install_requires=[
'smbus'
],
)
Attachments
openmv_disk.PNG
lcd2.PNG
lcd1.PNG
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Thu Jul 18, 2019 3:26 pm

Most of the samples are for different boards (Adafruit, Grove, Node, ESP32 or PY boards).
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Thu Jul 18, 2019 4:59 pm

Please use the pyb examples. The OpenMV Cam has the same pyb module that the pyb board from MicroPython has. So, that should be what you use. Different MicroPython boards have different hardware abstraction layers. I don't know what the SMBUS module is.


This is the file you want to use: https://github.com/dhylands/python_lcd/ ... i2c_lcd.py
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 10:43 am

Actually, I think you mean pyb_i2c_lcd_test.py, which calls pyb_i2c_lcd.py.

I'm still not able to get anything to appear on the LCD display, so I'm doing more advanced troubleshooting. Hopefully something will come of this to serve as examples for other users.

Last night, I tested my LCD displays by connecting them to an Arduino UNO. I got two displays working on the UNO, one 16x2 with I2C expander board from Ywrobot and a 20x4 display with I2C expander board from Sunfounder. Both use address 27. See pictures.

I created a test program to run on the OpenMV board to scan the I2C bus and report what it finds. It is coming up with nothing, which might explain why I'm not seeing anything displayed on the LCD. Is there a problem with my test script or is it something with the OpenMV board?

Scott
Attachments
I2C problem.PNG
Sunfounder 20x4 LCD.jpg
ywrobot 16x2 LCD display.jpg
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Fri Jul 19, 2019 12:33 pm

Mmm, scan should work. Question, are there pull up resistors on the I2C lines?

Also, from the pic you swapped SCL and SDA. That will prevent it from working.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 2:39 pm

I have SDA to SDA and SCL to SCL.

I'm not sure if the boards have pullup resistors. If I recall, they should be 4.7 - 10 kohm?

Scott
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 3:10 pm

I added 10K pullups to SDA and SCL, but still no success. My scan program still doesn't find any I2C devices.

Code: Select all

# I2C Scanner Basic - By: Scott Murchison - Fri Jul 19 2019
# simple program to scan I2C bus for connected devices


from pyb import I2C

i2c = I2C(2, I2C.MASTER)

print('Scan i2c bus...')
devices = i2c.scan()

if len(devices) == 0:
  print("No i2c devices found !")
else:
  print('i2c devices found:',len(devices))

for device in devices:
  print("Decimal address: ",device)
  print("Hex address: ",hex(device))


User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Fri Jul 19, 2019 3:30 pm

Mmm, okay,

Can you use this module instead for I2C and try scan?

https://docs.micropython.org/en/latest/ ... e.I2C.html

The machine module bit bangs I2C with pins so it should always work. It's possible a MicroPython upstream change broke I2C.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 4:30 pm

I'm trying several programs using

from machine import I2C

but no success so far
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 4:45 pm

First test:

Code: Select all

from machine import I2C
i2c = I2C(freq=400000)
devices = i2c.scan()
print(devices)
I get TypeError: 'scl' argument required


Second test:

Code: Select all

from machine import I2C
i2c = I2C(2,4,5,freq=400000)
devices = i2c.scan()
print(devices)
I get TypeError: extra positional arguments given
User avatar
iabdalkader
Posts: 1085
Joined: Sun May 24, 2015 3:53 pm

Re: Support for I2C LCD 4x20 displays

Postby iabdalkader » Fri Jul 19, 2019 5:16 pm

Try:

Code: Select all

i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Fri Jul 19, 2019 10:32 pm

Sure. I'll get back to you when I get a chance to try it.
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 22, 2019 10:46 am

This works;

Code: Select all

# I2C Scanner Basic - By: Scott Murchison - Fri Jul 19 2019
# simple program to scan I2C bus for connected devices


from machine import I2C, Pin

i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))

print('Scanning i2c bus...')
devices = i2c.scan()

if len(devices) == 0:
  print("No i2c devices found !")
else:
  print('i2c devices found:',len(devices))

for device in devices:
  print("Decimal address: ",device)
  print("Hex address: ",hex(device))
Here is the output;

Scanning i2c bus...
i2c devices found: 1
Decimal address: 39
Hex address: 0x27
MicroPython v1.9.4-4570-g939334a54-dirty on 2019-07-18; OPENMV4 with STM32H743
Type "help()" for more information.
>>>
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Mon Jul 22, 2019 11:29 am

Okay, our pyb must have issues with the I2C driver on the H7.

Ibrahim, can you look into this?

In the meantime, if you want to try to change the calls from using pyb to machine in the example program you may be able to get it working. Us fixing the bug is going to take a while.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 22, 2019 2:08 pm

Thanks for responding. I was trying to change the calls this morning, but I ran into some other problems. In his pyb_i2c_lcd_test program, I changed

Code: Select all

from pyb import I2C
to

Code: Select all

from machine import I2C, Pin
and

Code: Select all

i2c = I2C(2, I2C.MASTER)
to

Code: Select all

i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
but in the called module pyb_i2c_lcd, it fails at this line because i2c has no attribute for send.

Code: Select all

self.i2c.send(0, self.i2c_addr)
For now, I haven't had enough experience with Python to get this all working. All I want to be able to do is send strings to a 20x4 lcd. It's frustrating, but I thank you anyways for trying to help.
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Mon Jul 22, 2019 2:21 pm

Can you just buy an LCD that supports serial? That's works fine with the H7 right now. You just TX the string you want to send at like 9600 baud to it and then it displays it.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 22, 2019 2:38 pm

I suppose I could, but I have a bunch of i2c LCDs laying around, so it seemed like a reasonable idea to use them. I know that it seems excessive, but I have plenty of Arduinos too. I could send my strings to the Arduino and let it handle the display. That would offload some of the work I need the OpenMV board to do, such as panning around while live viewing a scene looking for an April tag.

I think I read some posts about connecting the OpenMV to an Arduino. What do you suggest as the best method? serial, SPI, I2C
User avatar
kwagyeman
Posts: 3716
Joined: Sun May 24, 2015 2:10 pm

Re: Support for I2C LCD 4x20 displays

Postby kwagyeman » Mon Jul 22, 2019 4:08 pm

Use async serial, and then use an Arduino mega since it has multiple serial ports. Or, use software serial with your Arduino. You want to avoid using the main serial UART with the camera.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Jul 22, 2019 5:05 pm

I agree. All I would be doing is sending strings one way to the display.
User avatar
iabdalkader
Posts: 1085
Joined: Sun May 24, 2015 3:53 pm

Re: Support for I2C LCD 4x20 displays

Postby iabdalkader » Tue Jul 23, 2019 6:04 pm

If it works and you found the device what is the issue ?

Also machine i2c doesn't have a send function:

http://docs.openmv.io/library/machine.I2C.html

I see pyb I2C scan is not working, okay I'll check it. In the meantime I think you can use the machine module, btw pyb is obsolete and sooner or later it's going to be removed.
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Thu Nov 14, 2019 11:01 am

This is an old issue and I am now building my robot, so I want to check to see if OpenMV supports a 2 line or 4 line I2C LCD display. I have a solution whereby the OpenMV camera sends serial data to an Arduino and the Arduino takes care of the display, but a more elegant option would be to connect the LCD to the OpenMV directly. Just wondering if this was ever fixed.
User avatar
iabdalkader
Posts: 1085
Joined: Sun May 24, 2015 3:53 pm

Re: Support for I2C LCD 4x20 displays

Postby iabdalkader » Sat Nov 16, 2019 6:37 pm

ScottM wrote:
Thu Nov 14, 2019 11:01 am
This is an old issue and I am now building my robot, so I want to check to see if OpenMV supports a 2 line or 4 line I2C LCD display. I have a solution whereby the OpenMV camera sends serial data to an Arduino and the Arduino takes care of the display, but a more elegant option would be to connect the LCD to the OpenMV directly. Just wondering if this was ever fixed.
I don't have any LCDs to test with, but it should work. Give it a try and let me know.
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Support for I2C LCD 4x20 displays

Postby ScottM » Mon Dec 16, 2019 9:03 pm

I use these LCD displays. They are pretty cheap and available from many websites. https://www.ebay.ca/itm/IIC-I2C-TWI-200 ... 2364303996

Return to “Technical Discussion”

Who is online

Users browsing this forum: MSN [Bot] and 6 guests