To control 4×4 Matrix Keypad Module using Raspberry Pi Pico
Required Components
4×4 Matrix Keypad_1 no
Raspberry Pi Pico board_1 no
Data Cable_1 no
Connecting Wires_8 no
Circuit
Steps
Make sure the components are working properly.
4×4 Matrix keypad 1 to 8 pins connected to GP1 to GP8 of Raspberry Pi Pico Board respectively.
Check the Python program.
Check the Electrical Circuit.
Run the Python program.
Python Program
from machine import Pin
import utime
# define PINs according to cabling
# following array matches 1,2,3,4 PINs from 4x4 Keypad Matrix
col_list=[1,2,3,4]
# following array matches 5,6,7,8 PINs from 4x4 Keypad Matrix
row_list=[5,6,7,8]
# set row pins to output and change array elements from
# int to Pin objects, all set to high
for x in range(0,4):
row_list[x]=Pin(row_list[x], Pin.OUT)
row_list[x].value(1)
# set columns pins to input and change array elements
# from int to Pin objects. We'll read user input here
for x in range(0,4):
col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
# Create a map between keypad buttons and chars
key_map=[["D","#","0","*"],\
["C","9","8","7"],\
["B","6","5","4"],\
["A","3","2","1"]]
def Keypad4x4Read(cols,rows):
for r in rows:
r.value(0)
result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]
if min(result)==0:
key=key_map[int(rows.index(r))][int(result.index(0))]
r.value(1) # manages key keept pressed
return(key)
r.value(1)
# Start the main loop
print("--- Ready to get user inputs ---")
while True:
key=Keypad4x4Read(col_list, row_list)
if key != None:
print("You pressed: "+key)
utime.sleep(0.3) # gives user enoght time to release without having double inputs
To control 4×3 Matrix Keypad Module using Raspberry Pi Pico
Required Components
4×3 Matrix Keypad_1 no
Raspberry Pi Pico board_1 no
Data Cable_1 no
Connecting Wires_8 no
Circuit
Steps
Make sure the components are working properly.
4×3 Matrix keypad 1 to 7 pins connected to GP1 to GP7 of Raspberry Pi Pico Board respectively.
Check the Python program.
Check the Electrical Circuit.
Run the Python program.
Python Program
from machine import Pin
import utime
# define PINs according to cabling
# following array matches 1,2,3 PINs from 3x4 Keypad Matrix
col_list=[1,2,3]
# following array matches 4,5,6,7 PINs from 3x4 Keypad Matrix
row_list=[4,5,6,7]
# set row pins to output and change array elements from
# int to Pin objects, all set to high
for x in range(0,4):
row_list[x]=Pin(row_list[x], Pin.OUT)
row_list[x].value(1)
# set columns pins to input and change array elements
# from int to Pin objects. We'll read user input here
for x in range(0,3):
col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
# Create a map between keypad buttons and chars
key_map=[["#","0","*"],\
["9","8","7"],\
["6","5","4"],\
["3","2","1"]]
def Keypad3x4Read(cols,rows):
for r in rows:
r.value(0)
result=[cols[0].value(),cols[1].value(),cols[2].value()]
if min(result)==0:
key=key_map[int(rows.index(r))][int(result.index(0))]
r.value(1) # manages key keept pressed
return(key)
r.value(1)
# Start the main loop
print("--- Ready to get user inputs ---")
while True:
key=Keypad3x4Read(col_list, row_list)
if key != None:
print("You pressed: "+key)
utime.sleep(0.3) # gives user enoght time to release without having double inputs
To get the reading of position encoder sensor direction change using Raspberry Pi Pico
Required Components
Position encoder sensor_1 no
Raspberry Pi Pico_1 no
Connecting Wires_4 set
Circuit
Steps
Make sure the components are working properly.
Connect the position encoder sensor to the Raspberry Pi Pico.
Connect the Raspberry Pi Pico pin GP8 & GP9 to Position encoder sensor CLK & DT pin and Position encoder SW pin to the GP2 of Raspberry Pi Pico.
Connect the +5v and ground(gnd) connections respectively.
Check the Python program.
Check the Electrical Circuit.
Run the Python program.
Python Program
from machine import Pin
import utime
DT_Pin = Pin(9, Pin.IN, Pin.PULL_UP)
CLK_Pin = Pin(8, Pin.IN, Pin.PULL_UP)
SW = Pin(2, Pin.IN, Pin.PULL_UP)
LEDs = [25,4]
#create an empty list to assing pins in pico
led_pins = []
for x in range(0,2):
led_pins.append(Pin(LEDs[x], Pin.OUT))
value = 0
previousValue = 1
def rotary_changed():
global previousValue
global value
if previousValue != CLK_Pin.value():
if CLK_Pin.value() == 0:
if DT_Pin.value() == 0:
value = (value - 1)%2
print("anti-clockwise", value)
else:
value = (value + 1)%2
print("clockwise", value)
previousValue = CLK_Pin.value()
if SW.value() == 0:
print("Button pressed")
utime.sleep(1)
while True:
for i in range(0,2):
led_pins[i].value(0)
rotary_changed()
led_pins[value].value(1)
utime.sleep(0.001)
Connect Raspberry Pi Pico board VBus and GND pin to the bread board by using wires.
Connect 220 Ω Resistor to the LED Anode (+) pin and LED cathode (-) pin to Gnd.
Connect 220 Ω Resistor another pin to GP16.
Connect 10 k Ω Resistor to the switch button.
Connect switch button another pin to the Raspberry Pi Pico pin GP17.
When we press the push button LED blinks.
Check the Python program.
Check the circuit connections.
Run the Python program.
Python Program
from machine import Pin
from time import sleep
led_pin = Pin(16, Pin.OUT) # 16 number in is Output
push_button = Pin(17, Pin.IN) # 17 number pin is input
while True:
logic_state = push_button.value()
if logic_state == True: # if push_button pressed
led_pin.value(1) # led will turn ON
else: # if push_button not pressed
led_pin.value(0)