
Light dependent resistors
In this example, we will have a closer look at an LDR. The principal behind it is the same as the bend sensor used in previous bend sensor example. Depending on the light levels cast onto the sensor, the sensor changes its output voltage. LDR comes in different shapes and sizes and the LDR used in Figure 2.8 comes premounted with a surface mounted resistor on a small PCB manufactured by Adafruit. The principal of the PCB is similar to the circuit created using a breadboard and components in Figure 2.3.

Figure 2.8: A PCB mounted LDR and resistor
The difference is that in the case of the PCB, in this example, everything is mounted nicely and we only need to attach it to the FLORA board using alligator clips. As shown in Figure 2.8, the LDR PCB has three connections VCC, OUT, and GND. Sometimes VCC is used to indicate power in. OUT in this case is the output signal, which is an analog signal. In Figure 2.9, you will find the necessary connections to the FLORA board. There is not a lot of space between the connections, so make sure that the alligator clips do not touch one another.

Figure 2.9: The LDR connected to the FLORA board
Since we are using the same connections as in the previous example in the chapter, you can use the same code to test you sensor. The following sketch is based on the same code as used in the bend/flex sensor example, but we have implemented some code to remap the range of values from the sensor into a new larger range of values.
//Variable to store the pin int ldrSensor = 10; void setup(){ //Start the serial communication Serial.begin(9600); } void loop(){ //Save the data from the sensor into storeData int storeData=analogRead(ldrSensor); //Re-map storeData to a new range of values int mapValue=map(storeData,130,430,0,2000); //Print the re-mapped value Serial.println(mapValue); //Give the computer some time to print delay(200); }
The map()
function in the code is used to take on a value range and remap it to a new range of values. In order for this function to work, you need to know the range of values possible from the senor you are using. Using the code from the bend sensor example, you can do this. In order to check you LDR sensor range, you need to print the data to your serial monitor. First, check the value you get when the LDR is not covered and write this down. Then, cover the LDR as much as possible and check the value. Make sure to remember this value as well since you'll need it.
The map function takes five parameters map(sensorValue
, sensorMin
, sensorMax
, desiredMin
, and desiredMax)
. The first parameter is the actual sensor data. The second and third are the minimum and maximum values from the sensor, which you wrote down from the previous test. The fourth and fifth are the minimum and maximum values of your desired range. In the case of the LDR that I an using, this gives a range of values between 130
to 430
, which we will choose to remap into a value range of 0
to 2000
.
The map function comes in handy in many cases when you want to use the data from a sensor but the default range does not fit your needs. Using the map function, you can translate it into any desired range.