Exercise 11 - Connecting a LDR

1.      INTRODUCTION


A LDR (Light Dependent Resistor) is a electronic component in family of variable resistors. This means that a LDR decrease its resistor when light intensity rise.


2.      HOW TO CONNECT?

Materials:

·         Arduino UNO board

·         Protoboard

·         Some wire Jumpers

·         220 and 10K ohms resistors

·         LDR

·         LED

To connect LDR on Arduino you need to make a voltage divisor with a 10K resistor for example. Connect the resistor to Ground and the other side to pin A0. Also, connect the LDR to a resistor (Resistor, pinA0 and LDR are connected at one point) and 5V to the other side of LDR. On the other hand, make a standard connection for an LED on pin13.



EXERCISE_A (Testing the sensor)

 


3.      PROGRAMMING

EXERCISE_A (Testing the sensor)

This program increases luminosity of a LED when ambient luminosity is decreasing. It is necessary to regulate the higher and lower value in function of the place where you are measuring.

int ldr = 0;  //define a pin for Photo resistor

int led = 11;   //define a pin for LED

int higher = 945;

int lower = 660;

 

void setup()

{

  Serial.begin(9600);  //Begin serial communcation

  pinMode( led, OUTPUT );

}

 

On loop, the program writes in a LED in function of the value read in LDR pin. However, if this value overcomes higher or lower value automatically it is approximated to limit value.

 

void loop()

{

  int value = analogRead(ldr);

  Serial.println(value); //Write the value of the photoresistor to the serial monitor.

  //If the limits are exceeded value is equal to the maximum or minimum

  if (value >= higher) {

    value = higher - 1;

  }

  if (value <= lower) {

    value = lower + 1;

  }

  value = map(value, higher, lower, 0, 255);//Change the range and invert it to

  //increase the intesity of led when resistor

  //of ldr is increasing.

  analogWrite(led, value);

  delay(20); //short delay for faster response to light.

}


Last modified: Tuesday, 21 January 2020, 10:28 AM