BartOL
Posts: 9
Joined: Mon Apr 27, 2015 10:22 am

3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Apr 27, 2015 12:36 pm

Dear all,

I'm working on a project which uses an 3x4 membrane pad and a 16x2 Adafruit I2C LCD display.
the goal is to display the pressed numbers on the LCD display but i can't get it working and don't know how to proceed.

I get them working fine in 2 seperate python scripts but when i try to merge them it's going wrong.
For the LCD display i'm using the Adafruit libraries and i'm supspecting them as the cause of it.
I'm just a beginner so this is just a gamble and don't know how to proceed from this point.

Can some one help me out here? how do i get these 2 python scripts merged to 1 working script?
I already googled my ass off and searched in this forum but i can't find anything suitable.

used materials:
3x4 membrane pad
16x2 LCD i2C Adafruit
Raspberry Pi B+

see seperate scripts below:

Code: Select all

# 3x4 Matrix membrane keypad

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

MATRIX= [[1,2,3],
         [4,5,6],
         [7,8,9],
         ['*',0,'#']
         ]
ROW=[7,11,13,15]
COL=[12,16,18]

for j in range(3):
    GPIO.setup(COL[j],GPIO.OUT)
    GPIO.output(COL[j],1)

for i in range(4):
    GPIO.setup(ROW[i],GPIO.IN,pull_up_down = GPIO.PUD_UP)

try:
    while(True):
                for j in range(3):
                    GPIO.output(COL[j],0)
                    for i in range(4):
                        if GPIO.input(ROW[i]) == 0:
                            print MATRIX [i][j]
                            time.sleep(0.2)
                            while (GPIO.input(ROW[i])==0):
                                pass

                    GPIO.output(COL[j],1)
except KeyboardInterrupt:
    GPIO.cleanup()

Code: Select all

#LCD I2C test

Import Adafruit_CharLCD as LCD
import time

lcd=LCD.Adafruit_CharLCDplate()

lcd.clear()
time.sleep(2)
lcd.message(' Hello ')
time.sleep(5)
lcd.clear()


thanks,

regards Bart

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Apr 27, 2015 1:26 pm

How about this? It should show the last key pressed

Code: Select all

# 3x4 Matrix membrane keypad

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCDimport 
import time

GPIO.setmode(GPIO.BOARD)

# Initialise and clear LCD
lcd=LCD.Adafruit_CharLCDplate()
lcd.clear()
time.sleep(0.5)

MATRIX= [[1,2,3],
         [4,5,6],
         [7,8,9],
         ['*',0,'#']
         ]
ROW=[7,11,13,15]
COL=[12,16,18]

for j in range(3):
    GPIO.setup(COL[j],GPIO.OUT)
    GPIO.output(COL[j],1)

for i in range(4):
    GPIO.setup(ROW[i],GPIO.IN,pull_up_down = GPIO.PUD_UP)

try:
    while(True):
                for j in range(3):
                    GPIO.output(COL[j],0)
                    for i in range(4):
                        if GPIO.input(ROW[i]) == 0:
                            key_pressed = MATRIX[i][j]
                            lcd.clear()
                            lcd.message("{} was pressed".format(key_pressed))
                            time.sleep(0.2)
                            while (GPIO.input(ROW[i])==0):
                                pass

                    GPIO.output(COL[j],1)
except KeyboardInterrupt:
    GPIO.cleanup()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

BartOL
Posts: 9
Joined: Mon Apr 27, 2015 10:22 am

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Tue Apr 28, 2015 6:52 pm

Hey Scotty,

Thanks for your quick reply , but unfortunately it doesn't work. :cry:
When i execute the script i get the error:

File "/home/pi/Desktop/keypad-LCD.py", line 7, in <module>
GPIO.setmode(GPIO.BOARD)
ValueError: A different mode has already been set!

Could it be that the GPIO.setmode(GPIO.BOARD) command conflicts with the Adafruit LCD library for some reason? Perhaps importing the Adafruit library already reserves the GPIO pins?

Thanks already!

Bart

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Fri May 01, 2015 2:28 pm

BartOL wrote:Hey Scotty,

Thanks for your quick reply , but unfortunately it doesn't work. :cry:
When i execute the script i get the error:

File "/home/pi/Desktop/keypad-LCD.py", line 7, in <module>
GPIO.setmode(GPIO.BOARD)
ValueError: A different mode has already been set!

Could it be that the GPIO.setmode(GPIO.BOARD) command conflicts with the Adafruit LCD library for some reason? Perhaps importing the Adafruit library already reserves the GPIO pins?

Thanks already!

Bart
Exactly right, Adafruit_CharLCD.py sets the GPIO mode to BCM so you will need to use BCM numbering in your other python program.

Thank Gordon for the BCM pinout below:
Image
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Fri May 01, 2015 2:39 pm

scotty101 wrote:How about this? It should show the last key pressed
modified to BCM numbering

Code: Select all

# 3x4 Matrix membrane keypad

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCDimport 
import time

GPIO.setmode(GPIO.BCM)

# Initialise and clear LCD
lcd=LCD.Adafruit_CharLCDplate()
lcd.clear()
time.sleep(0.5)

MATRIX= [[1,2,3],
         [4,5,6],
         [7,8,9],
         ['*',0,'#']
         ]

ROW=[4,17,21,22] # if Pi is Rev1
# ROW=[4,17,27,22] # if Pi is Rev2
COL=[18,23,24]

for j in range(3):
    GPIO.setup(COL[j],GPIO.OUT)
    GPIO.output(COL[j],1)

for i in range(4):
    GPIO.setup(ROW[i],GPIO.IN,pull_up_down = GPIO.PUD_UP)

try:
    while(True):
                for j in range(3):
                    GPIO.output(COL[j],0)
                    for i in range(4):
                        if GPIO.input(ROW[i]) == 0:
                            key_pressed = MATRIX[i][j]
                            lcd.clear()
                            lcd.message("{} was pressed".format(key_pressed))
                            time.sleep(0.2)
                            while (GPIO.input(ROW[i])==0):
                                pass

                    GPIO.output(COL[j],1)
except KeyboardInterrupt:
    GPIO.cleanup()
Doug.
Building Management Systems Engineer.

BartOL
Posts: 9
Joined: Mon Apr 27, 2015 10:22 am

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Jun 15, 2015 8:23 pm

Doug and scotty,

Thanks a lot! it finally works! :D :D :D
Why do they use the BCM pinout instead the "normal"pin out?
Is BCM preferred over the "normal" pin out for certain reason?

regards,

Bart



BMS Doug wrote:
scotty101 wrote:How about this? It should show the last key pressed
modified to BCM numbering

Code: Select all

# 3x4 Matrix membrane keypad

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCDimport 
import time

GPIO.setmode(GPIO.BCM)

# Initialise and clear LCD
lcd=LCD.Adafruit_CharLCDplate()
lcd.clear()
time.sleep(0.5)

MATRIX= [[1,2,3],
         [4,5,6],
         [7,8,9],
         ['*',0,'#']
         ]

ROW=[4,17,21,22] # if Pi is Rev1
# ROW=[4,17,27,22] # if Pi is Rev2
COL=[18,23,24]

for j in range(3):
    GPIO.setup(COL[j],GPIO.OUT)
    GPIO.output(COL[j],1)

for i in range(4):
    GPIO.setup(ROW[i],GPIO.IN,pull_up_down = GPIO.PUD_UP)

try:
    while(True):
                for j in range(3):
                    GPIO.output(COL[j],0)
                    for i in range(4):
                        if GPIO.input(ROW[i]) == 0:
                            key_pressed = MATRIX[i][j]
                            lcd.clear()
                            lcd.message("{} was pressed".format(key_pressed))
                            time.sleep(0.2)
                            while (GPIO.input(ROW[i])==0):
                                pass

                    GPIO.output(COL[j],1)
except KeyboardInterrupt:
    GPIO.cleanup()

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Wed Jun 17, 2015 1:09 pm

BartOL wrote:Doug and scotty,

Thanks a lot! it finally works! :D :D :D
Why do they use the BCM pinout instead the "normal"pin out?
Is BCM preferred over the "normal" pin out for certain reason?

regards,

Bart
I'm not sure why Adafruit use BCM numbering, the LCD python program was originally written in 2012 based on the work of Lance R Vick (who chose BCM numbering).
Alex Eames wrote:In RPi.GPIO you can use either pin numbers (BOARD) or the Broadcom GPIO numbers (BCM), but you can only use one system in each program. I habitually use the GPIO numbers, but neither way is wrong. Both have advantages and disadvantages.
If you use pin numbers, you don’t have to bother about revision checking, as RPi.GPIO takes care of that for you. You still need to be aware of which pins you can and can’t use though, since some are power and GND.

If you use GPIO numbers, your scripts will make better sense if you use a Gertboard, which also uses GPIO numbering. If you want to use the P5 header for GPIO28-31, you have to use GPIO numbering. If you want to control the LED on a Pi camera board (GPIO5) you also have to use GPIO numbering.
The important thing is to pick the one that makes sense to you and use it. It’s also important to be aware of the other system in case you ever need to work on someone elses code.
http://raspi.tv/2013/rpi-gpio-basics-4- ... and-inputs
Doug.
Building Management Systems Engineer.

BartOL
Posts: 9
Joined: Mon Apr 27, 2015 10:22 am

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Thu Jun 18, 2015 5:17 pm

Ok seems legit, thanks for the info. Good to know for future projects.
Thanks again!

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Fri Jun 19, 2015 8:28 am

BartOL wrote:Ok seems legit, thanks for the info. Good to know for future projects.
Thanks again!
You're welcome.

Congratulations on getting your project working!
Doug.
Building Management Systems Engineer.

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Fri Jan 26, 2018 5:49 pm

Hello i am new to the programming and raspberry pi world i am trying to use these two components to create a password system. an example of a code that asks for a password, then you have to enter the password through the 3x4 matrix keypad.

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Feb 19, 2018 2:49 pm

could some on e explain this error

File "/home/pi/LCDKYPD.py", line 10, in <module>
lcd=LCD.Adafruit_CharLCDplate()
NameError: name 'LCD' is not defined

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Feb 19, 2018 2:59 pm

Try changing

Code: Select all

import Adafruit_CharLCD as LCDimport
to

Code: Select all

import Adafruit_CharLCD as LCD
Also have you installed the Adafruit LCD library?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Feb 19, 2018 4:22 pm

yes i have installed the library, and the problem still persists.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Feb 19, 2018 4:29 pm

Then share the code you are running and post the exact error message that you are getting.

Do you perhaps have something else inside your python script called LCD?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Mon Feb 19, 2018 4:35 pm

I am running the exact same code and the error is,

Traceback (most recent call last):
File "/home/pi/LCDKYPD.py", line 10, in <module>
lcd=LCD.Adafruit_CharLCDplate()
NameError: name 'LCD' is not defined

madscientist007
Posts: 12
Joined: Fri Jan 26, 2018 5:45 pm

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit

Wed Feb 21, 2018 12:07 pm

I managed to fix the problem all i did was change my declarations to,

Code: Select all

import RPi.GPIO as GPIO
import Adafruit_CharLCD as LCDimport 
import time
from Adafruit_CharLCD import Adafruit_CharLCD
      
GPIO.setmode(GPIO.BCM)

# Initialise and clear LCD

lcd = Adafruit_CharLCD

Return to “Other projects”