Optical
Light-Dependent Resistor (LDR)
This recipe relies on a sensor known as the Light Dependent Resistor, or LDR for short.
While more modern and accurate sensors are readily available on the market, LDRs are a great way to understand how analogue sensing is done.
As the name implies, a Light Dependent Resistor changes its electrical resistance upon exposure to light. The more light hits the front surface of the sensor, the less electricl resistance there is.
Wiring & Code
Select a microcontroller platform in the tabs below to view the wiring and code meant for the platform:
-
Breadboard diagram
Download Light-dependent resistor (LDR) Fritzing file
Code
For a refresher on how to use the code in this recipe, click here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// LDR sensing code for the Arduino UNO // Move your hand over the LDR sensors to alter the brightness of the LED // update these definitions below if you move the pins: #define LDR_SENSE_PIN A0 // analogue input pin A0 #define LED_PIN 11 // digital/PWM pin 11 #define LDR_MAX 240 // what is the maximum LDR reading that you detected from your circuit (via the Serial Monitor)? Note that this also depends on the environment you are sensing! #define LDR_MIN 140 // similarly, note down the minimum LDR reading. We use these two limits of the readings to generate a more dynamic scaling between these values (see below) int ldrVal = 0; // this is a variable; we use this to keep track of numerical values in our code (see line 16) void setup() { // runs once at startup pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // turn off the LED at startup Serial.begin(115200); // initializes the Serial port (for real-time inspection) } void loop() { // runs forever ldrVal = analogRead(LDR_SENSE_PIN)/4; int scaledVal = constrain(ldrVal, LDR_MIN, LDR_MAX); // here we 'clamp' all possible sensor readings to the minimum and maximum desired range of the sensor. scaledVal = map(scaledVal, LDR_MIN, LDR_MAX, 0, 255); // next, we 'scale' the incoming sensor readings (between LDR_MIN and LDR_MAX) and map them to a 0-255 range (for setting the LED brightness) analogWrite(LED_PIN, scaledVal); // this writes out a scaled brightness value (from 0-255) to the LED for visual confirmation Serial.println(scaledVal); // we can also write the readings out to the Serial Port – viewable via the Serial Monitor } /* Please note that the code provided here is licensed under the MIT license. The MIT License (MIT) Copyright © 2024 Chuan Khoo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-
COMING SOON…
More code samples for this component for other platforms will be added here over time.