Light-Dependent Resistors: Arduino LDR Tutorial and Use Cases

게시자

Whether you’re building a robot or configuring a monitor, begin your project by noticing the ambient light in your workspace. This reading may lead you to adjust the brightness of a clock or other display, or even set up an automatic nightlight that senses a darkening room. Light-dependent resistors (LDRs) are the key to building automated lighting. LDRs are useful and inexpensive, making them an excellent choice if you’d like to experiment without breaking the bank. We’ll pair an LDR with an Arduino Uno board to demonstrate how they operate together.

Arduino LDR Tutorial: LDR Pin Diagram

0818_pairing_ldr_fig1

Fig 1: Resistor: 2 kilohms

First, here’s a quick demonstration of how to use an LDR:

  1. Hook up the circuit shown in the above diagram using an Arduino Uno (or adapt it to any dev board with an analog input). The fixed resistor used here is two kilohms, but higher or lower resistors may work better in your situation.
  2. Once you complete the circuit, open Examples/Basics/AnalogReadSerial in the Arduino IDE, and load it onto your Uno.
  3. When you open the serial monitor up at 9600 baud, and you’ll see a steady stream of numbers from 0 to 1023indicating a voltage value of 0-5V scaled to a range of 0-1023flowing down your console window.

LDR Pin Configuration Explained

You may wonder why you’re unable to hook the LDR straight from the +5V pin to the A0 pin. While the LDR’s resistance does change, you don’t have a voltage divider in this setup. Whether resistance is relatively high or low, it’s all “seen” across the LDR component, fixing A0 at +5V. When you add the resistor to ground, it forms a voltage divider. Now the LDR must “spread” the voltage across the two resistors in series, and A0 senses the middle value. The equation at work here is:

VA0 = Vsource (Rfixed / (RLDR + Rfixed).

To see what happens without a voltage divider, pull the ground pin out of the circuit board temporarily. You’ll see the serial monitor number jump up to 1023, indicating that the full 5V of potential is set at that pin.

Arduino LDR Light Control

0818_pairing_ldr_fig2

Fig 2: Left resistor: 2 kilohms, right resistor: 220 ohms

The easiest way to measure voltage is to use a multimeter. The magic of Arduino and other dev boards is that you can program them to react automatically to changing conditions. For example, your LDR and Arduino can adjust the intensity of an LED in response to the ambient lighting in the room. You can build this device in a few steps:

  1. Add an LED and resistor as shown in the diagram below. Add a 220-ohm resistor to limit current output.
  2. Check its functionality with the Basics/Blink example sketch, changing LED_BUILTIN to 3 in the three places where the light flashes.
  3. You can change the perceived intensity of the LED by pulsing the LED very quickly and changing how long it stays on each time it flashesa technique called pulse-width modulation, or PWM.
  4. Load the code found here, a combination of the AnalogReadSerial example sketch used earlier, and another example sketch called Fading, located in the Analog drop-down menu. The code uses the “map” function to tie things together, scaling the 0-1023 LDR sensor input value to 0-255 for PWM output.

Depending on the amount of light that the LDR seesand thus the voltage at A0the LED will brighten or dim as needed. Pairing the LDR with an Arduino Uno helps make sure your LED doesn’t shine too brightly for its environment. This application is just one example of how to use these inexpensive sensors in your workshop.

Jeremy S. Cook is an engineer who has worked in manufacturing automation and writes for a variety of technical publications. Zach Wendt is a mechanical engineer with a background in consumer product development. He works for Arrow Electronics, a major supplier of Arduino products

최신 뉴스

Sorry, your filter selection returned no results.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.