Temperature & Humidity Data Logger Project

Have questions about a SparkFun product or board? This is the place to be.

Moderators: phalanx, TS-Tim

Post Reply
Oldspring
Posts: 7
Joined: Fri Apr 04, 2008 7:00 pm

Temperature & Humidity Data Logger Project

Post by Oldspring » Mon Jun 30, 2008 3:10 pm

Hello all,

This project uses BasicStamp 2P to interface with a DS1307, SHT15, JP Module and 16x4 LCD display. Code used is standard PBasic 2.5, it should work on most BasicStamp chips.

The project is a data logger demonstration program. It can record temperature and humidity data to MMC/SD card with date and time. Press the temperature unit select button switch to select Celsius or Fahrenheit. The data can be processed to the virtual XY chart or Polar Chart through the website ( www.jianpingusa.com/jpchart ).

Components:
1) Basic Stamp 2P
Manufactory: www.parallax.com
Datasheet: http://www.parallax.com/tabid/440/Default.aspx

2) DS1307
Manufactory: www.maxim-ic.com ,
Datasheet: http://datasheets.maxim-ic.com/en/ds/DS1307.pdf

3) SHT15
Manufactory: www.sensirion.com
Datasheet: http://www.sensirion.com/en/pdf/product ... HT7x_E.pdf

4) JP Module
Manufactory: www.jianpingusa.com
Datasheet: http://www.jianpingusa.com/datasheet/JP ... uction.pdf

5) 16x4 LCD Display (CM1644 series)
Manufactory: http://www.femacorp.com/
Datasheet: http://www.femacorp.com/Products/LCD/Ch ... 0Index.htm

Data processed website:
www.jianpingusa.com/jpchart

Attached files: ( 5 files )
1) code

Code: Select all


'======================================================
'   File...... Temperature + Humidity Data Logger Demo
'   Purpose... SHT15 + DS1307 + JP Module + 16x4 LCD
'   Author.... OldSpring
'   Email..... OldSprings@yahoo.com
'   Started...
'   Updated... 18 June 2008
'
'   {$STAMP BS2p}
'   {$PBASIC 2.5}
'
'=======================================================

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T4800       CON     188
    T9600       CON     84
    T19K2       CON     32
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T4800       CON     500
    T9600       CON     240
    T19K2       CON     110
  #CASE BS2PX
    T2400       CON     1646
    T4800       CON     813
    T9600       CON     396
    T19K2       CON     188
#ENDSELECT

'-------------------------------------------------------
'                 I/O Definitions
'-------------------------------------------------------

SDA             CON     8       'Pin8 = SDA, Pin9 = SCL FOR DS1307
BtnWrk          PIN     10      'Timer controller pin
ShtDat          CON     15      'SHT Data Pin
ShtClk          CON     14      'SHT Clock Pin
C_F             PIN     0       'Select C or F button Pin
E               CON     1       'LCD Enable Pin
SOUT            PIN     13
StatePin        PIN     12
ResetPin        PIN     11
'-------------------------------------------------------
'                    Constants
'-------------------------------------------------------

ShtTemp         CON     %00011  ' read temperature
ShtHumi         CON     %00101  ' read humidity
ShtStatW        CON     %00110  ' status register write
ShtStatR        CON     %00111  ' status register read
ShtReset        CON     %11110  ' reset
Ack             CON     0
NoAck           CON     1
DS1307          CON     $D0     ' DS1307 address

'-------------------------------------------------------
'                    Variables
'-------------------------------------------------------
BT              VAR     Bit
DS              VAR     Nib
SHT             VAR     Nib
Rec             VAR     Nib

sec             VAR     Byte
minute          VAR     Byte
hr              VAR     Byte
day             VAR     Byte
date            VAR     Byte
month           VAR     Byte
year            VAR     Byte

ioByte          VAR     Byte    ' data input/output from SHT1x
ackBit          VAR     Bit     ' ack/nak from/to SHT1x
T_Delay         VAR     Byte    ' timeout delay timer
T_Out           VAR     Bit     ' timeout status
soT             VAR     Word    ' temp counts from SHT1x
tC              VAR     Word    ' temp for C
tF              VAR     Word    ' temp for F
CF              VAR     Bit
tC_tF           VAR     Bit
soRH            VAR     Word    ' humidity counts from SHT1x
rhLin           VAR     Word    ' humidity; linearized
rhTrue          VAR     Word    ' humidity; temp compensated
status          VAR     Byte    ' SHT1x status byte
'Baud            CON     T2400   ' 2400,8,N,1
Baud            CON     T19K2   ' 19200,8,N,1

'--------------------------------------------------------
'                  LCD command
'--------------------------------------------------------
LcdCls          CON     $01     ' clear the LCD
LcdHome         CON     $02     ' move cursor to home
LcdLine1        CON     $80     ' Lcd address for line 1
LcdLine2        CON     $C0     ' Lcd address for line 2
LcdLine3        CON     $90     ' Lcd address for line 3
LcdLine4        CON     $D0     ' Lcd address for line 4

Init_LCD:
  PAUSE 1000                    ' LCD self init
  LCDCMD E, %00110000           ' wakeup
  PAUSE 10
  LCDCMD E, %00100000           ' set data for 4 bits
  LCDCMD E, %00101000           ' set 2(4) line mode with 5x8 font
  LCDCMD E, %00001100           ' turn cursor off
  LCDCMD E, %00000110           ' auto increment cursor

'------------- First Screen ---------------
  LCDCMD E, LcdCls
  LCDOUT E, LcdLine1, ["  Temperature   "]
  LCDOUT E, LcdLine2, ["  and Humidity  "]
  LCDOUT E, LcdLine3, ["  Data Logger   "]
  LCDOUT E, LcdLine4, ["Made by OldSprig"]

' ------------------------------------------------------------------------------
'                         Initialization
' ------------------------------------------------------------------------------

  OUTPUT ResetPin
  ResetPin  = 0
  PAUSE 1000
  ResetPin  = 1
  PAUSE 1000

Initialize:
  GOSUB SHT_Connection_Reset    ' reset device connection
  I2COUT SDA,DS1307,7,[$10]     ' Enable OSC @ 1Hz

'---------------------------------------------------
'          Set Date and Time to DS1307
'---------------------------------------------------

'  I2COUT SDA,DS1307,6,[$08]                     ' Year    0 - 99
'  I2COUT SDA,DS1307,5,[$06]                     ' Month   1 - 12
'  I2COUT SDA,DS1307,4,[$24]                     ' Date    1 - 31
'  I2COUT SDA,DS1307,3,[$02]                     ' Day     1 - 7
'  I2COUT SDA,DS1307,2,[$12]                     ' Hours   0 - 23
'  I2COUT SDA,DS1307,1,[$30]                     ' Minutes 0 - 59
'  I2COUT SDA,DS1307,0,[$00]                     ' Seconds 0 - 59

  Rec = 0
  BT = 0
  DS = 0
  SHT  = 0
  CF = 0

'-----------------------------------------------------------
'             Format MMC/SD Card with FAT16
'Warning: Format will erase ALL data on your MMC/SD card!!!!
'-----------------------------------------------------------

'Formatting:
'  LCDCMD E, LcdCls
'  LCDOUT E, LcdLine1, ["  Formatting... "]
'  SEROUT SOUT, Baud, ["MF!"]                  'Format MMC/SD Card
'  PAUSE 50
'  SEROUT SOUT, Baud, ["OldSpring!!"]           'Volume label
'  PAUSE 1000

'Waiting:
'  IF StatePin = 1 THEN
'  LCDOUT E, LcdLine1, ["  Finished!     "]
'  ELSE
'  PAUSE 1000
'  GOTO  Waiting
'  ENDIF

Init_JP_Module:
  OUTPUT ResetPin
  ResetPin  = 0
  PAUSE 1000
  ResetPin  = 1
  PAUSE 1000

  SEROUT SOUT, Baud, ["MA!"]                    'Open data file
  PAUSE 50
  SEROUT SOUT, Baud, ["TestFile.CSV!!"]         'File Name
  PAUSE 1000

  IF StatePin = 0 THEN                          'If file not find, create a new file
    ResetPin  = 0                               'Reset JP Module
    PAUSE 500
    ResetPin  = 1
    PAUSE 500
    SEROUT SOUT, Baud, ["MC!"]                  'Create a new file
    PAUSE 50
    SEROUT SOUT, Baud, ["TestFile.CSV!!"]       'File Name
    PAUSE 1000
    SEROUT SOUT, Baud, ["Date,Time,Temperature,Humidity", CR,"!!"]  'Data title
    PAUSE 1000
    GOSUB Init_JP_Module
  ENDIF

  LCDCMD E, LcdCls
  LCDOUT E, LcdLine1, ["Date:"]
  LCDOUT E, LcdLine2, ["Time:"]
  LCDOUT E, LcdLine3, ["Temp:"]
  LCDOUT E, LcdLine4, ["Humi:"]
  GOSUB ShowDT
  GOSUB ShowTH
'--------------------------------------------------------
'                     Main program
'--------------------------------------------------------
Main:

 IF (C_F = 1) AND (tC_tF = 0) THEN
  tC_tF = 1
 ENDIF

 IF (C_F = 0) AND (tC_tF = 1) THEN
    IF CF = 0 THEN
       CF = 1
    ELSE
       CF = 0
    ENDIF
    GOSUB  ShowTH
    tC_tF = 0
 ENDIF

 IF (BtnWrk = 0) AND (BT = 1) THEN
  GOSUB ShowDT                           'reflash time and date per second
  IF SHT = 5 THEN GOSUB ShowTH           'reflash Temp and humi per 5 seconds
  IF Rec = 10 THEN GOSUB DataRec         'Record Data per 10 seconds
  BT = 0
  DS = DS + 1
  SHT = SHT + 1
  Rec = Rec + 1
 ENDIF

 IF (BtnWrk = 1) AND (BT = 0) THEN
  BT = 1
 ENDIF

 GOTO Main

 END

' ----------------------------------------------------------
'                       Subroutines
'-----------------------------------------------------------
ShowDT:
' Get Time and Date from the DS1307
  I2CIN SDA,DS1307,0,[sec,minute,hr,day,date,month,year]
  LCDOUT E, LcdLine1 + 7, [HEX2 month, "/", HEX2 date, "/", HEX2 year]
  LCDOUT E, LcdLine2 + 7, [HEX2 hr, ":", HEX2 minute, ":", HEX2 sec ]
  DS = 0
  RETURN

ShowTH:
  GOSUB SHT_Measure_Temp
  GOSUB SHT_Measure_Humi

  IF CF = 1 THEN
    LCDOUT E, LcdLine3 + 7, [DEC (tC / 10), ".", DEC2 tC, " ", 223, "C"]
  ELSE
    LCDOUT E, LcdLine3 + 7, [DEC (tF / 10), ".", DEC2 tF, " ", 223, "F"]
  ENDIF

  LCDOUT E, LcdLine4 + 7, [DEC (rhTrue / 10), ".", DEC2 rhTrue," %RH"]
  SHT = 0
RETURN

DataRec:
  IF StatePin = 1 THEN
    SEROUT SOUT, Baud, [HEX2 month, "/", HEX2 date, "/", HEX2 year ,","]
    SEROUT SOUT, Baud, [HEX2 hr, ":", HEX2 minute, ":", HEX2 sec ,","]
    SEROUT SOUT, Baud, [DEC (tF / 10), ".", DEC2 tF ,","]
    SEROUT SOUT, Baud, [DEC (rhTrue / 10), ".", DEC2 rhTrue,CR]
    SEROUT 13, Baud, ["!!"] 'write data to SD/MMC Card
  ELSE
    LCDCMD E, LcdCls
    LCDOUT E, LcdLine1, [" Check SD Card  "]   'Please check mmc/sd card first
    LCDOUT E, LcdLine2, [" please...      "]
    LCDOUT E, LcdLine3, [" Waiting for sys"]   'waiting for system reopen file if card ok
    LCDOUT E, LcdLine4, [" reopen file...."]
    GOSUB  Init_JP_Module
  ENDIF
  Rec = 0
  RETURN


SHT_Connection_Reset:
  SHIFTOUT ShtDat, ShtClk, LSBFIRST, [$FFF\9]
  RETURN

SHT_Start:
  INPUT ShtDat                ' generates SHT start sequence
  LOW ShtClk                  '          _____         _____
  HIGH ShtClk                 ' ShtDat        |_______|
  LOW ShtDat                  '              ___     ___
  LOW ShtClk                  ' Clock    ___|   |___|   |___
  HIGH ShtClk
  INPUT ShtDat
  LOW ShtClk
  RETURN

SHT_Measure_Temp:
  GOSUB SHT_Start             ' start device
  ioByte = ShtTemp            ' temperature command
  GOSUB SHT_Write_Byte        ' send command
  GOSUB SHT_Wait
  ackBit = Ack                ' Ack = 1
  GOSUB SHT_Read_Byte         ' get MSB
  soT.HIGHBYTE = ioByte
  ackBit = NoAck              ' NoAck = 0
  GOSUB SHT_Read_Byte         ' get LSB
  soT.LOWBYTE = ioByte
  tC = soT / 10 - 400         ' convert to tenths C
  tF = soT ** 11796 - 400     ' convert to tenths F
  RETURN

SHT_Measure_Humi:
  GOSUB SHT_Start             ' start device
  ioByte = ShtHumi            ' humidity command
  GOSUB SHT_Write_Byte        ' send command
  GOSUB SHT_Wait
  ackBit = Ack                ' Ack = 1
  GOSUB SHT_Read_Byte         ' get MSB
  soRH.HIGHBYTE = ioByte
  ackBit = NoAck              ' NoAck = 0
  GOSUB SHT_Read_Byte         ' get LSB
  soRH.LOWBYTE = ioByte

  rhLin = (soRH ** 26542)
  rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100)
  rhLin = rhLin - 40
  rhTrue = ((tC / 10 - 25) * (soRH ** 524 + 1) + (rhLin * 10)) + 5 / 10
  RETURN

SHT_Write_Status:
  GOSUB SHT_Start             ' start device
  ioByte = ShtStatW           ' reg command
  GOSUB SHT_Write_Byte        ' send command
  ioByte = status
  GOSUB SHT_Write_Byte
  RETURN

SHT_Read_Status:
  GOSUB SHT_Start
  ioByte = ShtStatW           ' reg command
  GOSUB SHT_Read_Byte         ' send command
  ackBit = NoAck
  GOSUB SHT_Read_Byte
  RETURN

SHT_Write_Byte:
  SHIFTOUT ShtDat, ShtClk, MSBFIRST, [ioByte]   ' send data
  SHIFTIN  ShtDat, ShtClk, LSBPRE, [ackBit\1]   ' get ack bit
  RETURN

SHT_Read_Byte:
  SHIFTIN  ShtDat, ShtClk, MSBPRE, [ioByte]     ' get data
  SHIFTOUT ShtDat, ShtClk, LSBFIRST, [ackBit\1] ' send ack bit
  INPUT ShtDat                                  ' data pin input
  RETURN

SHT_Wait:
  INPUT ShtDat                                 ' data pin input
  FOR T_Delay = 1 TO 250
    T_Out = INS.LOWBIT(ShtDat)                 ' Check bit
    IF (T_Out = 0) THEN EXIT
    PAUSE 5
  NEXT
  RETURN

SHT_Soft_Reset:
  GOSUB SHT_Connection_Reset                    ' reset the connection
  ioByte = ShtReset                             ' reset command
  ackBit = NoAck                                ' NoAck = 0
  GOSUB SHT_Write_Byte                          ' send it
  PAUSE 20
  RETURN
2) schematic
Image

3) project photo
Image

4) XY Chart
Image

5) Polar Chart
Image

6) Video Link: http://www.youtube.com/watch?v=CY6NPmKLrRc

Author of this project: OldSpring
Thanks to Sparkfun team and all forum users.

Post Reply