Tutorial Video
Hardware
- PHPoC Blue or PHPoC Black
- Micro USB to USB Cable (to upload source code to PHPoC Device)
- Light Sensor
- Jumper wires
About Light Sensor
Light sensor is used to detect the current ambient light level - i.e. how bright/dark it is. The light sensor used in this example includes three pins:
- VCC pin.
- GND pin.
- Signal pin (outputs analog signal).
- The sensor outputs analog signal to signal pin. There bigger the ambient light level is, the higher the signal value is.
- By reading value of sensor's signal pin, we can infer ambient light level. To read value of sensor's signal pin, we just need to connect the sensor's signal pin to PHPoC device's ADC (Analog to Digital Converter) pin and use PHPoC code to read the value.
- Grove light sensor (for more convenience, use it in combination with PHPoC Grove Expansion Board).
- LDR Photoresistor Light Sensor.
Wiring Diagram

Quick Steps
Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
- Download PHPoC Support Package.
- Upload example\p4s\01.php_task\02.adc_light_sensor to PHPoC Blue/Black.
- Click "Run" button on PHPoC Debugger.
Source Code
Source files includes:
- init.php: this file is run when PHPoC system is powered or reset. It is used to specify which file is run is system loop.
- task0.php: this file is run in system loop of PHPoC devices.
init.php
This file is run when PHPoC system is powered or reset. It is used to specify that task0.php is run is system loop.
PHP Code:
<?php
system("php task0.php");
?>
task0.php
[Full Code]
PHP Code:
<?php
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
include "/lib/sd_340.php";
define("ADC_MAX", 2520);
echo "PHPoC example : P4S-34X / ADC / Catalex Light Sensor\r\n";
adc_setup(0, 0); // adc0, channel 0
$last_adc_in = 0;
while(1)
{
$adc_in = adc_in(0, 30);
if($adc_in > ADC_MAX)
$adc_in = ADC_MAX;
if(abs($adc_in - $last_adc_in) > 5)
{
printf("Illuminance level : %d(%%)\r\n", $adc_in * 100 / ADC_MAX);
$last_adc_in = $adc_in;
sleep(1);
}
}
?>
[Explanation]
Source code of this file does:
- Setup ADC pin.
- Read value from light sensor.
- Calculate illuminance level based on the read value.
- Print illuminance level to PHPoC Debugger's console.
See Also
- Light Sensor - Monitoring Light Sensor from Webpage using Hypertext.
- Light Sensor - Monitoring Light Sensor from Webpage with Image.
- Light Sensor - Monitoring Light Sensor via WebSocket.
- Light Sensor - Monitoring Light Sensor via WebSocket with Web-based Gauge.
Other Resources