# Lesson 13 Controlling LED by Light

##### Share for us

Introduction

In this lesson, we will use light intensity to control the luminance of an LED. When the light gets stronger, the LED will get brighter. When the light gets weaker, the LED will get darker.

Components

– 1*Raspberry Pi

– 1*Network cable (or USB wireless network adapter)

– 1*Photoresistor

– 1*LED

– 1*Resistor (220Ω)

– 1*Resistor (10KΩ)

-Jumper wires

Experimental Principle

In this experiment, we use PWM to control the luminance of the LED. The greater the duty cycle is, the brighter the LED is.

The resistance of the photoresistor decreases when the light intensity increases. We use the photoresistor output as the parameter of PWM after simple calculation by ADC. The weaker the light intensity is, the greater the PWM duty cycle is, and the brighter the LED is.

Experimental Procedures

Step 1: Connect the circuit

Step 2: Edit and save the code(see path/Rpi_BasicKit/13_illumCtrl/illumCtrl.c)

Step 3: Compile the code

Step 4: Run the program

Press Enter, if you change the incident light intensity, you will see the luminance of the LED change accordingly.

Code

``````#include <wiringPi.h>
#include <stdio.h>

typedef unsigned char uchar;
typedef unsigned int  uint;

{
//10:CH0
//11:CH1
uchar i;
uchar dat1=0, dat2=0;

for(i=0;i<8;i++)
{

}

for(i=0;i<8;i++)
{
}

return(dat1==dat2) ? dat1 : 0;
}

int main(void)
{

if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}