2.2.1 Photoresistor¶
Introduction¶
Photoresistor is a commonly used component of ambient light intensity in life. It helps the controller to recognize day and night and realize light control functions such as night lamp. This project is very similar to potentiometer, and you might think it changing the voltage to sensing light.
Required Components¶
In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Schematic Diagram¶


Experimental Procedures¶
Step 1: Build the circuit.

Step 2: Go to the folder of the code.
cd ~/raphael-kit/nodejs/
Step 3: Run the code.
sudo node photoresistor.js
When the code is running, the brightness of the LED will change according to the light intensity sensed by the photoresistor.
Code
const Gpio = require('pigpio').Gpio;
const ADC0834 = require('./adc0834.js').ADC0834;
exports.ADC0834 = ADC0834;
const adc = new ADC0834(17, 18, 27);
const led = new Gpio(22, {mode: Gpio.OUTPUT});
setInterval(() => {
adc.read(0).then((value) => {
console.log(value);
led.pwmWrite(value);
}, (error)=>{
console.log("Error: " + error);
});
}, 100);
Code Explanation
The codes here are the same as that in 2.1.7 Potentiometer. Please check the code explanation of 2.1.7 Potentionmeter for details.
Phenomenon Picture¶
