Labs / Electronic modules

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Author: Li Zisheng · Source: FreakStudio open documentation, v1.3 · License: CC BY 4.0 · Driver code (input/pcf8574keys_driver): MIT License

Contents
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

I. Module Introduction

5-direction key module based on PCF8574, with the PCF8574DWR chip as the core, connects to the microcontroller via the I2C interface (communication voltage level: 3.3V). Equipped with a 5-direction key (with independent contacts for center, up, down, left and right), tact switches and LED indicators, the module also features chip address selection, MCU interface, power filtering and indicator lights, which is suitable for key input expansion scenarios of embedded devices and provides a flexible human-computer interaction key control solution for the system.

II. Introduction to Main Chips

PCF8574DWR is an I²C bus expansion chip that can extend the 2-wire I²C interface to an 8-bit parallel I/O port, supporting bidirectional data transmission without requiring additional clock or control signals. It is compatible with a wide supply voltage range of 2.5V to 6V, operates at a temperature range of -40°C to 85°C, features strong output drive capability (can directly drive LED s), incorporates a built-in power-on reset circuit, and comes in a 16-pin SOIC package. With compact size and high compatibility, it is widely used in scenarios requiring additional parallel interfaces such as microcontroller I/O port expansion, industrial control, and smart homes.

A five-way key is a common input device, typically used in the control interfaces of electronic products such as remote controls, game controllers and mobile devices. It contains five internal contacts: a common contact at the center, with multiple fixed contacts distributed around it. Its operating principle relies on the movable contact spring to connect with the common contact and the surrounding fixed contacts, so as to realize the switching of multiple states. When the operating lever is moved, the movable contact spring comes into contact with the corresponding contact, thereby forming a circuit connection.

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

A 5-way key looks like a single key, but it actually contains five distinct key states. By pressing it in different directions, users can switch between multiple functions. Here, we connect the common contact (COM terminal) of the 5-way key to the + 5V power supply. When none of the keys is pressed, the voltage measured at any key is 0V, because all contacts are in an open circuit state and no current flows; when any key is pressed, the movable contact spring of that key will connect with the common contact to form a closed circuit, and the voltage of the corresponding key measured at this time will rise to the supply voltage (5V).

2.1 Chip Overview

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

PCF8574 is a chip that implements general-purpose I²C protocol-based I/O port expansion, equipped with 8-bit quasi-bidirectional GPIO ports (P0 to P7), which can be flexibly used as input/output without direction control signals. After power-on, the GPIO pins default to high level, can directly drive LED s, and also support open-drain interrupt output;It is compatible with 1.4V~5.5V power supply, operates within the temperature range of -40℃~+ 85℃, and has a standby power consumption of <1μA. It is mainly applied to communication cabinets, servers, industrial automation and GPIO processor devices with insufficient resources to meet the I/O port expansion requirements.

2.2 Pin Definition

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

2.3 Functional Block Diagram

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

PCF8574DWR The functional block diagram is described as follows:

Typical Scenario (Taking I/O Expansion as an Example):

2.4 Performance Parameters

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

PCF8574DWR Some of the parameter descriptions are as follows:

2.5 Reference Circuit

None.

2.6 Chip Configuration

None.

2.7.1 I²C Bus Features

I²C bus is used for bidirectional two-wire communication between different ICs or modules. The two wires are the Serial Data Line (SDA) and the Serial Clock Line (SCL) respectively. When connected to the output stage of a device, both wires must be connected to the positive power supply via pull-up resistors. Data transmission can only be initiated when the bus is idle.

2.7.2 Bit Transmission

One bit of data is transmitted per clock pulse. During the high level of the clock pulse, SDA line must remain stable, as any change on the data line at this time will be interpreted as a control signal.

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

2.7.3 Start and Stop Conditions

When the bus is idle, both the data line and the clock line remain at a high level. When the clock is at a high level, the transition of the data line from high to low is defined as a start condition (S). When the clock is at a high level, the transition of the data line from low to high is defined as a stop condition (P).

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

2.7.4 System Configuration

The device that generates messages is the "transmitter", and the device that receives messages is the "receiver". The device that controls messages is the "master device", and the device controlled by the master device is the "slave device"(see the figure below).

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Between the start condition and the stop condition, there is no limit on the number of data bytes transmitted from the transmitter to the receiver. Each 8-bit byte is followed by 1 acknowledge bit. The acknowledge bit is a high level driven onto the bus by the transmitter, while the master device generates an additional acknowledge-related clock pulse.

The addressed slave receiver must generate an acknowledge after the reception of each byte. Also a master must generate an acknowledge after the reception of each byte that has been clocked out by the slave transmitter. The device that acknowledges must pull down the SDA line during the acknowledge clock pulse, so that the SDA line is stable low during the HIGH period of the acknowledge related clock pulse, taking into account the setup and hold times.

The master receiver must signal the end of data to the transmitter by not generating an acknowledge for the last byte of the clock output from the slave device. In this case, the transmitter must hold the data line high to allow the master to generate a stop condition.

Write Mode Sequence Diagram:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Read Mode Sequence Diagram:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

When SCL is at a high level, a high-to-low transition of SDA is defined as the stop condition (P). Data transmission can be stopped at any time via the stop condition. When this occurs, the valid data from the last acknowledge phase (in output mode) remains valid, while the input data will be lost.

2.7.5 I²C Bus Timing Characteristics

SymbolParameterMinTypMaxUnit
fSCLSCL clock frequency--100kHz
tSWTolerable spike width on the bus--100ns
tBUFBus free time4.7--μs
tSU:STAStart condition setup time4.7--μs
tHD:STAStart condition hold time4--μs
tLOWSCL low time4.7--μs
tHIGHSCL high time4--μs
trSCL and SDA rise time--1μs
tfSCL and SDA fall time--0.3μs
tSU:DATData setup time250--ns
tHD:DATData hold time0--ns
tVD:DATSCL low to data output valid time--3.4μs
tSU:STOStop condition setup time4--μs

Note: All timing values are valid within the range of operating power supply voltage and ambient temperature, and refer to V SS to V DD for the input voltage swing, corresponding to V IL and V IH.

The Sequence Diagram of the IIC bus is as follows:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

2.8 PCB Routing Precautions

None.

III. Circuit Engineering

3.1 Circuit Source Engineering

To obtain the source project of the circuit, please follow the official account [Freak Embedded]

3.2 Module Usage Instructions

SCH_Schematic1_0.96-inch SSD1306-OLED Module-V1.1_2025-11-13.pdf 1.9 MB

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

This module provides an I2C pins. It is compatible with 3.3V communication level, and can stably transmit the input signals of the 5-way buttons (center, up, down, left, right) and tact switches. It is suitable for scenarios such as human-computer interaction button expansion for embedded devices and button input control for maker projects, providing the system with flexible I2C communication rates, and can flexibly configure 8 types of I2C device addresses ranging from 0x20 to 0x27 via the A0, A1, and A2 pins. It is compatible with 3.3V communication level, and can stably transmit the input signals of the 5-way buttons (center, up, down, left, right) and tact switches. It is suitable for scenarios such as human-computer interaction button expansion for embedded devices and button input control for maker projects, providing the system with flexible I/O extended button interaction capabilities.

PCF8574DWR 's IIC address can be selected via the 3 short-circuit points on the back of the physical device, and the address truth table is shown in the following figure.

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)
GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

3.3 Application Experiment

Here, we use the GraftPort-RP2040 development board for testing, and connect the 5-way button module based on PCF8574 to communicate with the main controller via the I2C interface.

Here, we need to set the address bits in advance by soldering two short-circuit points:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Here, we first use the HY2.0-4P cable to connect the module to the GraftPort-RP2040 development board's I2C0 interface:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

The module connections are shown in the following table:

Development Board Pins

Module Pins

Description

Pin 4

SDA

I2C data transmission line, corresponding to the I2C0 SDA

Pin 5

SCL

I2C clock signal line, corresponding to I2C0 's SCL

Here, we need to search for the relevant driver packages on uPyPI: https://upypi.net/zh/

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Enter pcf8574keys_driver to search, and the following will be displayed:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Click to copy the local installation command:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

Here, we can use the mip tool to import module driver library code. The following are the usage commands for the mpremote tool:

mpremote mip install github:FreakStudioCN/GraftSense-Drivers-MicroPython/input/pcf8574keys_driver

After importing the PCF8574 5-way button module - MicroPython driver code, copy the following main.py code into our project:

# Python env   : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time    : 2025/9/8 AM 11:42
# @Author  : 缪贵成
# @File    : main.py
# @Description : Five-way button test file

# ======================================== Import Modules =========================================

from machine import I2C, Pin
import time
from pcf8574 import PCF8574
from pcf8574keys import PCF8574Keys, KEYS_MAP

# ======================================== Global Variables ============================================

# I2C configuration
I2C_ID = 0
# Modify according to actual hardware
SCL_PIN = 5
# Modify according to actual hardware
SDA_PIN = 4

PCF8574_ADDR = None

# ======================================== Functions ============================================

# ======================================== Custom Classes ============================================

# ======================================== Initialization ==========================================

time.sleep(3)
print("FreakStudio:PCF8574 Five-way Button Test Program")
# Initialize I2C
i2c = I2C(I2C_ID, scl=Pin(SCL_PIN), sda=Pin(SDA_PIN), freq=400000)

# Start scanning for devices on the I2C bus and return a list of slave addresses
devices_list: list[int] = i2c.scan()
print('START I2C SCANNER')

# If the devices list is empty, no device is connected to the I2C bus
if len(devices_list) == 0:
    # If not empty, print the slave device addresses
    print("No i2c device !")
else:
    # Iterate over the slave device address list
    print('i2c devices found:', len(devices_list))
for device in devices_list:
    # Check whether the device address is the PCF8574 address
    if 0x20 <= device <= 0x28:
        # Assume the first device found is the PCF8574 address
        print("I2c hexadecimal address:", hex(device))

        PCF8574_ADDR = device
# Initialize PCF8574
pcf = PCF8574(i2c, PCF8574_ADDR)
# Initialize the five-way button
keys = PCF8574Keys(pcf, KEYS_MAP)

# ========================================  Main Program  ============================================

while True:
    # Print the current state of all buttons
    all_states = keys.read_all()
    print(all_states)
    # Check the SW1 and SW2 button states to control the LED on/off
    if keys.read_key('SW1') == True:
        keys.led_on()
    if keys.read_key('SW2') == True:
        keys.led_off()
    # Refresh the status display every 100ms
    time.sleep(0.1)

To flash the code, open a terminal and use the mpremote tool to connect to the main controller; the output is as follows:

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)

As you can see, when a key is pressed, the corresponding output in the terminal will become press.

IV. Drivers

pcf8574keys_driver.zip 13 KB

None.

VI. Revision History

Document Version

Date of Modification

Modifier

Modified Content

v1.0

2025-11-13

Ling Gaoxing

Initialize the document

v1.1

2025-12-29

Li Zisheng

Added application experiments and production files

v1.2

2025-12-31

Xin Ruili

Create an open version of the document based on the internal document version v1.1.

v1.3

2026-03-29

Xin Ruili

Modified the application experiment section.

VII. Relevant Materials

7.1 Chip Datasheet

PCF8574 Manual.pdf 3.4 MB

7.2 Unique Icons for PCB Silkscreen

GraftSense: 5-Directional Button Module Based on PCF8574 (Open Version)