Sunspot Home
Slug index

Reading an i2c SE95 thermometer using Python, i2cset and i2cget

The SE95 is a 13 bit i2c thermometer

If you load i2ctools this gives you i2cset and i2cread
- also do i2cdetect 0 to see the address

Just my second attempt at Python - but it works!

#!/usr/bin/python
# use i2cset and i2cget to read a SE95 13 bit i2c thermometer
# with all 3 address lines tied to 5 volts the address is 0x4f (decimal 79)
# available decimal addresses are 72 73 74 75 76 77 78 79
# ./SE95_i2ctools_read_once.py 79

import os
import sys

address = sys.argv[1] # get the address argument

# write a 0 to the 0 register
os.popen('i2cset -y 0 ' + address + ' 0x00 0x00 b > /dev/null 2>&1')

SE95_temp = os.popen('i2cget -y 0 '+ address +' 0x00 w').read()

# get the two hex bytes from the received text string (it is like 0xdc12)
SE95_byte_HI = SE95_temp[4:6]
SE95_byte_LO = SE95_temp[2:4]

SE95_byte_HI_dec = int(SE95_byte_HI , 16)
SE95_byte_LO_dec = int(SE95_byte_LO , 16)

sign = 1

# is the temperature negative?
if SE95_byte_HI_dec >= 128:
<tab here>SE95_byte_HI_dec = float(SE95_byte_HI_dec ^ 255) # ^ is XOR
<tab here>SE95_byte_LO_dec = float((SE95_byte_LO_dec ^ 255) + 1)
<tab here>sign = -1

SE95_byte_HI_dec = float(SE95_byte_HI_dec)
SE95_byte_LO_dec = float(SE95_byte_LO_dec)

temp_all = (SE95_byte_HI_dec + SE95_byte_LO_dec/256)

temp_1_dec = round(temp_all, 1)

if temp_1_dec == 0:
<tab here>sign = 1 #or else we see "-1"

temp_1_dec = sign * temp_1_dec

print temp_1_dec

soic

The chip is SOIC surface mount so I used a SOIC to DIL8 adapter (eBay)
If you hold the chip so you can read the lable pin 1 is at bottom left
- note the sloping edge

Sunspot Home
Slug index

This is my earlier Blassic basic version (for a Debian Slug) that I translated to Python for an Openwrt Slug

#!/usr/sbin/blassic
' use i2cset and i2cget to read a SE95 13 bit i2c thermometer
' with all 3 address lines tied to 5 volts the address is 0x4f (decimal 79)
' available decimal addresses are 72 73 74 75 76 77 78 79
'./SE95_i2ctools_read_once.bas 79

address$ = PROGRAMARG$(1)

'write a 0 to the 0 register
SHELL "i2cset -y 0 "+address$+" 0x00 0x00 b > /dev/null 2>&1"

SHELL "i2cget -y 0 "+address$+" 0x00 w > /var/www/ramdisk/lm75_1.txt"
OPEN "/var/www/ramdisk/lm75_1.txt" FOR INPUT AS #1:INPUT #1,temp$:CLOSE #1

PRINT "temp$ = ",temp$
PRINT
'get the two hex bytes from the stored text string
HI$ = RIGHT$(temp$,2) : LO$ = MID$(temp$,3,2)

print "HI$ = ",HI$
print "LO$ = ",LO$

'get the decimal value of the hex numbers
HI_dec = VAL("&h"+ HI$) : LO_dec = VAL("&h"+ LO$)

sign = 1
' if bit 7 of the HI byte is 1 then the temperature is negative
' convert twos complement to decimal (invert all bits and add 1 to the low byte)
IF HI_dec >= 128 THEN HI_dec = HI_dec XOR 255 :LO_dec = (LO_dec XOR 255) +1: sign = -1

temp_all = (HI_dec + LO_dec/256)
temp_1_dec = ROUND(temp_all,1)
IF temp_1_dec = 0 THEN sign = 1
temp_1_dec = sign * temp_1_dec
PRINT temp_1_dec

SYSTEM