Temperature and humidity sensor (RS232)

From uCtrl
This page is currently a draft. Please check back later for additional changes.
Temperature and humidity sensor

Information

TBD

I/O

Inputs

  • PC0 Temperature sensor (analog)
  • PC1 Humidity sensor (analog)

Calculations

Temperature
Humidity

Outputs

  • PB0 Life signal
  • PB1 Link activity

Serial communication

  • PD0 Rx
  • PD1 Tx

D-sub 15-pin

  1. 13.8V
  2. 0V

Communication

Interfacing is done with RS-232, using MAX232 or MAX202.

Serial settings

Baud
38400
Data bits
8
Parity
None
Stop bits
1

Gallery

Parts used

Qty. Part
1 AVR ATmega8-16PU, DIL-28, 16 MHz, 23 I/Os
5 Capacitor, aluminium electrolytic, 1 µF, 50V
1 Capacitor, aluminium electrolytic, 10 µF, 25V
2 Capacitor, aluminium electrolytic, 47 µF, 25V
1 Capacitor, ceramic, 1 nF, 100V
3 Capacitor, metallized polyester foil, 0.01 µF
1 D-Sub soldering cups, 15 pin male
1 D-Sub soldering cups, 9 pin female
1 DIL socket, 16-pin, 7.62mm
1 DIL socket, 28-pin, 7.62mm
1 Enclosure, plastic (1591 FL), 120x65x40mm, flange
1 Fuse 5x20 mm, 200 mA, fast-acting
1 Fuse holder, open, PCB, 5x20mm
1 Fuse holder, open, PCB, Protective cover
1 Heatsink, 27.3K/W, 19mm, attachable, TO220
1 Humidity sensor, HIH 4000, 0 - 100 %, +4 to +5.8 V
1 LED 5mm clear, Yellow, 2.0V, 20mA, 250mcd, 6°
1 LED 5mm coloured clear, Green, 2.1V, 20mA, 30mcd, 10°
2 LED holder 5mm, RTC51, black plastic
64 cm2 PCB, stripboard, 100x160mm, 160cm2
1 Power jack, panel, 2.1mm, plastic housing
2 Resistor, carbon film, 0.25W, 330 Ω, 5%
2 Resistor, carbon film, 0.25W, 4.7 kΩ, 5%
1 RS232 interface, MAX232CPE, dual
25 Straight pin header, female, Single row, 2.54mm
25 Straight pin header, male, Single row, 2.54mm
1 Temperature sensor LM35DZ, 0 to +100 °C, +-0,6 °C
1 Transistor, NPN, 100 mA, 45V, 0.5W, BC547B
1 Voltage regulator +5V, 2 A, L78S05CV

Source code

'--------------------------------------------------------------
'                         Thomas Jensen
'--------------------------------------------------------------
'  file: AVR_OMU_v.3.0
'  date: 05/11/2011
'  prot: 2.10
'  sn# : 131
'--------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 8000000
$baud = 38400
Config Portb.0 = Output
Config Portb.1 = Output
Config Watchdog = 128

$version 3 , 0 , 2

'inn
'PC0: Inn 1 Temp
'PC1: Inn 2 Humid

'ut
'PB0: Lifesignal
'PB1: Link activity

'serial
'PD0: Rx
'PD1: Tx

Dim Send As String * 30 , Stored_id As Eram Byte
Dim Serialcharwaiting As Byte , Serialchar As Byte
Dim Comminput As String * 9 , Com_value As Word
Dim Com_com As String * 1 , Com_nr As String * 1
Dim Led As Byte , Com_stat As String * 4 , Status As Byte
Dim Value As Word , Values As String * 4 , Id As Byte , Ids As String * 2

Dim Crc As Byte
Dim Verinfo As String * 20

Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc

Const Min_id = 32
Const Max_id = 125
Const Pwm_max = 255
Const Out_max = 0
Const Stat_max = 7

Led_life Alias Portb.0
Led_act Alias Portb.1

If Stored_id >= Min_id And Stored_id <= Max_id Then Id = Stored_id Else Id = Min_id

Ids = Hex(id)                                               'module id number
Const Status_serial = "131"                                 'serial number
Const Status_name = "OMU2"                                  'unit name
Const Status_verboot = "1.0.0"                              'status version bootloader
Const Status_verprot = "2.1.1"                              'status version protocol
Const Status_dio = "0000"                                   'digital inputs, outputs
Const Status_ai = "020A"                                    'analog inputs, bits
Const Status_ao = "0000"                                    'analog outputs, bits

Start Watchdog                                              'startup parameters
Set Status.0
If Id = Min_id Then Set Status.1

Main:
Serialcharwaiting = Ischarwaiting()

If Serialcharwaiting = 1 Then                               'check if serial received
   Serialchar = Inkey()
   If Serialchar = Id Or Serialchar = 126 Then              'look for address or broadcast
      Led = 203
      Goto Set_value
      End If
   End If

If Led > 0 Then Decr Led                                    'activity LED timer
If Led = 200 Then Led_act = 1
If Led = 0 Then Led_act = 0

If Status = 0 Then                                          'life led & statusbyte set
   Led_life = 1
   Else
   Led_life = 0
   Led_act = 1
   End If

Reset Watchdog
Waitus 50
Goto Main
End

Set_value:                                                  'serial receive
Input Comminput Noecho                                      'read serialport

Com_com = Mid(comminput , 2 , 1)                            'command check
Com_nr = Mid(comminput , 4 , 1)                             'output nr check
Com_stat = Mid(comminput , 6 , 4)                           'output full check
Com_value = Hexval(com_stat)

If Com_com = "o" Then                                       'output
Select Case Com_nr

Case "0"                                                    'set digital output status
   Value = 0                                                'get digital output status
   Values = Hex(value)
   'Values = Format(values , "0000")
   Send = Ids + ",o,0:" + Values
   Gosub Serialsend
   'Goto Main

End Select
Goto Main
End If

If Com_com = "i" Then                                       'input
Select Case Com_nr

Case "0"                                                    'get digital input status
   Value = 0
   Values = Hex(value)
   'Values = Format(values , "0000")
   Send = Ids + ",i,0:" + Values
   Gosub Serialsend
   'Goto Main

Case "1"                                                    'analog input 1
   Value = Getadc(0)
   Values = Hex(value)
   'Values = Format(values , "0000")
   Send = Ids + ",i,1:" + Values
   Gosub Serialsend
   'Goto Main

Case "2"                                                    'analog input 2
   Value = Getadc(1)
   Values = Hex(value)
   'Values = Format(values , "0000")
   Send = Ids + ",i,2:" + Values
   Gosub Serialsend
   'Goto Main

End Select
Goto Main
End If

If Com_com = "s" Then                                       'status
Select Case Com_nr

Case "0"                                                    'status byte
   If Com_stat <> "" Then
      If Com_value > Stat_max Then Com_value = Stat_max     'max binary value
      If Com_value.0 = 1 Then Reset Status.0                'bootflag
      If Com_value.1 = 1 Then Reset Status.1                'default address
      If Com_value.2 = 1 Then Toggle Status.2               'manual fail
      End If
   Values = Hex(status)
   'Values = Format(values , "0000")
   Send = Ids + ",s,0:" + Values
   Gosub Serialsend

Case "1"                                                    'serial number
   Send = Ids + ",s,1:" + Status_serial
   Gosub Serialsend
Case "2"                                                    'unit name
   Send = Ids + ",s,2:" + Status_name
   Gosub Serialsend
Case "3"                                                    'firmware version
   Verinfo = Version(2)
   Send = Ids + ",s,3:" + Verinfo
   Gosub Serialsend
Case "4"                                                    'compiled date
   Verinfo = Version()
   Send = Ids + ",s,4:" + Verinfo
   Gosub Serialsend
Case "5"                                                    'bootloader version
   Send = Ids + ",s,5:" + Status_verboot
   Gosub Serialsend
Case "6"                                                    'protocol version
   Send = Ids + ",s,6:" + Status_verprot
   Gosub Serialsend
Case "7"                                                    'digital I/Os
   Send = Ids + ",s,7:" + Status_dio
   Gosub Serialsend
Case "8"                                                    'analog inputs & bits
   Send = Ids + ",s,8:" + Status_ai
   Gosub Serialsend
Case "9"                                                    'analog outputs & bits
   Send = Ids + ",s,9:" + Status_ao
   Gosub Serialsend

End Select
Goto Main
End If

If Com_com = "u" Then                                       'setup
Select Case Com_nr

Case "0"                                                    'reboot
   Send = Ids + ",u,0:0001"
   Gosub Serialsend
   Wait 1

Case "1"                                                    'address
   If Com_value >= Min_id And Com_value <= Max_id Then      'store address
      Stored_id = Com_value
      Id = Stored_id
      End If
   Send = Ids + ",u,1:00" + Hex(id)
   Gosub Serialsend
   If Ids <> Hex(id) Then Wait 1                            'reboot if address change

End Select
Goto Main
End If

Goto Main
End

Serialsend:
   Crc = Checksum(send)
   Print Send + "#" + Crc
   Return
End