Introduction: Raspberry Pi - TMD26721 Infrared Digital Proximity Detector Python Tutorial

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

TMD26721 is an infrared digital proximity detector which provides a complete proximity detection system and digital interface logic in a single 8-pin surface mount module.The proximity detection includes improved signal-to-noise and accuracy. A proximity offset register allows compensation for optical system crosstalk between the IR LED and the sensor. Here is its demonstration with raspberry pi using python code.

Step 1: What You Need..!!

1. Raspberry Pi

2. TMD26721

3. I²C Cable

4. I²C Shield for Raspberry Pi

5. Ethernet Cable

Step 2: Connections:

Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.

Then connect the one end of I2C cable to TMD26721 sensor and the other end to the I2C shield.

Also connect the Ethernet cable to the pi or you can use a WiFi module.

Connections are shown in the picture above.

Step 3: Code:

The python code for TMD26721 can be downloaded from our github repository- ControlEverythingCommunity

Here is the link for the same :

The datasheet of TMD26721 can be found here:

We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:

You can also copy the code from here, it is given as follows:

# Distributed with a free-will license.

# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.

# TMD26721

# This code is designed to work with the TMD26721_I2CS I2C Mini Module available from


import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# TMD26721 address, 0x39(57)

# Select enable register register, 0x00(0), with command register 0x80(128)

# 0x0D(14) Power on, Wait enabled, Proximity enabled

bus.write_byte_data(0x39, 0x00 | 0x80, 0x0D)

# TMD26721 address, 0x39(57)

# Select proximity time control register, 0x02(2), with command register 0x80(128)

# 0xFF(255) Time = 2.73 ms

bus.write_byte_data(0x39, 0x02 | 0x80, 0xFF)

# TMD26721 address, 0x39(57)

# Select wait time register 0x03(03), with command register, 0x80(128)# 0xFF(255) Time - 2.73ms

bus.write_byte_data(0x39, 0x03 | 0x80, 0xFF)

# TMD26721 address, 0x39(57# Select pulse count register, 0x0E(14), with command register 0x80(128)

# 0x20(32) Pulse count = 32

bus.write_byte_data(0x39, 0x0E | 0x80, 0x20)

# TMD26721 address, 0x39(57)

# Select control register, 0x0F(15), with command register 0x80(128)

# 0x20(32) Proximity uses CH1 diode

bus.write_byte_data(0x39, 0x0F | 0x80, 0x20)


# TMD26721 address, 0x39(57)

# Read data back from 0x18(57) with command register 0x80(128), 2 bytes

# Proximity lsb, Proximity msb

data = bus.read_i2c_block_data(0x39, 0x18 | 0x80, 2)

# Convert the data

proximity = data[1] * 256 + data[0]

# Output data to screen

print "Proximity of the Device : %d" %proximity

Step 4: Applications:

TMD26721 is an infrared digital proximity sensor which can be incorporated in Mobile Handset Touchscreen Control and Automatic Speakerphone Enable. It can also provide Mechanical Switch Replacement as well as Paper Alignment. Its high efficiency and reliability makes it suitable for various proximity sensing applications.