Skip to main content

Project 3: Simulated Community Street Light System

๐ŸŽฏ Project Objectivesโ€‹

Build an automatic street light system that:

  • Uses a light sensor to detect ambient light intensity
  • Automatically controls 4 full-color LEDs (blue light) as street lights
  • Lights up more LEDs as environment gets darker
  • Displays real-time sensor values on screen

๐Ÿ“‹ Required Hardwareโ€‹

ComponentQuantityConnection
Steamania JoyKit V21Main Controller
TCS34725 Color Sensor1I2C Bus (for ambient light detection)
WS2812B Full-Color LED4On-board + External (configured as blue street lights)

๐Ÿ”ง Circuit Setupโ€‹

  1. TCS34725 Sensor: Connect to I2C bus (SDA, SCL, VCC, GND)
  2. 4x WS2812B LEDs:
    • LED #0: On-board (if available) or external
    • LED #1-3: External LEDs connected in series
    • All configured to emit blue light (street light simulation)

๐Ÿ’ก Core Tasksโ€‹

Task 1: Enable Light Sensorโ€‹

  • Use opcode 0xC4 to enable TCS34725 color sensor
  • Light intensity value stored in registers R31-R33 (RGB components)
  • Calculate total light intensity from RGB values

Task 2: Implement Multi-Level Threshold Logicโ€‹

  • Define 5 light levels based on sensor readings:
    • Very Bright (> 800): All LEDs OFF
    • Bright (600-800): 1 LED ON
    • Moderate (400-600): 2 LEDs ON
    • Dim (200-400): 3 LEDs ON
    • Very Dim (< 200): All 4 LEDs ON

Task 3: Control Multiple LEDsโ€‹

  • Use opcode 0xE4 to control each LED independently
  • Set all active LEDs to blue color (0x0000FF)
  • Implement sequential or parallel LED control

Task 4: Display Real-Time Dataโ€‹

  • Show current light intensity value
  • Display number of active LEDs
  • Update display in real-time

๐Ÿ“ Reference Solutionโ€‹

Program Codeโ€‹

000 C4 0001 0000  ; Enable TCS34725 color sensor
001 C7 0001 0000 ; Enable WS2812B LED control
002 10 0028 FFFF ; Set foreground color to white (for text)
003 0E 0000 0010 ; Jump to main loop at line 010

; Main Loop - Start at line 010
010 11 0001 0031 ; Copy R31 (Red) to R01
011 11 0002 0032 ; Copy R32 (Green) to R02
012 11 0003 0033 ; Copy R33 (Blue) to R03
013 32 0004 0102 ; R04 = R01 + R02 (Red + Green)
014 32 0004 0403 ; R04 = R04 + R03 (Total = R + G + B)

; Check Light Level and Control LEDs
015 04 0320 0030 ; If R04 > 800 (very bright), jump to all OFF
016 04 0258 0040 ; If R04 > 600 (bright), jump to 1 LED
017 04 0190 0050 ; If R04 > 400 (moderate), jump to 2 LEDs
018 04 00C8 0060 ; If R04 > 200 (dim), jump to 3 LEDs
019 0E 0000 0070 ; Otherwise (very dim), all 4 LEDs ON

; Level 0: Very Bright - All LEDs OFF
030 E4 0000 0000 ; LED 0 OFF
031 E4 0100 0000 ; LED 1 OFF
032 E4 0200 0000 ; LED 2 OFF
033 E4 0300 0000 ; LED 3 OFF
034 10 0020 0000 ; R20 = 0 (LED count)
035 0E 0000 0080 ; Jump to display

; Level 1: Bright - 1 LED ON
040 E4 0000 00FF ; LED 0 ON (blue)
041 E4 0100 0000 ; LED 1 OFF
042 E4 0200 0000 ; LED 2 OFF
043 E4 0300 0000 ; LED 3 OFF
044 10 0020 0001 ; R20 = 1 (LED count)
045 0E 0000 0080 ; Jump to display

; Level 2: Moderate - 2 LEDs ON
050 E4 0000 00FF ; LED 0 ON (blue)
051 E4 0100 00FF ; LED 1 ON (blue)
052 E4 0200 0000 ; LED 2 OFF
053 E4 0300 0000 ; LED 3 OFF
054 10 0020 0002 ; R20 = 2 (LED count)
055 0E 0000 0080 ; Jump to display

; Level 3: Dim - 3 LEDs ON
060 E4 0000 00FF ; LED 0 ON (blue)
061 E4 0100 00FF ; LED 1 ON (blue)
062 E4 0200 00FF ; LED 2 ON (blue)
063 E4 0300 0000 ; LED 3 OFF
064 10 0020 0003 ; R20 = 3 (LED count)
065 0E 0000 0080 ; Jump to display

; Level 4: Very Dim - All 4 LEDs ON
070 E4 0000 00FF ; LED 0 ON (blue)
071 E4 0100 00FF ; LED 1 ON (blue)
072 E4 0200 00FF ; LED 2 ON (blue)
073 E4 0300 00FF ; LED 3 ON (blue)
074 10 0020 0004 ; R20 = 4 (LED count)

; Display Update - Line 080
080 55 0000 0011 ; Display light intensity at (0, 0)
081 55 0050 0020 ; Display LED count at (5, 0)
082 02 03E8 0000 ; Delay 1000ms (1 second)
083 0E 0000 0010 ; Jump back to line 010 (main loop)

Optimized Solution with Better Light Calculationโ€‹

000 C4 0001 0000  ; Enable TCS34725 color sensor
001 C7 0001 0000 ; Enable WS2812B LED control (supports 4 LEDs)
002 10 0028 FFFF ; Set foreground color to white (for text)
003 10 0029 0000 ; Set background color to black
004 0E 0000 0010 ; Jump to main loop

; Main Loop - Read Sensor and Calculate Light Intensity
010 11 0001 0031 ; R01 = R31 (Red component)
011 11 0002 0032 ; R02 = R32 (Green component)
012 11 0003 0033 ; R03 = R33 (Blue component)

; Calculate average light intensity: (R + G + B) / 3
013 32 0004 0102 ; R04 = R01 + R02
014 32 0004 0403 ; R04 = R04 + R03 (total = R + G + B)
015 10 0005 0003 ; R05 = 3 (divisor)
016 32 0004 0405 ; R04 = R04 / R05 (average)

; Multi-Level Threshold Detection
017 04 0320 0030 ; If R04 > 800, jump to Level 0 (all OFF)
018 04 0258 0040 ; If R04 > 600, jump to Level 1 (1 LED)
019 04 0190 0050 ; If R04 > 400, jump to Level 2 (2 LEDs)
01A 04 00C8 0060 ; If R04 > 200, jump to Level 3 (3 LEDs)
01B 0E 0000 0070 ; Otherwise Level 4 (all 4 LEDs)

; Level 0: Very Bright (Light > 800) - All LEDs OFF
030 E4 0000 0000 ; LED #0: OFF
031 E4 0100 0000 ; LED #1: OFF
032 E4 0200 0000 ; LED #2: OFF
033 E4 0300 0000 ; LED #3: OFF
034 10 0020 0000 ; R20 = 0 (active LED count)
035 0E 0000 0080 ; Jump to display update

; Level 1: Bright (600 < Light โ‰ค 800) - 1 LED ON
040 E4 0000 00FF ; LED #0: ON (blue)
041 E4 0100 0000 ; LED #1: OFF
042 E4 0200 0000 ; LED #2: OFF
043 E4 0300 0000 ; LED #3: OFF
044 10 0020 0001 ; R20 = 1
045 0E 0000 0080 ; Jump to display

; Level 2: Moderate (400 < Light โ‰ค 600) - 2 LEDs ON
050 E4 0000 00FF ; LED #0: ON (blue)
051 E4 0100 00FF ; LED #1: ON (blue)
052 E4 0200 0000 ; LED #2: OFF
053 E4 0300 0000 ; LED #3: OFF
054 10 0020 0002 ; R20 = 2
055 0E 0000 0080 ; Jump to display

; Level 3: Dim (200 < Light โ‰ค 400) - 3 LEDs ON
060 E4 0000 00FF ; LED #0: ON (blue)
061 E4 0100 00FF ; LED #1: ON (blue)
062 E4 0200 00FF ; LED #2: ON (blue)
063 E4 0300 0000 ; LED #3: OFF
064 10 0020 0003 ; R20 = 3
065 0E 0000 0080 ; Jump to display

; Level 4: Very Dim (Light โ‰ค 200) - All 4 LEDs ON
070 E4 0000 00FF ; LED #0: ON (blue)
071 E4 0100 00FF ; LED #1: ON (blue)
072 E4 0200 00FF ; LED #2: ON (blue)
073 E4 0300 00FF ; LED #3: ON (blue)
074 10 0020 0004 ; R20 = 4
; Falls through to display update

; Display Update
080 55 0000 0011 ; Display light value at (0, 0)
081 55 0050 0020 ; Display LED count at (5, 0)
082 02 03E8 0000 ; Delay 1000ms
083 0E 0000 0010 ; Loop back to start

๐Ÿ” Detailed Explanationโ€‹

Initialization (Lines 000-004)โ€‹

Line 000: C4 0001 0000

  • Enable TCS34725 color sensor module
  • Sensor automatically measures ambient light and stores RGB values
  • R31 = Red component, R32 = Green component, R33 = Blue component
  • Values range from 0-65535 (16-bit)

Line 001: C7 0001 0000

  • Enable WS2812B full-color LED control
  • Supports controlling up to 4+ LEDs independently
  • Each LED addressed by index (0, 1, 2, 3...)

Line 002: 10 0028 FFFF

  • Set foreground color to white (0xFFFF in RGB565)
  • Used for text display on screen

Line 003: 10 0029 0000

  • Set background color to black
  • Provides contrast for text display

Line 004: 0E 0000 0010

  • Jump to main loop at line 010

Light Intensity Calculation (Lines 010-016)โ€‹

Line 010: 11 0001 0031

  • Copy Red component from R31 to R01
  • Prepares for calculation

Line 011: 11 0002 0032

  • Copy Green component from R32 to R02

Line 012: 11 0003 0033

  • Copy Blue component from R33 to R03

Line 013: 32 0004 0102

  • R04 = R01 + R02 (Red + Green)
  • Uses opcode 0x32 for register addition

Line 014: 32 0004 0403

  • R04 = R04 + R03 (Total = R + G + B)
  • R04 now contains total light intensity

Line 015: 10 0005 0003

  • Set R05 = 3 (divisor for averaging)

Line 016: 32 0004 0405

  • R04 = R04 / R05 (Average = Total / 3)
  • R04 now contains average light intensity (0-65535 scale)

Threshold Detection (Lines 017-01B)โ€‹

Line 017: 04 0320 0030

  • If R04 > 800 (very bright), jump to line 030 (all LEDs OFF)
  • 0320 = 800 in hexadecimal

Line 018: 04 0258 0040

  • If R04 > 600 (bright), jump to line 040 (1 LED ON)
  • 0258 = 600 in hexadecimal

Line 019: 04 0190 0050

  • If R04 > 400 (moderate), jump to line 050 (2 LEDs ON)
  • 0190 = 400 in hexadecimal

Line 01A: 04 00C8 0060

  • If R04 > 200 (dim), jump to line 060 (3 LEDs ON)
  • 00C8 = 200 in hexadecimal

Line 01B: 0E 0000 0070

  • Otherwise (very dim, โ‰ค 200), continue to line 070 (all 4 LEDs ON)

LED Control by Levelโ€‹

Level 0: Very Bright (Lines 030-035)โ€‹

  • All 4 LEDs turned OFF
  • E4 [LED_Index] 0000 0000 = Black/OFF
  • R20 = 0 (store LED count for display)

Level 1: Bright (Lines 040-045)โ€‹

  • LED #0 ON (blue: 0000 00FF)
  • LEDs #1-3 OFF
  • R20 = 1

Level 2: Moderate (Lines 050-055)โ€‹

  • LEDs #0-1 ON (blue)
  • LEDs #2-3 OFF
  • R20 = 2

Level 3: Dim (Lines 060-065)โ€‹

  • LEDs #0-2 ON (blue)
  • LED #3 OFF
  • R20 = 3

Level 4: Very Dim (Lines 070-074)โ€‹

  • All 4 LEDs ON (blue)
  • R20 = 4

Display Update (Lines 080-083)โ€‹

Line 080: 55 0000 0011

  • Display light intensity value at screen position (0, 0)
  • Shows value from R04 (average light intensity)

Line 081: 55 0050 0020

  • Display LED count at position (5, 0)
  • Shows value from R20 (number of active LEDs)

Line 082: 02 03E8 0000

  • Delay 1000 milliseconds (1 second)
  • 03E8 = 1000 in decimal
  • Provides stable updates and prevents flickering

Line 083: 0E 0000 0010

  • Jump back to line 010
  • Creates infinite loop for continuous monitoring

๐ŸŽจ Light Level Threshold Tableโ€‹

Light LevelIntensity RangeLEDs ONBlue LEDs Active
Very Bright> 8000None
Bright601-8001LED #0
Moderate401-6002LED #0, #1
Dim201-4003LED #0, #1, #2
Very Dimโ‰ค 2004All 4 LEDs

๐Ÿ”ง LED Color Configurationโ€‹

All active LEDs are set to blue to simulate street lights:

E4 [LED_Index] 0000 00FF
โ”‚ โ”‚ โ””โ”€ Blue (FF = 255 = max brightness)
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€ Green (00 = off)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ LED Index (00-03)

๐Ÿ” Key Learning Pointsโ€‹

  1. Sensor Integration: Reading and processing data from color/light sensor
  2. Multi-Level Logic: Implementing 5-level threshold detection
  3. Sequential Control: Controlling multiple outputs independently
  4. Data Visualization: Displaying real-time sensor values
  5. Automation: Creating light-responsive automatic system
  6. Register Arithmetic: Using addition and division for calculations

๐Ÿงช Testing Checklistโ€‹

  • TCS34725 sensor is properly connected and enabled
  • Light intensity values display correctly on screen
  • All 4 LEDs respond to light level changes
  • Threshold transitions are smooth and accurate
  • LEDs are correctly colored blue
  • System updates approximately once per second
  • More LEDs turn on as environment gets darker

๐Ÿ’ก Extension Challengesโ€‹

  1. Adjustable Thresholds: Add buttons to adjust light sensitivity
  2. Gradual Dimming: Use PWM to smoothly transition between levels
  3. Different Colors: Use different colors for different light levels
  4. Data Logging: Track and display minimum/maximum light values
  5. Time-Based Control: Add RTC module for sunset/sunrise simulation
  6. Motion Detection: Add PIR sensor to only light up when motion detected
  7. Energy Monitoring: Calculate and display power consumption
  8. Wireless Control: Add Bluetooth for remote monitoring

๐Ÿ› Common Issues and Solutionsโ€‹

Issue 1: LEDs Don't Turn Onโ€‹

Cause: LED control not enabled or incorrect LED index Solution: Verify line 001 enables LED control and check LED indices (0-3)

Issue 2: Light Values Seem Incorrectโ€‹

Cause: Sensor not calibrated or wrong calculation Solution: Check sensor enable (line 000) and verify RGB averaging logic

Issue 3: Thresholds Don't Match Expected Behaviorโ€‹

Cause: Threshold values need adjustment for your environment Solution: Modify threshold values in lines 017-01A based on actual readings

Issue 4: Only Some LEDs Workโ€‹

Cause: LED chain not properly connected Solution: Check physical connections and LED indexing

Issue 5: Display Updates Too Fast/Slowโ€‹

Cause: Delay time incorrect Solution: Adjust delay value in line 082 (e.g., 500ms for faster, 2000ms for slower)

๐Ÿ“Š Real-World Applicationsโ€‹

This project simulates real-world smart city applications:

  1. Energy Efficiency: Only use necessary lighting based on conditions
  2. Automatic Control: No manual intervention required
  3. Scalability: Can extend to control hundreds of street lights
  4. Smart Cities: Foundation for IoT-based urban lighting systems
  5. Safety: Ensures adequate lighting when needed

Instructions Used in This Projectโ€‹

OpcodeNameDescriptionDocumentation Link
0x02DelaySet delay time in millisecondsDelay
0x04Greater Than JumpJump if R00 > immediate valueConditional Jumps
0x0EUnconditional JumpJump to specified lineUnconditional Jump
0x10Write RegisterWrite immediate value to registerRegister Operations
0x11Copy RegisterCopy value from one register to anotherRegister Operations
0x32Register AddAdd two registers, result in first registerRegister Operations
0x55Display TextDisplay register value on screenText Output
0xC4Enable TCS34725Enable color/light sensor moduleExternal Devices
0xCBEnable WS2812BEnable LED control moduleExternal Devices
0xE4Set LED ColorSet RGB color for specific LEDLights & Music

Additional Referenceโ€‹

  • Register Map - Complete register documentation
    • R31-R33: TCS34725 RGB data registers
    • R28: Foreground color register
    • R29: Background color register
  • Instruction Set Summary - Full instruction set overview

๐ŸŽ“ Assessment Rubricโ€‹

CriteriaExcellent (5)Good (4)Satisfactory (3)Needs Improvement (2)
Circuit SetupAll components correctly connectedMinor connection errorsSome connections incorrectMajor wiring issues
Program LogicClean, efficient code with proper thresholdsWorking code with minor issuesCode works but inefficientCode doesn't work correctly
LED ControlAll 4 LEDs respond correctly to light levels3 LEDs work correctly2 LEDs work correctly1 or fewer LEDs work
DisplayShows both light value and LED count clearlyShows one value correctlyDisplay has errorsNo display output
Testing & DebugThoroughly tested, all edge cases handledMost cases testedBasic testing onlyLittle to no testing