As you know there are almost none Raspberry Pi on the market (sold out, not available) where I got an Orange Pi zero 2 and for increasing the GPIOs I bought a PI4IOE5V96248 Expander. (48-bit I/O pins )
This communicates by I2C,
Was created the next python code:
import smbus
import time
bus = smbus.SMBus(3)
slave_address = 0x20
def set_led_state(states):
try:
bus.write_i2c_block_data(slave_address, 0x00, states)
print("Address acknowledgment: Received")
print("Data byte acknowledgment: Received")
print(f"Address byte sent: 0x{slave_address:02X}")
print(f"Data bytes sent: {[f'0x{s:02X}' for s in states]}")
except IOError:
print("Address acknowledgment: Not received")
print("Data byte acknowledgment: Not received")
# Define LED states
LED_OFF = 0x00
LED_1 = 0x01
LED_2 = 0x02
LED_3 = 0x04
LED_4 = 0x08
LED_5 = 0x10
LED_6 = 0x20
LED_7 = 0x40
LED_8 = 0x80
# Prompt the user to select an LED
def prompt_led_selection():
led = int(input("Enter the LED number (1-48) to switch on (0 to exit): "))
if led == 0:
return None
elif led < 1 or led > 48:
print("Invalid LED number. Please try again.")
return prompt_led_selection()
else:
return led
# Repeat the LED selection until the user chooses to exit (0)
while True:
selected_led = prompt_led_selection()
if selected_led is None:
break
# Calculate the LED state based on the selected LED number
state_index = (selected_led - 1) // 8
bit_position = (selected_led - 1) % 8
# Create an array of LED states
states = [LED_OFF] * 6
states[state_index] = 0x01 << bit_position
set_led_state(states)
# Print the selected LED number
print(f"LED {selected_led} switched ON")
time.sleep(0.1) # Delay for 2 seconds
# Turn off all LEDs
set_led_state([LED_OFF] * 6)
print("All LEDs are OFF")
While the previous code can work more or less, the issue is that the pin 9 is recognized as 1. Then the Pin 40 is recognized as 48. Where from the 0-8 the pins are not working.
The datasheet for the PI4IOE5V96248 says:
To write, the master (microcontroller) first addresses the slave device. By setting the last bit of the byte containing the slave address to logic 0 the Write mode is entered. The PI4IOE5V96248 acknowledges and the master sends the first data byte for IO0_7 to IO0_0. After the first data byte is acknowledged by the PI4IOE5V96248, the second data byte IO1_7 to IO1_0 is sent by the master.
After the second data byte is acknowledged by the PI4IOE5V96248, the three data byte IO2_7 to IO2_0 is sent by the master and so on.
Any idea what could be the source of the problem?
I tried mapping LED by LED, and I tried modifying the section code of the # Create an array of LED states
I tried modifying also the section for # Calculate the LED state based on the selected LED number.
But even there was worst cases where there was not any led switching on, so I shared one that at least is working from 9-48 pins (gpio 8-47)