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.

../_images/list_2.2.1_photoresistor.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

LED

BUY

ADC0834

-

Photoresistor

BUY

Schematic Diagram

../_images/image321.png ../_images/image322.png

Experimental Procedures

Step 1: Build the circuit.

../_images/image198.png

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

../_images/image199.jpeg