In this tutorial, we’ll use the TDFA30608, a USB digital input board, to read the ON/OFF state of a pushbutton switch from a Raspberry Pi.
The TDFA30608 provides eight digital input channels, allowing your applications to read dry-contact signals and switch states over USB. Its input circuitry is electrically isolated from the USB interface, making it well suited for experiments involving industrial equipment and external power supplies.
We’ll start with a single pushbutton connected to CH0 and verify that pressing it turns the input ON.
What You’ll Need
| Item | Description |
|---|---|
| Raspberry Pi with an OS installed | Raspberry Pi 4 or 5 recommended, running Raspberry Pi OS |
| TDFA30608 digital input board | An isolated USB digital input module with eight channels |
| TDAC-USB2B1M5 USB cable | Connects the TDFA30608 to the Raspberry Pi |
| External power supply | A 5 V or 12 V supply for the input side of the TDFA30608 |
| Pushbutton switch | Used to test the ON/OFF state of CH0 |
| Hookup wire | For connecting the power supply, pushbutton, and TDFA30608 terminals |
| Internet connection | Required to install packages and download the source code |
The TDFA30608 accepts input voltages from 5 to 24 V. This tutorial assumes a 5 V or 12 V external power supply.
Wire the Input Circuit
For this example, we’ll connect the C terminal to the positive side of the external power supply.
Do not connect the TDFA30608 to the Raspberry Pi via USB just yet. Start by wiring the input side as follows:
External power supply + → TDFA30608 C terminal
External power supply − → One side of the pushbutton
Other side of pushbutton → TDFA30608 CH0 terminal
With this wiring:
- CH0 is OFF while the button is released.
- CH0 turns ON when the button is pressed.
The remaining channels work the same way. To use CH1, for example, connect the pushbutton to the CH1 terminal instead. The C terminal is shared, so it can remain connected to the positive side of the external power supply.
Once the wiring is complete, turn on the external power supply. We’ll check the input LED after connecting the board to the Raspberry Pi via USB.
Install the Required Tools
Update the Raspberry Pi, then install the development tools and libraries required to build TD-USB:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git build-essential libusb-dev python3
Configure USB Permissions
Create a udev rule so that non-root users can access the TDFA30608.
In the following rule, 32ee is the vendor ID for Tokyo Devices products, and 1770 is the product ID for the TDFA30608:
sudo tee /etc/udev/rules.d/99-usb-tokyodevices.rules <<EOF
SUBSYSTEM=="usb", ATTR{idVendor}=="32ee", ATTR{idProduct}=="1770", MODE="0666"
EOF
Reload the rules:
sudo udevadm control --reload-rules
sudo udevadm trigger
Now connect the TDFA30608 to the Raspberry Pi using the USB cable. If it was already connected, unplug it and reconnect it so that the new rule takes effect.
Press the pushbutton and check the LED corresponding to CH0 on the TDFA30608. If the LED lights up, the input circuit is wired correctly.
Build and Install TD-USB
The TDFA30608 is controlled using TD-USB, the official command-line utility. Clone the source code from GitHub and build it:
git clone https://github.com/tokyodevices/td-usb.git
cd td-usb
make
Run the utility without any arguments to verify that the build succeeded:
./td-usb
If the version information appears, the utility is working.
Next, check whether the TDFA30608 is detected:
./td-usb tdfa30608 list
XXXXXXXXXXXXXX
If a connected TDFA30608 is found, its serial number will be displayed.
Read the Digital Inputs
First, read the current input state once:
./td-usb tdfa30608 get
0
When the pushbutton is released, all inputs are OFF, so the command returns 0.
Now hold down the pushbutton connected to CH0 and run the same command again:
./td-usb tdfa30608 get
1
If the command returns 1, CH0 has been detected as ON.
The value returned by the TDFA30608 is an 8-bit bitmask representing all eight input channels. For example:
- CH0 corresponds to
1. - CH1 corresponds to
2. - If CH0 and CH1 are both ON, the value is
3.
| Active inputs | Binary representation | Returned value |
|---|---|---|
| None | 00000000 |
0 |
| CH0 only | 00000001 |
1 |
| CH1 only | 00000010 |
2 |
| CH0 and CH1 | 00000011 |
3 |
| CH7 only | 10000000 |
128 |
To read the inputs continuously, use the --loop option. The following command prints the input state every 200 milliseconds:
./td-usb tdfa30608 get --loop=200
0
0
1
1
0
Press Ctrl+C to stop monitoring.
Monitor the Input from Python
Next, we’ll use Python to run ./td-usb tdfa30608 get --loop=200 and print a message only when the state of CH0 changes.
Inside the td-usb directory, create a file named button_watch.py:
#!/usr/bin/env python3
import subprocess
import time
CMD = ["./td-usb", "tdfa30608", "get", "--loop=200"]
def is_on(value, channel):
return (value & (1 << channel)) != 0
proc = subprocess.Popen(
CMD,
stdout=subprocess.PIPE,
text=True,
bufsize=1,
)
last_state = None
print("Monitoring CH0. Press Ctrl+C to stop.")
try:
for line in proc.stdout:
try:
value = int(line.strip())
except ValueError:
continue
ch0 = is_on(value, 0)
if ch0 != last_state:
state_text = "ON" if ch0 else "OFF"
print(
f'[{time.strftime("%H:%M:%S")}] '
f"CH0: {state_text} Input value: {value}"
)
last_state = ch0
except KeyboardInterrupt:
proc.terminate()
print("\nMonitoring stopped.")
Run the Python Script
Run the script from the td-usb directory:
$ python3 button_watch.py
Monitoring CH0. Press Ctrl+C to stop.
[12:03:10] CH0: OFF Input value: 0
[12:03:14] CH0: ON Input value: 1
[12:03:17] CH0: OFF Input value: 0
If the output changes to CH0: ON when you press the button and returns to CH0: OFF when you release it, everything is working correctly.
Monitor Additional Channels
The wiring and Python logic are the same for CH1 through CH7.
To monitor CH2, for example, connect the pushbutton to the CH2 terminal and pass channel number 2 to is_on():
ch2 = is_on(value, 2)
You can also connect multiple switches and monitor CH0, CH1, and CH2 simultaneously. Because the input value is a bitmask, your application can determine exactly which combination of channels is currently ON.
Where to Go from Here
In this tutorial, we connected a pushbutton to the TDFA30608 and read its ON/OFF state from a Raspberry Pi.
Once you can read digital inputs, you can build applications such as:
- Detecting whether a door, guard, or equipment cover is open
- Using a foot switch or pushbutton as an input for a PC application
- Logging contact outputs from industrial equipment
- Monitoring multiple switches from a single Raspberry Pi
Because the TDFA30608 provides straightforward USB access to isolated digital inputs, it is a practical starting point for connecting external switches and contact signals to your own software. Begin with a single channel, then expand your application to monitor multiple inputs as needed.


