Light Dependent Resistors are a simple and versatile sensor. This month, we will discuss using them with an Arduino or Raspberry Pi as a basic sensor.
Light Dependent Resistors, or LDRs, have been around for many decades and are one of the early semiconductor devices. In the current digital community, they have their detractors and opponents. This is not without reason, and we will discuss the caveats of the LDR. That said, within their limitations, they are a versatile and easy to use sensor that may well just do exactly what you need.
Unlike photodiodes or phototransistors, there is no PN junction in a Light Dependent Resistor. This makes them a passive device, despite being made from semiconductor materials. Two types exist, which are defined by the state of their semiconductors. If the semiconductor material is pure, the device is ‘intrinsic’. The energy level of the incoming radiation (light) needs to be higher to make the device work. Photons hitting intrinsic devices need to make electrons move from the valence band of the substance’s molecules, to the conduction band.
In contrast, ‘extrinsic’ devices have impurities added to the semiconductor, to ‘dope’ the material. The resultant ionic interactions mean there are already electrons between the valence and conduction bands, so the incoming light photons do not need as much energy to make the electrons jump to the conduction band. In either case, once the electrons are moved into the conduction band by light arriving at the semiconductor material, a current can flow. This is why LDRs have a high resistance in the dark, which drops when exposed to light.
The differences between intrinsic and extrinsic devices, and the specific chemical composition (doping level) of each, mean that different devices respond to different wavelengths of light. Some work at the far end of the ultraviolet band, some at the other end of the infrared band, and others somewhere in between. Most extrinsic devices are sensitive in the IR spectrum. Luckily for the maker, none of this really matters. While it is good to be aware of, the retail market does not offer so much choice. Unless you’re in the industry and can buy from business-to-business trade suppliers, you’ll have trouble even finding an extrinsic device, and much of the intrinsic range.
More recently, some of these suppliers, notably Element 14 trading from Australia, have begun selling to non-business customers, but as the operation is still very much trade-based, many makers find them challenging. When it comes to domestic retailers, we have not yet found any LDRs for sale, discretely or in microcontroller add-on modules, that are not the basic, cheap Cadmium Sulphide (CdS) intrinsic devices. These function in the visible light band, the wavelengths of which have enough energy to cause the above process. They are, however, most sensitive in the blue area of the spectrum, and less so in the red end. In other words, they’re sensitive to sunlight and most general purpose artificial light, and their sensitivity broadly follows the wavelength response of the human eye.
A BIT OF A CHALLENGE
There are several materials which can be used for constructing LDRs. Lead Sulphide (PbS), Indium Antimonide (InSb), Cadmium Sulphide (CdS), Lead Selenide (PbSe), and Cadmium Selenide (CdSe). The Cadmium Sulphide (CdS) is the most common in consumer goods and as a retail component. However, Cadmium and lead are both hazardous substances restricted by the European Union’s Reduction of Hazardous Substances legislation (RoHS). Even in countries which do not subscribe to RoHS conventions, these devices do contribute to heavy metal and toxicity contamination in landfill, so care should be taken as much as with any other device when disposing of them. They, and devices containing them, should be treated as e-waste where such services exist.
The common CdS LDR is made in layers. Ultra-pure, finely-powdered Cadmium Sulphide is mixed with adhesive components (binders) which do not alter the chemical properties of the substance. This mixture is deposited on an inert substrate for structure, often a ceramic, and sintered (heating a powder until solidification occurs without complete melting). This is why the cadmium sulphide sometimes appears to be spray-painted on. A more consistent but expensive technique is to press and then sinter the CdS/binder mix into wafers, which are then adhered to the substrate, or encapsulated at the end of the process.
The final step is for electrodes to be deposited onto the surfaces of the semiconductor by a process called ‘vacuum evaporation’. This is an article on its own, so we’ll leave it for now. The electrodes are shaped so that they feature a series of interlocking fingers, which gives the device the characteristic ‘snake’ or ‘s’ pattern that many makers recognise. These electrodes are electrically connected to the leads that protrude slightly above the upper (sensitive) surface of the device, and extend below the substrate. The device is then encapsulated in plastic or under glass to protect the surface from contamination and moisture ingress.
LDRs are, like any component, not without their caveats. Even in constant radiant exposure, (light intensity), LDRs may change resistance with temperature. They are not as sensitive as semiconductors with a PN-junction, like phototransistors or photodiodes. Most importantly, however, they are slow. Called the Resistance Recovery Rate, sometimes listed as ‘Latency’, this is the rate at which the resistance changes after light is applied or removed. Some cells take almost a second to reach their dark resistance after intense light is removed.
The application of light produces a faster response, often around ten milliseconds. This is still slow in the grand scheme of electronics, so you won’t be using an LDR to build a tachometer any time soon. However, this property has its uses. Where trends in light are important, the LDR may filter out flicker which would give unwanted triggering in faster circuits. Some audio compressors use a light globe and LDR as a feedback circuit to avoid jitter, and some guitar effects also exploit what is effectively oscillation caused by the response.
MAKING CIRCUITS WITH LDRs
When it comes to microcontrollers, the LDR is still useful as a sensor despite its lack of precision and speed. If you want to know whether a light is on, then the lack of speed is not really an issue. Many modern fridges, for example, have an alarm to tell you when the door is not fully closed. An LDR and timer could be used to fit this feature to an older fridge. It won’t help you when the door is closed enough to activate the light switch but not enough to seal, but that is only a tiny bit of the door’s travel.
An LDR is also useful as a daylight sensor, or a feedback system for remote control of lighting. Want to know if you’ve left the lights on at home when you go out for dinner? An internet-connected Arduino board or Raspberry Pi can help you with that! Of course, you’ll have to turn around and go home unless you have IoT-connected lights, in which case you can already monitor them. In fact, you can perform light level sensing with an LDR, it just won’t be as precise as with a phototransistor. It will, however, respond more closely to what the human eye expects. LDRs can be used in both a digital and analogue input context.
Before we move on, we’re introducing the two most common LDRs on the retail market. Both look the same at first glance, but they are different sizes. Both are available from a variety of manufacturers, and are sometimes known by the part numbers of the manufacturers who first popularised them. We’re going to refer to them as ‘Type 1’ and ‘Type 2’ for the purposes of this project. We’ll refer to the diameter and common retail supplier part numbers in the tables, so just make sure you look at the curved sides, not the flat sides. The tables below summarise the key data. Note that even between devices that look the same, specifications vary significantly.
ACCURACY OF THE SPECIFICATIONS
During bench testing, we discovered while verifying our numbers, that the LDR examples we had, bought across the retail counter, did not have the same specifications as those in the tables shown here, the source of which is data supplied by the sellers. When we tested our first 10mm Type 2 LDR, we found a light resistance of around 500Ω. This is far lower than the quoted light resistance of between 2.8kΩ to 8.4kΩ. We then checked all five of our samples, and all were around the same. The lowest was 480Ω, the highest was 605Ω. This highlights the variety of these devices available, but as you’ll see, in most maker applications, they’ll work regardless.
Dark resistance is a bit harder to measure, as just putting your finger over the surface still lets plenty of light in. Try it with a multimeter and see for yourself. The answer for us was wrapping them in black PVC insulation tape and pressing the ends of the resultant tube so that there was no light ingress, and the leads were not touching. Suddenly, the dark resistance was much higher than with a finger over the top. In the case of the 10mm Type 2s, all exceeded the resistance range of our multimeter and read open circuit. Covered with a finger, the reading was closer to the number stated for the light resistance. We found around 2.5kΩ in each case.
The same held for the samples of the smaller 5mm Type 1s. The medium light resistance was 4.5kΩ while the data supplied stated 48kΩ to 140kΩ. We found around 55kΩ with a finger over the LDR, and an overload/out-of-range reading when completely covered with black tape.
This demonstrates the variety available in LDRs, and the value of verifying your components first. This is much harder when trying to verify the manufacturer’s data when working with, say, a 4000 series IC, but it’s not hard to make checking with a meter your regular practice for passive and most active discrete components. It also highlights an issue: If you’re working with a design that calls for sensing light levels, rather than simply ‘dark’ and ‘light’, a tunable design is needed. It also means resolution will be lower, as only some of the 0-5V range will be used.
* The Core Electronics component has vastly different light and dark resistance specifications.
** Altronics device 5mm and 1Mohm dark resistance
|1 x Light Dependent Resistor of choice%||RD3485||Z1621A||SEN09088|
|1 x 10kΩ Resistor*||RR0596||R7582||CE05092|
|1 x 4.7kΩ Resistor*||RR0588||R7574||CE05092|
|1 x 100Ω Resistor*||RR0548||R7534||CE05092|
|4 x 5kΩ Trimpots||RT4648||R2380A||CE05105|
|1 x BC549 NPN Transistor||ZT2156||Z1044||COM00521#|
|1 x BC559 PNP Transistor||ZT2168||Z1057||COM00522#|
* Quantity required, may only be sold in packs. You will also need standard prototyping supplies and your chosen microcontroller. % We used the Jaycar RD3485 for our tests, which is the Type 2, and you may need to adapt the 4.7kΩ resisor value for your chosen LDR. # Different devices, check datasheet
THE LDR AS AN ANALOG INPUT
As an analog input, an LDR can be used to detect the light level then use the numerical data to produce a scaled result in the code. A PWM signal, for example. Alternatively, it can detect the light level for a code-based comparator, with the code having a digital high or low response. Using an analog input to feed a code-based comparator isn’t as crazy as it seems to some at first glance. It means you can set your own threshold, rather than being limited to the digital I/O pins’ threshold.
It would be nice if we could just connect the LDR straight to the input, however, the Arduino Uno’s input has an impedance of 100MΩ. Other microcontrollers are designed the same way, so there will not be enough current flowing to create a reasonable voltage drop across the LDR, even in darkness. The device must be connected as a voltage divider, with the LDR and some other resistance functioning together. In most cases, this will be a trimmable resistor, sometimes with a fixed resistor in series. This is particularly useful given the wide resistance tolerance evident in the scant data available for most LDRs, where the resistance when exposed to light has a very large range between samples of the same device.
Normally, for an analog input, we would read any voltage between 0 and 5V. That isn’t going to be the case with the voltage divider using the LDR, but we can get a reasonable approximation. By using a 4.7kΩ fixed resistor and what we’re calling our Type 2 LDR (the larger one), we were able to measure 4.4V at the mid point of our divider (using our workbench LED lighting), and we measured a light resistance of 0.5kΩ. With a finger over the sensor, we had 3.1V. Wrapped with tape, the value was 270mV. This is suitable for a light level sensor which could, for example, data log the daylight hours and give a good approximation of sunrise and sunset as the lower light levels fade into dusk or rise into dawn.
However, for added versatility, we suggest the circuit shown. Instead of a fixed resistor, it has a 5kΩ trimpot. It also has a 5kΩ trimpot on the other side of the LDR. This means that if you want to make a shadow of a finger sensor, you can add resistance to the power rail side of the LDR and therefore make the change happen at a lower voltage. As described previously, without this extra trimpot, the 4.4V to 3.1V drop that occurs with only a finger covering the LDR means that some people may want the change closer to the middle of the 0-5V range for use in their code. The ideal method is to connect a multimeter to the point in the voltage divider that we take our signal from, and simply fiddle with the trimpots until you get the results that fit what you wish to achieve. In this and all the subsequent circuits, trimpots are used so that different LDRs can be used and tuned. After testing and development of your circuit, they could be replaced by fixed resistors.
Although some designs use a transistor in an analog input circuit, for most Arduino and Raspberry Pi applications, this is unnecessary. With a 100MΩ input impedance, there is no reason to buffer the inputs, which can be connected directly to 5V. Nor is there any real need to amplify unless you’re really keen to amplify the limited voltage change into a full 0-5V scale. That’s beyond most makers’ needs and beyond this article as well.
USING AN LDR ON A DIGITAL INPUT
On the other hand, the transistor can be more useful when using the LDR as a digital input. More on that shortly. Initially, however, the LDR can be used in the same circuit with two trimpots as was used in the analog example. This makes use of the digital I/O’s 2.5V threshold, and is mainly suitable for bright light to full darkness switch. To avoid stability issues, it’s better to stay away from the threshold itself. We played with the idea and were able to get a value of 2.8V with a finger over the sensor, 1.8V with three fingers covering the top, sides, and base of the sensor, and 4.4V under workbench lighting. For the sake of it, we covered the LDR all round with black tape and got 4mV (millivolts). The schematic is the same as for the analog example, it is just connected to a digital I/O pin instead of an analog one, and will simply give a HIGH/LOW signal rather than a 1024-bit number.
It is also possible to make a digital input circuit using a transistor. The transistor can be used to make change between light and dark more definite. The transistor is biased so that in low light (but not darkness) the transistor is below saturation and the output is comfortably below the digital I/O’s threshold. With light applied, the transistor reaches saturation and conducts well above the threshold of the I/O. The advantage here is that smaller changes in light can be detected, as darkness does not have to be absolute and light does not have to be bright.
Please note that this is far from the only way of doing this, and many other transistor-based LDR circuits exist. The one presented here worked satisfactorily with our particular LDR and an Arduino Uno.
To use the circuit, connect a multimeter to the output and adjust the trimpots until the transistor behaves as above. Due to the wild tolerances between LDRs noted above, and the gain differences between transistors, we cannot hope to tell you what values your trimpots should be set to. However, ours were 100Ω for RT1, and 4600Ω (4.6k) for RT2. The third trimpot RT3 controls the sensitivity. The diode stops it affecting the voltage divider with the LDR, but gives an additional path to ground besides the base of the transistor. We posted a video to social media showing the effect, and the difference with it removed.
If the position of the fixed resistor and LDR are swapped around, the behaviour of the circuit changes. In the example, we used the Type 2 LDR (the larger one) and a 4.7kΩ resistor, and achieved a dark value of 4.7V and a light value of 55mV. This is the complete opposite of what was above and is one way of achieving hardware inversion. In the circuit with three voltage divider elements, this is largely achievable as-is, because you simply adjust the ratio between the two trimpots accordingly.
The Internet may also tell you to invert the signal with a transistor. Although the previous methods will be fine for many makers, there are ways and reasons to invert with a transistor. Most obviously is if you want to amplify small voltage changes and so use only a small amount of the sensitivity of the device. Doing this looks just like the circuit for the digital I/O, but with a PNP transistor. The trimpots are set to bias the base so that the device is as sensitive or not as you like, but now it will switch on with darkness and off with light.
Of course, because we’re really concentrating on using LDRs with Arduino or Raspberry Pi, you can just do the inversion in code. For many, this is easier to do with the digital I/O, but the algorithm can still be performed to invert the analog signal. There are likely many ways to do this, but subtracting the 1024-bit ADC value from 1024 and using the result instead will have the effect of inverting the input and changing the switch between light-on and dark-on.
While the LDR is adequate for many simple tasks, it’s far from precise. If you need more precision, a photodiode or phototransistor may be the way to go. There are also sensors for other wavelengths like Ultraviolet (UV) or Infrared (IR), although both the photodiode and phototransistor will (usually) sense IR. There are also sensors around, often built into modules for the microcontroller maker, which are calibrated to sense light in units such as lux. Bear in mind that while these sensors may give precise outputs of x mV per unit, the human eye is neither linear nor calibrated in such units.
WHERE TO FROM HERE?
That is entirely up to you. We have presented building blocks, and we’d love to see how you use them. We’d be interested to see someone come up with an Arduino or Raspberry Pi data logger that tracks day length and can predict the sunrise and sunset times of the coming days. Can you build an LDR sensor tuned to give the right reading at sunset each day?