Introduction
If you are interested in sensors, you are probably familiar with proximity detection sensors. There are various sensors available for proximity detection; each with its own capabilities and limitations.
One method is to use the infrared transceivers. These sensors measure the time it takes for an infrared beam to travel back and forth and calculate the distance between the sensor and the target surface.
In this tutorial, we’ll introduce the TMD27713 distance detection sensor. Whether you are a beginner or experienced—and interested in Arduino and Electronics—this guide can help you interface the TMD27713 sensor step-by-step.
What You Will Learn
- How to interface the TMD27713 sensor
- Detecting the distance and ambient light intensity with the TMD27713 sensor
TMD27713 Sensor in a Nutshell
The TMD27713 chip is a proximity detection sensor based on infrared beams. We can also use its receiver to detect the ambient light intensity. The chip includes a digital proximity sensor, an LED driver, and an IR LED, which are integrated together to eliminate the need for calibration.
In addition, this chip can eliminate the background light, allowing it to work in various conditions, from very bright environments to dark rooms. It also has a wide dynamic range, making it suitable for detecting short distances. Moreover, the chip transmits and receives infrared energy much more efficiently using micro-optic lenses.
You can download the TMD27713 module datasheet from this link.
TMD27713 Module Pinout
The TMD27713 has five pins as shown below.
TMD27713 Sensor Features
At the beginning of this tutorial, you became familiar with some of the key features of the TMD27713 sensor. Here, we’ll briefly mention some others:
Ambient Light Sensing (ALS):
- Response time: similar to the human eye
- Programmable analog gain
- Programmable interrupt function with an upper and lower threshold
- Up to 16-bit resolution
- Highly sensitive (works even behind colored glass)
- Dynamic range up to 1000000:1
- Calibrated for 100mm detection
- Programmable number of IR pulses
- Programmable Current Sink – No need for limiting resistor
- Programmable interrupt function with an upper and lower threshold
- Small size: 3.94mm × 2.4mm × 1.35mm
Required Materials
*: You can also use any other Arduino board that supports the I2C protocol.
How to Interface the TMD27713 Sensor Module with Arduino
Now, let’s see how we can measure distance using the TMD27713 sensor and Arduino:
Step 1: Wiring
First, connect the TMD27713 module to Arduino as shown in the image below.
Step 2: Code
Copy the following code into the Arduino IDE software and upload it to your board. The code lines include comments explaining their functionality.
#include <Wire.h>
// TMD2771 I2C address is 39(57)
#define Addr 0x39
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial Communication, set baud rate = 9600
Serial.begin(9600);
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x00 | 0xA0); // Select enable register
Wire.write(0x0F); // Set power on, proximity and ALS enabled
Wire.endTransmission();// Stop I2C Transmission
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x01 | 0xA0); // Select ALS time register
Wire.write(0xDB); // Atime = 101 ms
Wire.endTransmission(); // Stop I2C Transmission
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x02 | 0xA0); // Select proximity time register
Wire.write(0xFF); // Ptime = 2.72 ms
Wire.endTransmission();// Stop I2C Transmission
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x03 | 0xA0); // Select Wait time register
Wire.write(0xFF); // Wtime = 2.72 ms
Wire.endTransmission();// Stop I2C Transmission
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x0E | 0xA0); // Select pulse count register
Wire.write(0x04); // Pulse count = 4
Wire.endTransmission();// Stop I2C Transmission
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x0F | 0xA0); // Select control register
// 120 mA LED strength, Proximtiy uses CH1 diode, 1x PGAIN, 1x AGAIN
Wire.write(0x20);
Wire.endTransmission();// Stop I2C Transmission
delay(800);
}
void loop()
{
unsigned int data[6];
Wire.beginTransmission(Addr); // Start I2C Transmission
Wire.write(0x14 | 0xA0); // Select data register
Wire.endTransmission();// Stop I2C Transmission
// Request 6 bytes of data
Wire.requestFrom(Addr, 6);
// Read 6 bytes of data
// c0Data lsb, c0Data msb, c1Data lsb, c1Data msb, proximity lsb, proximity msb
if(Wire.available() == 6)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
data[4] = Wire.read();
data[5] = Wire.read();
}
// Convert the data
int c0Data = (data[1] * 256) + data[0];
int c1Data = (data[3] * 256) + data[2];
double proximity = (data[5] * 256.0) + data[4];
float CPL = (101.0) / 24.0;
float luminance1 = (1 * c0Data - 2 * c1Data) / CPL;
float luminance2 = (0.6 * c0Data - 1.00 * c1Data) / CPL;
float luminance = 0.0;
if((luminance1 > 0) && (luminance1 > luminance2))
{
luminance = luminance1;
}
else if((luminance2 > 0) && (luminance2 > luminance1))
{
luminance = luminance2;
}
// Output data to serial monitor
Serial.print("Ambient Light luminance : ");
Serial.print(luminance);
Serial.println(" lux");
Serial.print("Proximity of the device : ");
Serial.println(proximity);
delay(1000);
}
After uploading the code, select the COM port and board type, then open the Serial Monitor. Make sure the baud rate is set to 9600.
As you can see, the device sends the brightness level and distance to the serial port. By moving an obstacle—such as your hand—closer to or farther from the module, you can observe changes in the distance. The smallest detectable distance is 4 cm—according to the adjustments we made for the TMD27713 module in the code.
What’s Next?
In this tutorial, you learned how to work with the TMD27713 proximity detection module and interface it with Arduino. If you need more information about the registers we configured in the code, you can refer to the datasheet of this sensor. For example, registers 0x04 to 0x0B are used to set the upper and lower threshold values for interrupt generation. By configuring them and using the INT pin of the module, you can create an automatic switch that is activated or deactivated based on changes in light or distance.
Because of its small size, the TMD27713 sensor is used in various projects including:
- Robotics projects: obstacle detection
- Mobile phones: turning off the screen when talking on the phone
- Displays: adjusting the backlight intensity