Page 1 of 1
3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Apr 27, 2015 12:36 pm
by BartOL
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
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Apr 27, 2015 1:26 pm
by scotty101
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()
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Tue Apr 28, 2015 6:52 pm
by BartOL
Hey Scotty,
Thanks for your quick reply , but unfortunately it doesn't work.
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
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Fri May 01, 2015 2:28 pm
by BMS Doug
BartOL wrote:Hey Scotty,
Thanks for your quick reply , but unfortunately it doesn't work.
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:

Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Fri May 01, 2015 2:39 pm
by BMS Doug
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()
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Jun 15, 2015 8:23 pm
by BartOL
Doug and scotty,
Thanks a lot! it finally works!
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()
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Wed Jun 17, 2015 1:09 pm
by BMS Doug
BartOL wrote:Doug and scotty,
Thanks a lot! it finally works!
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
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Thu Jun 18, 2015 5:17 pm
by BartOL
Ok seems legit, thanks for the info. Good to know for future projects.
Thanks again!
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Fri Jun 19, 2015 8:28 am
by BMS Doug
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!
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Fri Jan 26, 2018 5:49 pm
by madscientist007
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.
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Feb 19, 2018 2:49 pm
by madscientist007
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
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Feb 19, 2018 2:59 pm
by scotty101
Try changing
Code: Select all
import Adafruit_CharLCD as LCDimport
to
Also have you installed the Adafruit LCD library?
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Feb 19, 2018 4:22 pm
by madscientist007
yes i have installed the library, and the problem still persists.
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Feb 19, 2018 4:29 pm
by scotty101
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?
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Mon Feb 19, 2018 4:35 pm
by madscientist007
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
Re: 3x4 matrix combined with 16x2 LCD I2C Adafruit
Posted: Wed Feb 21, 2018 12:07 pm
by madscientist007
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