Skip to content

ranaweerasupun/tmp102-i2c-driver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tmp102-i2c-driver

A Linux kernel driver for the TI TMP102 I2C temperature sensor on Raspberry Pi. I wrote this because I wanted TMP102 readings to show up in the standard hwmon sysfs interface rather than polling the sensor from userspace directly.

$ cat /sys/class/hwmon/hwmon1/temp1_input
23562

Readings are in millidegrees Celsius — divide by 1000 to get °C. Standard tools like sensors (from lm-sensors) pick it up automatically once the module is loaded.


Hardware

Wiring — Raspberry Pi 40-pin header:

TMP102 Pin Raspberry Pi Pin
VCC Pin 1 — 3.3V
GND Pin 6 — GND
SDA Pin 3 — GPIO 2 / I2C SDA
SCL Pin 5 — GPIO 3 / I2C SCL
ADD0 Pin 6 — GND (address 0x48)

Tying ADD0 to GND sets the I2C address to 0x48. If you tie it to VCC (pin 1) instead, the address becomes 0x49 — just update reg in tmp102-overlay.dts accordingly.


Requirements

  • Raspberry Pi running Raspberry Pi OS Bookworm (64-bit recommended)
  • Kernel headers: sudo apt install raspberrypi-kernel-headers
  • Build tools: sudo apt install build-essential
  • Device tree compiler: sudo apt install device-tree-compiler
  • I2C tools (optional but handy): sudo apt install i2c-tools

Installation

1. Enable I2C

sudo raspi-config
# Interface Options → I2C → Enable

Or manually add dtparam=i2c_arm=on to /boot/firmware/config.txt and reboot.

2. Check the sensor is visible on the bus

sudo i2cdetect -y 1
# You should see 0x48 in the grid

If nothing shows up, double-check the wiring before going any further.

3. Build and install the device tree overlay

make dtbo
make install-overlay

Then register the overlay by appending to /boot/firmware/config.txt:

echo "dtoverlay=tmp102-overlay" | sudo tee -a /boot/firmware/config.txt
sudo reboot

4. Build and load the driver

make
sudo insmod tmp102.ko
dmesg | grep tmp102

If everything is wired correctly, you should see something like:

tmp102 1-0048: TMP102 at 0x48, temperature: 23.562°C

5. Read the temperature

cat /sys/class/hwmon/hwmon*/temp1_input
# e.g. 23562  (= 23.562°C)

Or install lm-sensors and use sensors:

sudo apt install lm-sensors
sensors

Unloading

make uninstall
# or just: sudo rmmod tmp102

Files

File Description
tmp102.c Kernel driver source
tmp102-overlay.dts Device tree overlay source
Makefile Build, install, and clean targets

How It Works

The driver follows the standard Linux I2C driver pattern:

  1. Device tree matching — the "ti,tmp102" compatible string in the overlay is what links the hardware description to this driver. Without the overlay, the kernel never calls probe.
  2. Probetmp102_probe() allocates the private state struct, does a quick temperature read to verify the sensor is actually responding, then registers with the hwmon subsystem.
  3. sysfs attributeSENSOR_DEVICE_ATTR wires up temp1_input. Each read goes through i2c_smbus_read_word_swapped(), which handles the byte-order difference, and I convert the raw 12-bit two's-complement value to millidegrees from there.
  4. Cleanup — everything is allocated with devm_ helpers, so the kernel cleans up automatically on removal without me needing to do anything in the remove callback.

License

The kernel driver (tmp102.c) is licensed under GPL-2.0, as required for Linux kernel modules.
All other files are licensed under the MIT License — see LICENSE.

About

A production-quality Linux kernel module for the TMP102 temperature sensor — I2C protocol, hwmon integration, and device tree support, built with smart home environmental monitoring in mind.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors