MicroPython Driver for ADT7410 Sensor

ADT7410 Driver Code for micro:bit

Download as zip file

'''
ADT7410 temperature sensor
MicroPython driver for BBC micro:bit

This driver concentrates on returning
temperature measurements.
No watchdog functions are provided.

REFERENCES:
ADT7410 Datasheet
https://www.fredscave.com/drivers/024-adt7410.html

AUTHOR: fredscave.com
DATE  : 2025/04
VERSION : 1.00
'''

from microbit import i2c, sleep
from micropython import const

I2C_ADDR    = const(0x48)
CMD_RESET   = const(0x2F)

# Registers
REG_TEMP    = const(0x00)
REG_CONFIG  = const(0x03)

# Modes
CONTINUOUS    = const(0x00)
ONE_SHOT      = const(0x20)
ONE_SPS       = const(0x40)
SHUT_DOWN     = const(0x60)

# Resolution
RES_HI = const(0x80)
RES_LO = const(0x00)

# Centigrade to Fahrenheit
CtoF = lambda C, d=1: round((C * 9/5) +32, d)

class ADT7410():
    # Constructor sets up I2C address, resolution and mode.
    def __init__(self, Addr = I2C_ADDR,
                 Resolution = RES_LO, Mode = CONTINUOUS):
        self.buf = bytearray(2)
        self.Addr = Addr
        self._Reset()
        if Resolution == RES_HI:
            self.Resolution = RES_HI
        else:
            self.Resolution = RES_LO
        if Mode not in (CONTINUOUS, ONE_SHOT, ONE_SPS):
            self.Mode = CONTINUOUS
        else:
            self.Mode = Mode
        self._SetMode()
        self.UserShutdown = False

    # User method to call to return latest temperature.
    # Resolution and sampling mode are as set by the
    # constructor.
    def Read(self):
        if self.Mode == ONE_SHOT and not self.UserShutdown:
            self.WakeUp()
        i2c.write(self.Addr, bytes([REG_TEMP]))
        self.buf = i2c.read(self.Addr, 2)
        return self._Temperature()

    # Forces the sensor into shutdown mode.
    # All chip internals are switched off.
    # The I2C interface is still active.
    # The last temperature conversion is still available.
    def ShutDown(self):
        i2c.write(self.Addr, bytes([REG_CONFIG, SHUT_DOWN]))
        self.UserShutdown = True

    # Wake upt he sensor from shutdown mode.
    # Temperature conversions commence based on
    # the mode and resolution set by the constructor.
    def WakeUp(self):
        self._SetMode()
        self.UserShutdown = False

    # Soft reset of the chip. All default values
    # are loaded which override any settings that
    # that may have been made in the constructor.
    def _Reset(self):
        i2c.write(I2C_ADDR, bytes([CMD_RESET]))
        sleep(300)

    # Writes a byte to the Configuration register
    # to set the resolution and mode as passed to
    # the constructor.
    def _SetMode(self):
        Config = self.Mode
        if self.Resolution == RES_HI:
            Config = Config | RES_HI
        i2c.write(self.Addr, bytes([REG_CONFIG, Config]))
        sleep(250)

    # Converts the 13-bit or 16-bit two's complement
    # binary value to the decimal equivalent.
    def _Temperature(self):
        positive = (self.buf[0] >> 7) == 0
        if self.Resolution == RES_LO:
            raw = self.buf[0]*256 + self.buf[1] >> 3
            if positive:
               return raw/16
            else:
               return (raw - 8192) / 16
        else:
            raw = self.buf[0]*256 + self.buf[1]
            if positive:
                return raw/128
            else:
                return (raw - 65536) / 128

          

Using Multiple ADT7410 Sensors

A maximum of 4 x ADT7410 sensors can share the same I2C bus. This is possible by tying the A0 and A1 pins in different combinations to VIN and GND.

This example shows just how easy to do this. Two ADT7410 breakout boards are connected to the micro:bit's I2C bus and temperatures are read simultaneously from both sensors.

Hooking up the sensors

  1. Connect VIN of each sensor to micro:bit's 3.3V pin.
  2. Connect GND of each sensor to micro:bit's GND pin.
  3. Connect SCL pin of each sensor to micro:bit's pin19.
  4. Connect SDA pin of each sensor to micro:bit's pin20.
  5. On one (only) of the sensors, connect a jumper between A0 and GND. This will change the default I2C address to 0x49.
  6. Ensure that the driver file fc_adt7410.py  has been copied to the micro:bit's filesystem.

The Code:


# Test 2 x ADT7410 temperature sensors.
# Sensor1 has the default I2C address of 0x48.

# Sensor2 has jumper between A0 and VIN pins.
# This changes the I2c Address to 0x49.

from fc_adt7410 import *

# Create the two sensor objects.
adt7410_1 = ADT7410()
adt7410_2 = ADT7410(Addr=0x49)

# Print out the configuration of the sensors.
print('Configuration for both sensors:')
print('Resolution is 13-bits')
print("Mode is 'Continuous'")
print('Interval between samples is 5 seconds\n')

# Take six readings simultaneously from each of
# the sensors with a sample interval of 5 sec.
# Output the results, rounded to two decimals.
for _ in range(0, 6):
    T1 = adt7410_1.Read()
    T2 = adt7410_2.Read()
    print('T1=', round(T1, 2),
          '     T2=', round(T2, 2))
    sleep(5000)
print()
          

Example Output:

Configuration for both sensors:
Resolution is 13-bits
Mode is 'Continuous'
Interval between samples is 5 seconds

T1= 25.19      T2= 24.69
T1= 25.19      T2= 24.69
T1= 25.25      T2= 24.69
T1= 25.25      T2= 24.75
T1= 25.25      T2= 24.75
T1= 25.25      T2= 24.69
          

Wrapping Up

As can be seen from the output that was obtained during the development of this example, the two sensors produced temperature readings that matched fairly closely.

2 x ADT7410 temperature sensor breakout boards connected to the micro:bit
2 x ADT7410 sensors and the micro:bit

Enjoy!