In this guide we go over the best steering servo, enjoy!
The LDR (Light-Dependent Resistor) are resistances whose resistive value (Ohms) varies depending on the light that falls on them. This makes them especially useful when you want to make a project that depends on the ambient light. However, this is not the only utility you can give your LDR with Arduino. LDRs are not only affected by visible light. Its resistive value also varies with infrared and ultraviolet light.
The LDR is usually composed of cadmium sulfide (CdS). The cadmium they contain reacts to light, allowing their electrons to move freely, allowing the passage of current. Thanks to this, a photoresist can pass from several MΩ in a total absence of light, to a few Ohms (typically 100Ω) when it receives direct light, in tenths of a second.
You can think of the behavior of the LDR as in that of a potentiometer that varies depending on the light. The variation of the resistivity of this electronic component makes it possible to use LDR with Arduino in a simple way since, when varying the resistivity in a circuit, there are changes in the voltage and current flowing through it, which will not be difficult to manage with Arduino.
How to use LDR with Arduino
To use LDR with Arduino, the simplest option is to create a voltage divider and analyze with your microcontroller the voltage variations that occur in your circuit.
A voltage divider is a widely used circuit when you want to regulate the voltage that reaches a component, circuit, etc. It is composed of two resistors in series, connected to a voltage source (battery, battery …) and earth. When you connect between the two resistors the element on which you want to obtain a specific voltage, it is enough that you find the values of both resistances
They provide you with that tension.
Note: In this post, you have a classic example of the use of voltage dividers.
The idea, in this case, is that one of those resistances is your LDR. As you can see, using LDR with Arduino is simple. However, depending on where you place your LDR (which of the two resistors of your voltage divider is the photoresist) and the value of the other resistance, your results will vary.
A factor that many people do not take into account when performing a voltage divider is the current. Do not worry, you’ll see later. Being a topic that most people neglect, I wanted to remind you so that it does not happen to you.
As I mentioned before, when making your voltage divider, you have several options:
Greater Light, Greater Voltage
If you connect your LDR with the voltage source, the more light affects your photoresist, the lower the potential difference (voltage drop) that you will have between the source and your Arduino, with which Arduino board will read a higher value.
In the same way, the higher the other resistance (which in this case is connected to ground) the greater the voltage drop that you will have your Arduino and earth. As in ground (GND) you have 0V, and that does not change, the higher the resistance, the higher the voltage value that will reach your plate.
In the case of the current, the approach is similar. If a large amount of light hits your LDR, its resistance will be reduced, and the current that will reach your microcontroller will be greater. If the other resistance (the one that is grounded) is large, it will hinder the passage of the current through it, and it will pass through your Arduino board.
If what I just told you seems a bit confusing, take a look at Ohm’s law. Write it on a piece of paper and start playing with the terms, you’ll see how you clarify right away (if not, remember you can ask what you want at the end of this article).
Greater Light, Less Voltage
If the above explanation has been clear to you, you will quickly understand the operation of this circuit (otherwise, and at the risk of you calling me heavy, ask your questions in the comments section at the end of the post). It’s just the opposite behavior to the previous one, so I do not think it’s necessary that I enter details
Whether you decide to use the first option or the second, you can modify the behavior of your circuit by code, that is, you can connect your LDR to the voltage source and get your system to behave as if you had connected it to ground. This makes the type of circuit you can mount much more flexible when using LDR with Arduino.
Voltage Divider + Potentiometer
A simple improvement to any of the two previous configurations is the incorporation of a potentiometer. Again, you have several options:
• You can replace your resistance (the normal resistance, not the LDR) with a potentiometer so that you could easily modify your system if you are going to change it from one site to another in which the amount of light is different. This would avoid having to modify the code of your Arduino. • If the source with which you feed your voltage divider has a high voltage, it is possible that your Arduino will get too many volts (which could destroy it). Putting a potentiometer between your voltage divider and your Arduino board you can regulate more precisely the voltage that reaches the microcontroller. You can also do this with a standard resistance. However, it can be useful to use a potentiometer if your source is spent, that is if you have a battery or battery that, over time, will reduce the voltage it provides to your circuit. As the battery is spent, you are reducing the resistivity of your potentiometer.
Calibrate LDR Photoresist
When you use LDR with Arduino (in the same way as when you use any component or analog sensor), what you do is measure the voltage that reaches your board and converts it to a digital value.
Most of the Arduino boards work with a voltage range between 0V and 5V (hence you have to ensure that in your circuit the maximum voltage that can reach from your voltage divider to your microcontroller is 5V) and convert the analog value that reaches a digital value between 0 and 1024.
To make this conversion as efficient as possible (which will give you greater precision), you can delimit with code the maximum and minimum light values with which your system will work so that the Voltage / Digital Value conversion improves, that is, between 0 and 1024 there are only probable values of light and you are not dedicating part of those values to amounts of light that your circuit will not work with (for example, it is absurd to dedicate part of the values of the range between 0 and 1024 at light values that correspond to being illuminating your LDR with a flashlight if in practice you will not receive more than sunlight).
I hope it does not cost you too much to understand the code (again, ask any questions). It is designed so that you can easily copy it to your sketch so that, with a few modifications, it is easy for you to use LDR with Arduino efficiently. I have not wanted to put a code for a specific project so that you find it more comfortable to adapt it to your project.
Surely there are many improvements that can be made to this code. If you know any I encourage you to comment at the end of this post.