News: ES33A08 ES33B08 ESP32 micropython code paste the code in Thonny (https://thonny.org/)
from machine import Pin, ADC
import time
# Define pins for the shift register 74HC595
CLOCK_595 = 27 # Clock pin for 74HC595
LATCH_595 = 14 # Latch pin for 74HC595
DATA_595 = 13 # Data pin for 74HC595
OE_595 = 4 # Output Enable for 74HC595, active low
# Define pins for the input shift register 74HC165
CLOCK_165 = 18 # Clock pin for 74HC165
LOAD_165 = 19 # Load pin for 74HC165
DATA_165 = 21 # Data pin for 74HC165
# Define pin for the PIR sensor
PIR_PIN = 5
# Initialize GPIO pins for 74HC595
clock_595 = Pin(CLOCK_595, Pin.OUT)
latch_595 = Pin(LATCH_595, Pin.OUT)
data_595 = Pin(DATA_595, Pin.OUT)
oe_595 = Pin(OE_595, Pin.OUT)
# Initialize GPIO pins for 74HC165
clock_165 = Pin(CLOCK_165, Pin.OUT)
load_165 = Pin(LOAD_165, Pin.OUT)
data_165 = Pin(DATA_165, Pin.IN)
# Initialize GPIO pin for PIR sensor
pir_sensor = Pin(PIR_PIN, Pin.IN)
# Initialize ADC for LM324DR (Analog signal processing)
analog_input = ADC(Pin(32)) # Use GPIO32 for analog input
# Ensure output is disabled during setup
oe_595.value(1) # Disable output (active low)
# Number to display mapping for 7-segment (decimal representation)
number_mapping = {
0: 252, # 0b11111100, 0xFC, Configuration for '0'
1: 96, # 0b01100000, 0x60, Configuration for '1'
2: 218, # 0b11011010, 0xDA, Configuration for '2'
3: 242, # 0b11110010, 0xF2, Configuration for '3'
4: 102, # 0b01100110, 0x66, Configuration for '4'
5: 182, # 0b10110110, 0xB6, Configuration for '5'
6: 190, # 0b10111110, 0xBE, Configuration for '6'
7: 224, # 0b11100000, 0xE0, Configuration for '7'
8: 254, # 0b11111110, 0xFE, Configuration for '8'
9: 246 # 0b11110110, 0xF6, Configuration for '9'
}
# Text to display mapping for 7-segment (common cathode configuration)
text_mapping = {
'A': 238, # 0b11101110, 0xEE, Configuration for 'A'
'b': 62, # 0b00111110, 0x3E, Configuration for 'b'
'C': 156, # 0b10011100, 0x9C, Configuration for 'C'
'd': 122, # 0b01111010, 0x7A, Configuration for 'd'
'E': 158, # 0b10011110, 0x9E, Configuration for 'E'
'F': 142, # 0b10001110, 0x8E, Configuration for 'F'
'G': 188, # 0b10111100, 0xBC, Configuration for 'G'
'H': 110 # 0b01101110, 0x6E, Configuration for 'H'
}
# Digit activation (decimal representation, assuming common cathode for simplicity, modify if using common anode)
digit_activation = {
1: 14, # 0b1110, 0xE, Activate the first digit (leftmost)
2: 13, # 0b1101, 0xD, Activate the second digit
3: 11, # 0b1011, 0xB, Activate the third digit
4: 7 # 0b0111, 0x7, Activate the fourth digit (rightmost)
}
# Channel control bits (decimal representation)
channel_mapping = {
1: 32768, # 0b1000000000000000, 0x8000
2: 16384, # 0b0100000000000000, 0x4000
3: 8192, # 0b0010000000000000, 0x2000
4: 4096, # 0b0001000000000000, 0x1000
5: 2048, # 0b0000100000000000, 0x0800
6: 1024, # 0b0000010000000000, 0x0400
7: 512, # 0b0000001000000000, 0x0200
8: 256 # 0b0000000100000000, 0x0100
}
# Input control bits for 74HC165 (D0 to D7, decimal representation)
input_mapping = {
1: 1, # 0b00000001, 0x01
2: 2, # 0b00000010, 0x02
3: 4, # 0b00000100, 0x04
4: 8, # 0b00001000, 0x08
5: 16, # 0b00010000, 0x10
6: 32, # 0b00100000, 0x20
7: 64, # 0b01000000, 0x40
8: 128 # 0b10000000, 0x80
}
# Voltage input bits (V1 to V4, decimal representation)
v_mapping = {
1: ADC(Pin(32)), # V1, GPIO32
2: ADC(Pin(33)), # V2, GPIO33
3: ADC(Pin(34)), # V3, GPIO34
4: ADC(Pin(35)) # V4, GPIO35
}
# Current input bits (I1 to I4, decimal representation)
i_mapping = {
1: ADC(Pin(36)), # I1, GPIO36
2: ADC(Pin(37)), # I2, GPIO37
3: ADC(Pin(38)), # I3, GPIO38
4: ADC(Pin(39)) # I4, GPIO39
}
# Function to write value to the shift register
def write_shift_register(value):
latch_595.value(0) # Prepare to latch data
for i in range(32): # Shift 32 bits of data
data_595.value((value >> i) & 1)
clock_595.value(1)
time.sleep_us(10)
clock_595.value(0)
latch_595.value(1) # Latch the data at the output of the shift registers
oe_595.value(0) # Enable output to take effect
# Function to read value from the input shift register
def read_shift_register():
load_165.value(0) # Load the parallel input to the shift register
time.sleep_us(10)
load_165.value(1)
value = 0
for i in range(8): # Read 8 bits of data
value |data_165.value() << (7 - i))
clock_165.value(1)
time.sleep_us(10)
clock_165.value(0)
return value
# Function to control CH1 based on IN1 and PIR sensor status
def control_ch1():
input_status = read_shift_register()
in1_status input_status >> 0) & 1 # Read the status of IN1
pir_status = pir_sensor.value() # Read the status of the PIR sensor
if in1_status == 1 or pir_status == 1: # If IN1 or PIR is high
combined_value = channel_mapping[1] # Activate CH1
else: # If both IN1 and PIR are low
combined_value = 0 # Deactivate all channels
write_shift_register(combined_value)
# Function to read analog inputs (V* and I*)
def read_analog_inputs():
analog_values = {}
for key, adc in v_mapping.items():
analog_values[f"V{key}"] = adc.read()
for key, adc in i_mapping.items():
analog_values[f"I{key}"] = adc.read()
return analog_values
# Function to indicate if something is connected to the inputs
def indicate_input_connections(input_status):
for i in range(8):
if (input_status >> i) & 1:
print(f"IN{i+1} is connected")
# Main loop
while True:
# Control CH1 based on IN1 and PIR sensor status
control_ch1()
# Read input status and indicate connections
input_status = read_shift_register()
indicate_input_connections(input_status)
# Print digital values
digital_values = {f"IN{i+1}": (input_status >> i) & 1 for i in range(8)}
print("Digital values:", ", ".join([f"IN{i+1}: {val}" for i, val in enumerate(digital_values.values())]))
# Read analog input (e.g., from LM324DR)
analog_values = read_analog_inputs()
print(f"Analog values: {analog_values}")
# Cycle through numbers 0 to 9 and corresponding channels
for channel in range(1, 9):
# Calculate the channel bit pattern (OUT 16 to OUT 9 corresponds to channels 1 to 8)
channel_bit_pattern = 1 << (16 - channel)
# Get the number to display and calculate the combined value for the shift register
number_to_display = channel % 10 # Display the same number as the channel
combined_value number_mapping[number_to_display] << 24) | \
(digit_activation[1] << 20) | \
channel_bit_pattern
# Write the combined value to the shift register
write_shift_register(combined_value)
# Print the current state for debugging
print(f"Channel {channel} is active, displaying number {number_to_display}")
# Wait before moving to the next number and channel
time.sleep(1)
# Additional delay before repeating the cycle, if needed
time.sleep(1)