I2C communication and temperature measurement with the Atmega8 microcontroller

Copyright of all software and diagrams: Guido Socher
License: GPL

 All parts needed to build this litte thermometer can be ordered from 
 http://shop.tuxgraphics.org/                                          
 They have also other electronics and components for a very good price!


You need a complete installation of the AVR GCC development environment
as described in 
http://linuxfocus.org/English/November2004/article352.shtml

If you want to start immediately and not spend time with a lot of installation
work then I really recommend to use the AVR programming CD from
http://shop.tuxgraphics.org/electronic/microcontroller.html

To compile the software and download it to the Atmega8 use the command:
make load

Building the I2C communication software
---------------------------------------
The shop.tuxgraphics.org  AVR programming CD does not have a regular 
gcc installation as you will anyhow not be able to install things on a
read-only CD. The primary purpose of this CD is to programm the 
microcontroller. However you may want to test the I2C communication 
after you have loaded software to the microcontroller. For this purpose
two programs are included in the "bin" subdirectory:

bin/i2c_rs232_pintest
bin/i2ctemp_linux

With bin/i2c_rs232_pintest you can test (using a voltmeter) that the DTR and
RTS lines of your rs232 port are working.

With bin/i2ctemp_linux you can read temperature valuses form the thermometer.

On the computer to which you intend to connect this termometer you should build
the i2ctemp_linux program and install it (this may or may not be the same computer
where you program the microcontroller):
make i2ctemp_linux
cp i2ctemp_linux /usr/bin

The i2ctemp_linux assumes by default that your thermometer is connected to
COM1 (/dev/ttyS0). You can change this with the option -p. Example:

> i2ctemp_linux -p /dev/ttyUSB0
In :i=19.8
Out:o=19.3

Using a USB to rs232 converter
------------------------------
For laptops which do these days not have a rs232 interface you can simply use
USB to rs232 adapter. I use e.g a no-name adapter which contains a Prolific 2303 chip.
It looks like this in the /proc/bus/usb/devices file:
Vendor=067b ProdID=2303 Rev= 2.02

The kernel module for this chip is called pl2303.

Makeing data available over the web
-----------------------------------
Never run i2ctemp_linux directly from the webserver. Instead add a contab entry
which runs a script to generate an appropriate webpage e.g every 15 minutes:

The script:
#!/bin/sh
webpagefile=/home/httpd/html/temp.html
echo "<h2>Local temperatures</h2><pre>" > $webpagefile
i2ctemp_linux | sed -e 's/i=/inside  /;s/o=/outside /' >> $webpagefile
echo "</pre>" >> $webpagefile

Run the above script e.g from a crontab entry which looks like this
(load a file containing this line with the command crontab):
1,15,30,45 * * * * /the/above/listed/scriptfile

Calibrating the thermometer
---------------------------
First of all you should note that this thermometer is already in
unclibrate state more accurate than most household thermometers. 
If you get the right parts (e.g from shop.tuxgraphics.org) then you will
already have an accuracy of +/- 0.5'C without any calibration.

If you want to calibrate it then edit the file ntc.h
Instructions on what to do are included.

The recommended oprating range is: -30'C to + 45'C for the outdoor sensor
and +10 to +45'C  for the indoor sensor. The indoor sensor is  more
accurate than the outdoor because the ADC uses a smaller reference voltage.
This limits the 0..1023 digital values of the ADC to a smaller range and
gives thus higher accuracy (the disdvantage is that the indoor sensor can
then not measure below 9'C, but it should not be soooo cold in your house ;-)

Revision history
----------------
2005-01-23: version 0.9 (first public version)
2005-02-07: version 1.0 updated for avr-libc-1.2.1 (avr/twi.h removed)

