Two Light Detecting Resistors#
This setup is slightly more interesting, we use two light detecting resistors (LDR) that have a variable resistance inversely dependant on the light. Near dark they are about 50 kΩ and 500 Ω in bright light. The Arduino can measure voltage so it is combined with a resistor to enable the Arduino to function. The LDR is connected to 5V and the other leg goes to a junction between the analogue pin and a 4.7 kΩ resistor that is connected to ground. This setup acts as a voltage divider. As a bonus we can trigger a LED when a threshold is reached. These values are dependant on the LDR characteristic and the light level.
Show/Hide Code TwoLDRs.ino
1// analogue-plot
2//
3// Read analogue values from A0 and A1 and print them to serial port.
4/*
5 Component Connect to and to
6 LDR right leg arduino 5V
7 LDR left leg arduino A0
8 LDR left leg 4.7kΩ resistor arduino GnD
9 LED long leg arduino pin 10
10 LED short leg 220Ω resistor arduino GnD
11
12 LDR right leg arduino 5V
13 LDR left leg arduino A1
14 LDR left leg 4.7kΩ resistor arduino GnD
15 LED long leg arduino pin 11
16 LED short leg 220Ω resistor arduino GnD
17*/
18
19int Vdivid0 = A0; // voltage divider analog pin
20int LEDpin0 = 10; // LED on PWM pin
21int Vdivid1 = A1; // voltage divider analog pin
22int LEDpin1 = 11; // LED on PWM pin
23int thresh = 500; // threshold light intensity
24int reading0, bright0, reading1, bright1;
25void setup()
26{
27 Serial.begin(9600);
28 pinMode(LEDpin0, OUTPUT); // LED pin as output
29 pinMode(LEDpin1, OUTPUT); // LED pin as output
30 }
31
32void loop()
33{
34 reading0 = analogRead(Vdivid0); // voltage divider reading
35 bright0 = 0; // set LED brightness to zero
36 // map reading to LED brightness
37 if(reading0<thresh) bright0 = map(reading0, 0, thresh, 255, 0);
38 analogWrite(LEDpin0, bright0); // change LED brightness
39
40 reading1 = analogRead(Vdivid1); // voltage divider reading
41 bright1 = 0; // set LED brightness to zero
42 // map reading to LED brightness
43 if(reading1<thresh) bright1 = map(reading1, 0, thresh, 255, 0);
44 analogWrite(LEDpin1, bright1); // change LED brightness
45
46 // print to serial
47 Serial.print(reading0);
48 Serial.print(" ");
49 Serial.print(reading1);
50 Serial.print("\n");
51
52 delay(1000); // delay 1000ms
53 }
Note
The leds must always be protected from overcurrent, on a 5V supply
a 220Ω resistor is suitable.
Map is a useful function to ensure that the Arduino works in the correct range, the first value (in our case reading0 and reading1) is the variable under consideration. The next two values are the first range, fromLow and fromHigh, the last two values are the second range, toLow and toHigh. In our case we are swopping the high and low values in the second range since resistance increases with brightness. The output of map is an integer.
In the first example we had only one value to send and this was simply a println. In the current example there are two values in every data packet. This means we need to send the first piece of data followed by a separator, the second piece of data followed by a return (we could have sent a println with the second piece).
As it stands we can check the output on the serial monitor and the serial plotter. When satisfied it creates the right output, we can tie into a python application. We could simply print the results within a console or Idle.
There are more interesting possibilities.