Here is an example of how to control the speed of a DC motor using Arduino Nano and LDR(Light Dependent Resistor). It is illustrated how the speed of the DC motor automatically increases and decreases according to the light intensity around the LDR. Thus, it can be used to physically indicate the amount of light.
LDR(Light Dependent Resistor)
An LDR(Light Dependent Resistor) is shown below.
An LDR is a sensor which changes its resistance according to the
intensity of light falling on it. It has high resistance when there is low light intensity incident on its surface and low resistance when there is high light intensity incident on its surface. In the dark, its resistance is in order of several MOhm and in presence of light it has several hundreds of Ohms. So the LDR resistance is inversely proportional to light intensity.
\(R_{LDR} = \frac{1}{I}\)
Hence it is often used in light sensor and LDR Arduino based alarm applications.
Arduino Nano & LDR controlled DC Motor Circuit diagram
The following picture shows connection of Arduino Nano, LDR and the DC motor that is to be controlled.
In the circuit diagram above, the LDR and resistor R2 or 4.7KOhm is connected as voltage divider and the junction of this is connected to analog pin A2 of Arduino Nano. The DC motor is connected via the 2N2222 transistor and 1KOhm current limiting resistor to the pin 10 of Arduino Nano. The DC motor is connected to the collector as load with 1N4007 protection diode and 0.1uF capacitor. The emitter is grounded.
When the light intensity falling onto the LDR increases, the resistance at the junction of the voltage divider will increase and hence the voltage measured by the Arduino Nano by A0 pin will increase. Arduino Nano is programmed to map the voltage range to duty cycle range. Using Arduino Nano PWM signal corresponding to the current read voltage at A0 is sent out of the pin 10 and the motor will rotate according to the power it is receiving. +12V is applied to the motor but the PWM duty cycle will determine how long the transistor is turned on and hence how much of the 12V the DC motor will receive.
Arduino Code for LDR controlled DC motor
The Arduino code for LDR controlled DCT motor is below.
const int ldr = A2;
const int motorPin = 10;
void setup () {
pinMode(motorPin, OUTPUT);
}
void loop() {
int res = analogRead(ldr);
res = map(res, 0, 1023, 0, 255);
analogWrite(motorPin, res);
delay(2);
}
In the above Arduino Nano program code we have two constant for the LDR pin which is A2 and for the motor pin which is pin 10. In the setup() function we have made the motorPin as an output pin. In the loop() function re continously read the analog value from the ldr pin and store it in variable res. We then map the acquired res value to the PWM value range(0 to 255). Then we use the analogWrite function to send out PWM value stored in res variable to the motorPin 10.
Arduino Nano & LDR controlled DC Motor Animation Video
The following video demonstrates with animation how the DC motor is controlled using the LDR and Arduino Nano.
So here in this tutorial we have shown how to make a DC motor speed control circuit using LDR and Arduino Nano. Another application of LDR is shown in the tutorial laser based alarm system.
