DS1820 temperature sensor – Python interface

Share it!

DS1820 one wire temperature sensor – Python interface

Please see the previous two posts about the progress of this project. This post continues from where we left off.

Summary of Changes

  • The support for the COM port and the LPT port have been combined into a single file.
  • Python interface is available now.
  • DEBUG mode with compiler switch.
  • Implemented ALARM SEARCH command

Source Files

There are two source files: DOWNLOAD them by right clicking on ds1820.c and ds1820wrapper.c.
Use the following compiler switches when compiling them:

O Optimize – mandatory
DEBUG prints more detailed messages
COM if you want to use serial port
LPT if you want to use line printer port

Note: you must use one of COM or LPT switch.

For example, to use the line printer port with a debug option, compile the downloaded files as follows:

gcc -c -fpic -I/usr/include/python2.7 -O -D LPT -D DEBUG ds1820.c ds1820wrapper.c

To create the loadable python module, type:

gcc -shared ds1820.o ds1820wrapper.o -o ds1820module.so

Now you can import this module into a python interpreter like any other python module, with

import ds1820

Note that you have to run python as root so you can access the IO ports.

Python Functions

Here is the list of python functions.

Base Functions
init Always call this function first.
detect Optional, to see if any devices are connected
ROM Functions
SkipROM Single Device Only
readROM Single Device Only – Returns the ROM code
searchROM Returns a list of ROM codes in a python dictionary
matchROM Takes ROM code as a parameter to select a device
searchALARM Returns a dictionary of devices in alarm condition
RAM Functions
convertT Convert Temperature
readPowerSupply Read Power Supply (0 – parasite, 0xFF – external)
readScratchpad Read Scratchpad – 9 bytes are read.
writeScratchpad Write TH and TL (High/Low limits)
copyScratchpad Copy TH and TL to EEPROM
recallE2 Recall TH and TL into Scratchpad memory

Sample Test Runs

All communication to the DS1820 device start from the master (in this case the computer). The master sends a reset(), followed by one ROM command, and one RAM command. An exception to this sequence is with the ROM/ALARM search commands, and the readROM command. No RAM command is sent after these commands.

NOTE: The reset() command is sent automatically from the wrapper functions, so we don’t have to worry about it.

Check the presence of DS1820

Here is a simple python script to check the presence of DS1820 device.

import ds1820
ds1820.init()
ds1820.detect()
>>> import ds1820
>>> ds1820.init()
True
# returns true when DS1820 is connected
>>> ds1820.detect()
True
# After I removed the device, it returns False
>>> ds1820.detect()
False
>>>

Read ROM code – single device only

Sample script to read ROM code.

import ds1820
ds1820.init()
(romcode, CRC) = ds1820.readROM()
print romcode, CRC

Output from the script above:

Sending 0x33 - Read ROM
writeByte: 33
CRC: 00 (Should be zero)
07 00 08 02 3b ad c3 10
Returned CRC: 00
[16, 195, 173, 59, 2, 8, 0, 7] 0

Note that with the DEBUG option, the detailed info is printed. Without the DEBUG option, the last line would be printed only from the python script.

Search ROM code – one or more devices

Sample script to search ROM codes.

import ds1820
ds1820.init()
rslt = ds1820.searchROM()
for i in rslt.keys():
    print i, rslt[i]

Output from the script above:

Searching for devices/alarms...
writeByte: f0
.writeByte: f0

Found 2 devices.
07 00 08 02 3b ad c3 10 CRC: 00
ba 00 08 02 3b d7 57 10 CRC: 00

TS2 [[16, 87, 215, 59, 2, 8, 0, 186], 0]
TS1 [[16, 195, 173, 59, 2, 8, 0, 7], 0]

Note that devices are named TSn, where n is an integer number starting with 1.

Match ROM code – one or more devices

Sample script to select one of the devices with Match ROM code and send convert() command to it.

import ds1820
ds1820.init()
ds1820.matchROM([16, 195, 173, 59, 2, 8, 0, 7])
ds1820.convertT()

Output from the script above:

Sending 0x55 - Match ROM
writeByte: 55
writeByte: 10
writeByte: c3
writeByte: ad
writeByte: 3b
writeByte: 02
writeByte: 08
writeByte: 00
writeByte: 07
writeByte: 44     # Convert command

Skip ROM code – single device only

If a single device is used, the fastest ROM command to use is the Skip ROM command(0xCC). The script below uses Skip ROM command to use the Scratchpad memory. The first two bytes of the scratchpad memory contains the temperature in Centigrade multiplied by two.

Note that if you read the scratchpad memory without sending convert() command after power-up, the device will send back 85 degree Centigrade. This is a phantom value.

import ds1820
ds1820.init()
ds1820.skipROM()
rslt = ds1820.readScratchpad()
print rslt

Output from the script above:

writeByte: cc
Sending 0xbe - Read ScratchPad
writeByte: be
CRC: 00
66 10 0c ff ff 0a 15 00 28
Temperature:  20.0C  68.0F
([40, 0, 21, 10, 255, 255, 12, 16, 102], 0)

Next Steps

Ability to measure temperature from a computer opens up all kinds of possibilities for future projects.

Also there are number of other one wire devices that communicate with the same protocol. Those devices open up even more possibilities.

Share it!
This entry was posted in Projects and tagged , , . Bookmark the permalink.

2 Responses to DS1820 temperature sensor – Python interface

  1. Jon says:

    Hey, not sure if you’re still doing anything with this, but I’m trying to use your python module, and I’m having a bit of trouble getting python to find it. I’ve created the ds1820module.so file, but where do I put it, and how do I tell python about it?

    Sorry, new at manually manipulating python modules.

  2. Jon says:

    Alright, forget I said anything. Modified the ds1820module.so file to be called ds1820.so and put it in /usr/lib/python2.7/site-packages, and now I’m good

    Thanks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

[keycaptcha]