import time
from machine import SPI, Pin
import max7219
spi = SPI(0, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19))
cs = Pin(15, Pin.OUT)
display = max7219.Matrix8x8(spi, cs)
display.brightness(8)
display.fill(0)
display.show()
symbols = [
# Smiley face
[
0b00111100,
0b01000010,
0b10100101,
0b10000001,
0b10100101,
0b10011001,
0b01000010,
0b00111100
],
# Heart
[
0b00000000,
0b01100110,
0b11111111,
0b11111111,
0b01111110,
0b00111100,
0b00011000,
0b00000000
],
# Circle
[
0b00111100,
0b01111110,
0b11111111,
0b11100111,
0b11100111,
0b11111111,
0b01111110,
0b00111100
],
# X mark
[
0b10000001,
0b01000010,
0b00100100,
0b00011000,
0b00011000,
0b00100100,
0b01000010,
0b10000001
],
# Plus Sign
[
0b00011000,
0b00011000,
0b00011000,
0b11111111,
0b11111111,
0b00011000,
0b00011000,
0b00011000
]
]
def display_symbol(symbol):
display.fill(0)
for y, row in enumerate(symbol):
for x in range(8):
if row & (1 << (7 - x)):
display.pixel(x, y, 1)
display.show()
while True:
for symbol in symbols:
display_symbol(symbol)
time.sleep(1)
# ============================================================
# MAX7219 8x8 LED MATRIX → RASPBERRY PI PICO WIRING GUIDE
# ============================================================
# MAX7219 Pin | Pico GPIO Pin | Wire Color (from image)
# ------------------------------------------------------------
# VCC → 3V3(OUT) | ORANGE
# GND → GND | BLACK
# DIN (Data In) → GP19 (SPI0 TX) | PINK
# CS (LOAD) → GP15 | BLUE
# CLK → GP18 (SPI0 SCK) | YELLOW
# ------------------------------------------------------------
# Notes:
# - match these pins in your MAX7219 code:
# sck = Pin(18)
# mosi = Pin(19)
# cs = Pin(15)
# - Power from 3.3V is safe for Pico + MAX7219 module.
# - Make sure GND of Pico and Matrix are connected.
# ============================================================
from machine import Pin, ADC, PWM
from time import sleep
pot = ADC(26)
red = PWM(Pin(15))
green = PWM(Pin(14))
blue = PWM(Pin(13))
for led in [red, green, blue]:
led.freq(1000)
def set_color(r, g, b):
red.duty_u16(int((r / 255) * 65535))
green.duty_u16(int((g / 255) * 65535))
blue.duty_u16(int((b / 255) * 65535))
while True:
pot_value = pot.read_u16()
angle = (pot_value / 65535) * 90
if angle <= 30:
t = angle / 30
r = int(255 * (1 - t))
g = int(255 * t)
b = 0
elif angle <= 60:
t = (angle - 30) / 30
r = 0
g = int(255 * (1 - t))
b = int(255 * t)
else:
r, g, b = 0, 0, 255
set_color(r, g, b)
print("Potentiometer: {:5d} | Angle: {:5.1f}° | LED RGB: ({:3d}, {:3d}, {:3d})"
.format(pot_value, angle, r, g, b))
sleep(0.1)
# ============================
# Raspberry Pi Pico Pin Layout
# ============================
# Potentiometer (ADC)
# Potentiometer VCC -> 3.3V (Pin 36 or Pin 39)
# Potentiometer GND -> GND (Pin 38)
# Potentiometer OUT -> GP26 / ADC0 (Pin 31)
# RGB LED (Common Anode OR Common Cathode depending on your setup)
# Red LED pin -> GP15 (PWM)
# Green LED pin -> GP14 (PWM)
# Blue LED pin -> GP13 (PWM)
# LED common pin -> 3.3V or GND depending on LED type
# (Based on your diagram, the resistors go between Pico and each LED pin.)
# Resistors (3 pcs, one per color)
# GP15 → resistor → Red LED pin
# GP14 → resistor → Green LED pin
# GP13 → resistor → Blue LED pin
# Breadboard Power Rails
# Pico 3.3V -> Red rail
# Pico GND -> Blue rail
from machine import Pin
import utime
class LCD:
def __init__(self, rs, en, d4, d5, d6, d7):
self.rs = Pin(rs, Pin.OUT)
self.en = Pin(en, Pin.OUT)
self.data_pins = [
Pin(d4, Pin.OUT),
Pin(d5, Pin.OUT),
Pin(d6, Pin.OUT),
Pin(d7, Pin.OUT)
]
self.init_lcd()
def pulse_enable(self):
self.en.value(1)
utime.sleep_us(40)
self.en.value(0)
utime.sleep_us(40)
def send_nibble(self, data):
for i in range(4):
self.data_pins[i].value((data >> i) & 1)
self.pulse_enable()
def send_byte(self, data, mode):
self.rs.value(mode)
self.send_nibble(data >> 4)
self.send_nibble(data & 0x0F)
utime.sleep_ms(2)
def command(self, cmd):
self.send_byte(cmd, 0)
def write_char(self, char):
self.send_byte(ord(char), 1)
def clear(self):
self.command(0x01)
utime.sleep_ms(2)
def move_to(self, col, row):
pos = col + 0x40 * row
self.command(0x80 | pos)
def putstr(self, string):
for char in string:
self.write_char(char)
def init_lcd(self):
utime.sleep_ms(20)
self.command(0x33)
self.command(0x32)
self.command(0x28)
self.command(0x0C)
self.command(0x06)
self.command(0x01)
utime.sleep_ms(5)
lcd = LCD(rs=16, en=13, d4=19, d5=20, d6=21, d7=26)
keypad_rows = [Pin(pin, Pin.OUT) for pin in [28, 22, 27, 18]]
keypad_columns = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in [17, 15, 14, 4]]
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
password = "1234"
input_buffer = ""
is_unlocked = False
def read_keypad():
for row in range(4):
keypad_rows[row].high()
utime.sleep(0.01)
for col in range(4):
if keypad_columns[col].value() == 1:
keypad_rows[row].low()
return keys[row][col]
keypad_rows[row].low()
return None
def request_password():
global input_buffer, is_unlocked
lcd.clear()
lcd.putstr("Enter Password:")
while not is_unlocked:
key = read_keypad()
if key:
if key == '#':
if input_buffer == password:
lcd.clear()
lcd.putstr("Unlocked!")
utime.sleep(2)
lcd.clear()
is_unlocked = True
else:
lcd.clear()
lcd.putstr("Wrong Password!")
utime.sleep(2)
lcd.clear()
lcd.putstr("Enter Password:")
input_buffer = ""
elif key == '*':
input_buffer = ""
lcd.clear()
lcd.putstr("Enter Password:")
else:
input_buffer += key
lcd.move_to(0, 1)
lcd.putstr('*' * len(input_buffer))
utime.sleep(0.3)
def enter_text():
global input_buffer
lcd.clear()
lcd.putstr("Enter Text:")
while True:
key = read_keypad()
if key:
if key == '#':
lcd.clear()
lcd.putstr(input_buffer)
input_buffer = ""
elif key == '*':
input_buffer = ""
lcd.clear()
lcd.putstr("Enter Text:")
else:
input_buffer += key
lcd.move_to(0, 1)
lcd.putstr(input_buffer)
utime.sleep(0.3)
try:
request_password()
if is_unlocked:
enter_text()
except KeyboardInterrupt:
lcd.clear()
print("Program interrupted.")
# ================= PIN MAPPING BASED ON CIRCUIT DIAGRAM =================
# LCD (16x2) → Raspberry Pi Pico
# RS → GP16
# EN → GP13
# D4 → GP19
# D5 → GP20
# D6 → GP21
# D7 → GP26
#
# LCD Power:
# VSS → GND
# VDD → +5V
# RW → GND
# Potentiometer middle pin → LCD V0 (contrast)
#
# Keypad (4×4):
# ROW0 → GP28
# ROW1 → GP22
# ROW2 → GP27
# ROW3 → GP18
#
# COL0 → GP17
# COL1 → GP15
# COL2 → GP14
# COL3 → GP4
# =======================================================================
from machine import Pin, ADC, PWM
from time import sleep
# =============================
# PIN DEFINITIONS
# =============================
# Potentiometer (Angle Sensor)
pot = ADC(26) # GP26 (ADC0)
# LDR Sensor (Object Detection)
ldr = ADC(27) # GP27 (ADC1)
# RGB LED Pins
red = PWM(Pin(15)) # GP15
green = PWM(Pin(14)) # GP14
blue = PWM(Pin(13)) # GP13
# Set PWM frequency
for led in (red, green, blue):
led.freq(1000)
# =============================
# RGB LED Function
# =============================
def set_color(r, g, b):
"""Set RGB LED brightness (0–255 per channel)."""
red.duty_u16(int((r / 255) * 65535))
green.duty_u16(int((g / 255) * 65535))
blue.duty_u16(int((b / 255) * 65535))
# =============================
# Angle → RGB color mapping
# =============================
def get_angle_color(angle):
"""
0–30° → Red → Green transition
30–60° → Green → Blue transition
60–90° → Solid Blue
"""
if angle <= 30:
t = angle / 30
r = int(255 * (1 - t))
g = int(255 * t)
b = 0
elif angle <= 60:
t = (angle - 30) / 30
r = 0
g = int(255 * (1 - t))
b = int(255 * t)
else:
r, g, b = 0, 0, 255
return r, g, b
# =============================
# Main Program Loop
# =============================
print("System Running...")
while True:
# Read sensors
pot_value = pot.read_u16()
ldr_value = ldr.read_u16()
# Convert pot value to angle (0–90°)
angle = (pot_value / 65535) * 90
# Get RGB color based on angle
r, g, b = get_angle_color(angle)
# LDR detection logic
# (Fine-tune threshold depending on your lighting)
OBJECT_THRESHOLD = 25000
if ldr_value < OBJECT_THRESHOLD:
object_state = "OBJECT PRESENT"
set_color(r, g, b) # Normal LED behavior
else:
object_state = "OBJECT REMOVED"
# Flashing RED alert
set_color(255, 0, 0)
sleep(0.1)
set_color(0, 0, 0)
sleep(0.1)
# Debug display
print(
"Angle: {:5.1f}° | Pot: {:5d} | LDR: {:5d} | State: {} | RGB: ({:3d},{:3d},{:3d})"
.format(angle, pot_value, ldr_value, object_state, r, g, b)
)
sleep(0.1)