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โ
| Component | Quantity | Connection |
|---|---|---|
| Steamania JoyKit V2 | 1 | Main Controller |
| TCS34725 Color Sensor | 1 | I2C Bus (for ambient light detection) |
| WS2812B Full-Color LED | 4 | On-board + External (configured as blue street lights) |
๐ง Circuit Setupโ
- TCS34725 Sensor: Connect to I2C bus (SDA, SCL, VCC, GND)
- 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
0xC4to 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
0xE4to 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 (
0xFFFFin 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
R31toR01 - Prepares for calculation
Line 011: 11 0002 0032
- Copy Green component from
R32toR02
Line 012: 11 0003 0033
- Copy Blue component from
R33toR03
Line 013: 32 0004 0102
R04 = R01 + R02(Red + Green)- Uses opcode
0x32for register addition
Line 014: 32 0004 0403
R04 = R04 + R03(Total = R + G + B)R04now 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)R04now contains average light intensity (0-65535 scale)
Threshold Detection (Lines 017-01B)โ
Line 017: 04 0320 0030
- If
R04 > 800(very bright), jump to line030(all LEDs OFF) 0320= 800 in hexadecimal
Line 018: 04 0258 0040
- If
R04 > 600(bright), jump to line040(1 LED ON) 0258= 600 in hexadecimal
Line 019: 04 0190 0050
- If
R04 > 400(moderate), jump to line050(2 LEDs ON) 0190= 400 in hexadecimal
Line 01A: 04 00C8 0060
- If
R04 > 200(dim), jump to line060(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/OFFR20 = 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 Level | Intensity Range | LEDs ON | Blue LEDs Active |
|---|---|---|---|
| Very Bright | > 800 | 0 | None |
| Bright | 601-800 | 1 | LED #0 |
| Moderate | 401-600 | 2 | LED #0, #1 |
| Dim | 201-400 | 3 | LED #0, #1, #2 |
| Very Dim | โค 200 | 4 | All 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โ
- Sensor Integration: Reading and processing data from color/light sensor
- Multi-Level Logic: Implementing 5-level threshold detection
- Sequential Control: Controlling multiple outputs independently
- Data Visualization: Displaying real-time sensor values
- Automation: Creating light-responsive automatic system
- 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โ
- Adjustable Thresholds: Add buttons to adjust light sensitivity
- Gradual Dimming: Use PWM to smoothly transition between levels
- Different Colors: Use different colors for different light levels
- Data Logging: Track and display minimum/maximum light values
- Time-Based Control: Add RTC module for sunset/sunrise simulation
- Motion Detection: Add PIR sensor to only light up when motion detected
- Energy Monitoring: Calculate and display power consumption
- 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:
- Energy Efficiency: Only use necessary lighting based on conditions
- Automatic Control: No manual intervention required
- Scalability: Can extend to control hundreds of street lights
- Smart Cities: Foundation for IoT-based urban lighting systems
- Safety: Ensures adequate lighting when needed
๐ Related Documentationโ
Instructions Used in This Projectโ
| Opcode | Name | Description | Documentation Link |
|---|---|---|---|
0x02 | Delay | Set delay time in milliseconds | Delay |
0x04 | Greater Than Jump | Jump if R00 > immediate value | Conditional Jumps |
0x0E | Unconditional Jump | Jump to specified line | Unconditional Jump |
0x10 | Write Register | Write immediate value to register | Register Operations |
0x11 | Copy Register | Copy value from one register to another | Register Operations |
0x32 | Register Add | Add two registers, result in first register | Register Operations |
0x55 | Display Text | Display register value on screen | Text Output |
0xC4 | Enable TCS34725 | Enable color/light sensor module | External Devices |
0xCB | Enable WS2812B | Enable LED control module | External Devices |
0xE4 | Set LED Color | Set RGB color for specific LED | Lights & Music |
Additional Referenceโ
- Register Map - Complete register documentation
R31-R33: TCS34725 RGB data registersR28: Foreground color registerR29: Background color register
- Instruction Set Summary - Full instruction set overview
๐ Assessment Rubricโ
| Criteria | Excellent (5) | Good (4) | Satisfactory (3) | Needs Improvement (2) |
|---|---|---|---|---|
| Circuit Setup | All components correctly connected | Minor connection errors | Some connections incorrect | Major wiring issues |
| Program Logic | Clean, efficient code with proper thresholds | Working code with minor issues | Code works but inefficient | Code doesn't work correctly |
| LED Control | All 4 LEDs respond correctly to light levels | 3 LEDs work correctly | 2 LEDs work correctly | 1 or fewer LEDs work |
| Display | Shows both light value and LED count clearly | Shows one value correctly | Display has errors | No display output |
| Testing & Debug | Thoroughly tested, all edge cases handled | Most cases tested | Basic testing only | Little to no testing |