{"id":115,"date":"2025-04-05T11:36:15","date_gmt":"2025-04-05T11:36:15","guid":{"rendered":"https:\/\/samerli.com\/en\/lessons\/?p=115"},"modified":"2025-05-05T15:13:47","modified_gmt":"2025-05-05T15:13:47","slug":"using-ldr-with-arduino-for-light-detection","status":"publish","type":"post","link":"https:\/\/samerli.com\/en\/lessons\/?p=115","title":{"rendered":"Using LDRs with Arduino for Light Detection"},"content":{"rendered":"\n<p>Making a light detection circuit is possible using <strong>Light Dependent Resistors (LDRs)<\/strong> with a microcontroller like <strong>Arduino<\/strong>. This lesson shows how to do so using the simple concept of voltage dividers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">List of Content<\/h4>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-1-needed-components\">1. Needed Components<\/a><\/li><li><a href=\"#aioseo-2-connections\">2. Connection and Schematics<\/a><\/li><li><a href=\"#aioseo-3-how-ldrs-work-voltage-divider\">3. How LDRs Work &#x26; Voltage Divider<\/a><\/li><li><a href=\"#aioseo-4-voltage-dividers\">4. Voltage Dividers<\/a><\/li><li><a href=\"#aioseo-5-code\">5. Code<\/a><\/li><li><a href=\"#aioseo-6-advanced-tips\">6. Notes and Advanced Tips<\/a><\/li><\/ul><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-1-needed-components\">1. Needed Components<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Arduino Uno<\/li>\n\n\n\n<li>LDR (Light Dependent Resistor)<\/li>\n\n\n\n<li>10k\u03a9 Resistor<\/li>\n\n\n\n<li>330\u03a9 Resistor<\/li>\n\n\n\n<li>LED<\/li>\n\n\n\n<li>Breadboard<\/li>\n\n\n\n<li>Jumper Wires<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-2-connections\">2. Connections and Schematics<\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"260\" height=\"330\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit.drawio2.png\" alt=\"An LDR connected with 5V on one side and A0 and a grounded 10k Ohm on the other side.\" class=\"wp-image-147\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit.drawio2.png 260w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit.drawio2-236x300.png 236w\" sizes=\"(max-width: 260px) 100vw, 260px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"78\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LED-circuit.drawio1.png\" alt=\"LED connected with GND (cathode) and with pin 9 through a 330 resistor (anode)\" class=\"wp-image-150\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>LDR Voltage Divider Circuit<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Connect LDR to 5V and analog pin A0.<\/li>\n\n\n\n<li>Connect 10k\u03a9 resistor between A0 and GND.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>LED<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Connect anode to digital pin 9 (with 330\u03a9 resistor).<\/li>\n\n\n\n<li>Cathode to GND.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2127\" height=\"1196\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1.jpg\" alt=\"\" class=\"wp-image-155\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1.jpg 2127w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1-300x169.jpg 300w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1-1024x576.jpg 1024w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1-768x432.jpg 768w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1-1536x864.jpg 1536w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/04\/LDR-circuit-picture-edited-1-2048x1152.jpg 2048w\" sizes=\"(max-width: 2127px) 100vw, 2127px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-3-how-ldrs-work-voltage-divider\">3. How LDRs Work &amp; Voltage Divider<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>LDR Principle<\/strong>: A Light dependent Resistor has a resistance that decreases or increases depending on the amount of light it is exposed to. For the LDR in this example, resistance <strong><em>decreases <\/em><\/strong>with <strong><em>increasing <\/em><\/strong>light intensity.<\/li>\n\n\n\n<li>Since the resistance is what changes, we cannot get a measurement directly to Arduino, because Arduino <strong>can only measure and get electrical input (voltage), not resistance. <\/strong>Solution? A <em>voltage divider<\/em> circuit.<br><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-4-voltage-dividers\">4. Voltage Dividers<\/h3>\n\n\n\n<p><strong>Voltage Divider<\/strong>: Convert changes in resistance into voltage changes. <\/p>\n\n\n\n<p>The output voltage (V<sub>out<\/sub>) in a voltage divider circuit is calculated using:<\/p>\n\n\n\n<p>V<sub>out<\/sub> = (R<sub>2<\/sub> \/ (R<sub>1<\/sub> + R<sub>2<\/sub>)) \u00d7 V<sub>cc<\/sub><\/p>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>V<sub>out<\/sub><\/strong> = Output voltage (measured at the junction between R<sub>1<\/sub> and R<sub>2<\/sub>)<\/li>\n\n\n\n<li><strong>R<sub>1<\/sub><\/strong> = Resistance of the first component (LDR in this case)<\/li>\n\n\n\n<li><strong>R<sub>2<\/sub><\/strong> = Fixed resistor value (10k\u03a9 in our circuit)<\/li>\n\n\n\n<li><strong>V<sub>cc<\/sub><\/strong> = Supply voltage (5V from Arduino)<\/li>\n<\/ul>\n\n\n\n<p><strong>How it works with an LDR:<\/strong><\/p>\n\n\n\n<p>As light increases \u2192 LDR resistance (R<sub>1<\/sub>) decreases \u2192 V<sub>out<\/sub> increases<\/p>\n\n\n\n<p>As light decreases \u2192 LDR resistance (R<sub>1<\/sub>) increases \u2192 V<sub>out<\/sub> decreases<\/p>\n\n\n\n<p>Here, LDR acts as R1 (variable resistance).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-5-code\">5. Code<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">const int ldrPin = A0;\nconst int ledPin = 9;\nint threshold = 500; \/\/ Adjust based on ambient light\n\nvoid setup() {\n  pinMode(ledPin, OUTPUT);\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  int ldrValue = analogRead(ldrPin);\n  Serial.print(\"LDR Value: \");\n  Serial.println(ldrValue);\n  \n  if (ldrValue &lt; threshold) {\n    digitalWrite(ledPin, HIGH); \/\/ Turn LED on in darkness\n  } else {\n    digitalWrite(ledPin, LOW);\n  }\n  delay(100);\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-6-advanced-tips\">6. Notes and Advanced Tips<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>While experimenting, you may want to make sure the LED light doesn&#8217;t reach the LDR (otherwise it may give a false light signal and close up the LED)<\/li>\n\n\n\n<li>Try to add potentiometer to adjust threshold dynamically. You should use analogRead() with a large resistor in series to the potentiometer.<\/li>\n\n\n\n<li>Create sunrise\/sunset simulator with PWM, using analogWrite().<\/li>\n<\/ul>\n\n\n\n<p>Now that you learned how to use LDR, you can expand even further by integrating more sensors or adding better functionality.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson explains how to Use Arduino with an Light Dependant Resistor (LDR) for light detection. We will learn how to use a simple voltage divider circuit using a resistor in series with the LDR.<\/p>\n","protected":false},"author":1,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":7,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions\/161"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/media\/155"}],"wp:attachment":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}