Light sensor with LED display

From uCtrl
This page is kept for historical interest. It may document projects or methods that are obsolete and/or no longer relevant. The information here is not kept up-to-date.

I used this module to control the LED lights in my first apartment.
LSU mounted in the living room.

Information

This module measures the light intensify and shows this on a scale from 0 to 782 on the LED display. By multiplying the shown value with 5, and then divide by 127 you get the light intensity in µW/cm2. The threshold value is shown while it's being adjusted, if the light intensity rises over/or falls below (optional) the output will be activated. The hysteresis is +-25. Only one digit is shown on the LED display at any time, but the speed is so fast that me see it as three stable digits.

LED-Display

Light-sensor-led-display-illustration-3gz734.png

When DP lit:

  1. Showing threshold value
  2. Output active
  3. EEPROM value not yet saved.

I/O

Inputs

  1. PC.0 Light sensor, analog value
  2. PC.1 Threshold up
  3. PC.2 Threshold down
  4. PC.3 Over/under threshold

Calculations

Light intensity

Outputs

  1. PB.0 LED-display digit 1
  2. PB.1 LED-display digit 2
  3. PB.2 LED-display digit 3
  4. PB.3 Output constant
  5. PB.4 Output pulse

LED-Display

  1. PortD.0 LED-display A
  2. PortD.1 LED-display B
  3. PortD.2 LED-display C
  4. PortD.3 LED-display D
  5. PortD.4 LED-display E
  6. PortD.5 LED-display F
  7. PortD.6 LED-display G
  8. PortD.7 LED-display DP

Led-display-segments-and-pin-out-j4611g.jpeg Led-display-binary-values-9q7v3q.png

D-Sub 9-pin

Pin Function Color
1 9-15V + Yellow
2 GND White
3 Output constant Green
4 Output pulse Brown

Images

Schematic drawing

Light-sensor-circuit-pgo0pg.png

Parts list

Qty. Part
1 AVR ATmega8-16PU, DIL-28, 16 MHz, 23 I/Os
1 Capacitor, aluminium electrolytic, 10 µF, 25V
2 Capacitor, aluminium electrolytic, 47 µF, 25V
1 Capacitor, ceramic, 1 nF, 100V
2 m Control cable, 4-cores, 0.25mm2, 250 V, Ø 4.6mm
1 D-Sub soldering cups, 9 pin male
1 DIL socket, 28-pin, 7.62mm
1 Diode, rectifier, 1 A, 400V, 1N4004
1 Enclosure, plastic (1591 FL), 120x65x40mm, flange
1 Fuse 5x20 mm, 400 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 LED display, 3x7-segment, 9.2mm, Green
1 Light-to-voltage sensor, TSL 250R, 127mV / (μW/cm2)
32 cm2 PCB, stripboard, 100x160mm, 160cm2
11 Resistor, carbon film, 0.25W, 330 Ω, 5%
6 Resistor, carbon film, 0.25W, 4.7 kΩ, 5%
3 Resistor, carbon film, 0.25W, 10 kΩ, 5%
3 Spacer, round unthreaded, 3mm, Ø6mm, Delrin
22 Straight pin header, female, Single row, 2.54mm
22 Straight pin header, male, Single row, 2.54mm
1 Switch, toggle, 1-pole, micro, (on)-off-(on)
1 Switch, toggle, 1-pole, micro, on-on
5 Transistor, NPN, 100 mA, 45V, 0.5W, BC547B
1 Voltage regulator +5V, 2 A, L78S05CV

Source code

Template:Basic code

'--------------------------------------------------------------
' Thomas Jensen
'--------------------------------------------------------------
' file: AVR_LIGHT_SENSOR_v1.0
' date: 18/03/2007
'--------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 1000000
Config Watchdog = 1024
Config Portb = Output
Config Portd = Output
Config Portc = Input

Dim W As Word , Volt As Word , Read_timer As Integer
Dim C_limit1 As Integer , C_limit2 As Integer
Dim Over_limit As Bit , Limit As Integer
Dim Volt_temp As String * 1 , Volt_string As String * 3
Dim Volt_number As Integer , Set_timer As Integer
Dim Over_limit_set As Bit , Action_timer As Integer
Dim Up_counter As Integer , Down_counter As Integer
Dim Eeprom_limit As Eram Integer , Eeprom_save As Integer
Dim Lowlimit As Integer , Highlimit As Integer

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

'get eeprom values
Limit = Eeprom_limit

Portd = 64
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Waitms 500
Portb.0 = 0
Portb.1 = 1
Waitms 500
Portb.1 = 0
Portb.2 = 1
Waitms 500
Portb.2 = 0
Waitms 500

Start Watchdog

'program start
Main:
'read light
If Read_timer = 0 Then
W = Getadc(0)
Volt = W
Read_timer = 666
End If

'set limit up
If Pinc.1 = 1 Then
   C_limit1 = 0
   Up_counter = 0
   End If
If Pinc.1 = 0 Then
   If C_limit1 = 0 Then Incr Limit
   C_limit1 = 1
   Incr Up_counter
   Eeprom_save = 20000
   Set_timer = 133
   End If

'set limit down
If Pinc.2 = 1 Then
   C_limit2 = 0
   Down_counter = 0
   End If
If Pinc.2 = 0 Then
   If C_limit2 = 0 Then Decr Limit
   C_limit2 = 1
   Incr Down_counter
   Eeprom_save = 20000
   Set_timer = 133
   End If

'correct if value out of range
If Limit < 0 Then Limit = 0
If Limit > 999 Then Limit = 999
If Volt > 999 Then Volt = 999

'check limit and set conditions
Lowlimit = Limit - 25
Highlimit = Limit + 25

If Pinc.3 = 0 Then
   If Volt > Highlimit Then Over_limit = 1
   If Volt < Lowlimit Then Over_limit = 0
   Else
   If Volt < Lowlimit Then Over_limit = 1
   If Volt > Highlimit Then Over_limit = 0
End If

'show light level/threshold
If Set_timer = 0 Then Volt_string = Str(volt)
If Set_timer > 0 Then Volt_string = Str(limit)

'digit 1
If Len(volt_string) > 2 Then Volt_temp = Left(volt_string , 1) Else Volt_temp = "0"
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Set_timer > 0 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 121
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Waitms 5

Portb.0 = 0
Portb.1 = 0
Portb.2 = 0

'digit 2
If Len(volt_string) = 2 Then Volt_temp = Left(volt_string , 1)
If Len(volt_string) > 2 Then Volt_temp = Mid(volt_string , 2 , 1)
If Len(volt_string) < 2 Then Volt_temp = "0"
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Over_limit = 1 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 80
Portb.0 = 0
Portb.1 = 1
Portb.2 = 0
Waitms 5

Portb.0 = 0
Portb.1 = 0
Portb.2 = 0

'digit 3
Volt_temp = Right(volt_string , 1)
Volt_number = Val(volt_temp)
Portd = Lookup(volt_number , Table)
If Read_timer > 600 And Eeprom_save = 0 Then Portd.7 = 1
If Read_timer < 600 And Eeprom_save > 0 Then Portd.7 = 1
If Len(volt_string) > 3 Then Portd = 80
Portb.0 = 0
Portb.1 = 0
Portb.2 = 1
Waitms 5

Portb.0 = 0
Portb.1 = 0
Portb.2 = 0

'set outputs
If Over_limit = 1 And Over_limit_set = 0 Then
   Over_limit_set = 1
   Portb.3 = 1
   Action_timer = 33
   End If
If Over_limit = 0 Then
   If Over_limit_set = 1 Then Action_timer = 100
   Over_limit_set = 0
   Portb.3 = 0
   End If

'pulse output
If Action_timer > 0 Then
   Decr Action_timer
   Portb.4 = 1
   End If
If Action_timer = 0 Then Portb.4 = 0

'timers
If Set_timer > 0 Then Decr Set_timer
If Read_timer > 0 Then Decr Read_timer

'fast threshold adjust
If Up_counter > 66 Then Incr Limit
If Down_counter > 66 Then Decr Limit

'save eeprom values
If Eeprom_save > 0 Then Decr Eeprom_save
If Eeprom_save = 1 Then Eeprom_limit = Limit

'loop
Reset Watchdog
Goto Main
End

'---- data for correct display of numbers on LED display ------
Table:
Data 63 , 6 , 91 , 79 , 102 , 109 , 125 , 7 , 127 , 111 , 128
' 0 1 2 3 4 5 6 7 8 9 dp