# LED BRIGHTNESS CONTROL USING LDR WITH CLOUDX

319

## Introduction: LED BRIGHTNESS CONTROL USING LDR WITH CLOUDX

The aim of this tutorial is to make a device that will automatically adjust the brightness of an LED according to the brightness of its environment.

The major components needed to achieved this project is the CloudX board and the LDR. An LDR (Light Dependent Resistor) is a device whose resistance varies with the change in amount of light incident on it, the resistance of LDR decreases with the increasing intensity of light and vice versa. The two major operations that happens within this project are the PWM (Pulse Width Modulation) and ADC (Analog to Digital Conversion).

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: COMPONENTS REQUIRED FOR THIS PROJECT

• CloudX M633
• LDR
• LED
• 10K Resistor
• 330R Resistor
• Jumper wire

## Step 2: UNDERSTANDING THE CIRCUIT

From the circuit, we notice that one end of the LDR is connected to ground while the other end is connected in series to a 10K resistor connected to a 5V source. The 5 volts is divided between the 10K resistor and the LDR with respect to each one’s resistance value.

The Voltage across the LDR is read by the CloudX analog pin (0). As we have established, the resistance of LDR decreases with the increasing intensity of light and vice versa, from Ohm’s law we know that the Voltage across the LDR is directly proportional to its resistance i.e. the Voltage read by the ADC reduces as the light in the surrounding increases. The LED is connected to the Microcontroller’s PWM pin (1). The PWM pin controls the brightness of the LED. An oscilloscope is connected to the same PWM pin to help to better portray the rise/fall in the signal on that pin. Note: For those who would like to replicate this project in the real world, a (220 – 330) ohm should be connected to the LED as it requires only a small amount of current to function.Design your circuits carefully... as shown above in the schematic above and if you want to design the project in real world use the same components and their values as shown in the circuit/simulation above and connect them together.

## Step 3: CODE

`#include <CloudX/M633.h>`
`#include <CloudX/Adc.h>`
`#include <CloudX/Pwm.h>`
```int LDRValue;   // create a variable for placing a whole number or integersetup(){           //setup here
Analog_setting();   // Configure and initialize the CloudX Analog Pins for Use
Pwm1_init(5000);    // Configure the CloudX PWM module 1
Pwm1_start();   // Start PWM Module 1 Operation.
Pwm1_duty(0);   // Set PWM duty cycle to 0.loop(){
//Program here
LDRValue = Analog_read(A0);//read analog signal at A0 and store value in LDRValue
LDRValue = ((float)LDRValue/1023) * 100; //convert LDRValue’s value to percentage
Pwm1_duty(LDRValue);    // Sets PWM duty cycle with the value inside LDRValue
}
}```

## Step 4: UNDERSTANDING THE CODE

The code is fairly simple.

The input from LDR is taken at the ADC pin (0) and supplied to the PWM pin(1) OF THE CloudX microcontroller.

We then Initialize the ADC and PWM Modules of the CloudX Microcontroller.

```Analog_setting();   // Configure and initialize the CloudX Analog Pins for UsePwm1_init(5000);    // Configure the CloudX PWM module 1
Pwm1_start();   // Start PWM Module 1 Operation.
Pwm1_duty(0);   // Set PWM duty cycle to 0.```

We store the value gotten fromthe 10-bit (0 to 1023) ADC Module. If the ADC pin reads 5V across the LDR it returns 1023 while it returns 0 at 0V.

`LDRValue = Analog_read(A0);//read analog signal at A0 and store value in LDRValue`

We then convert the value gotten to its value in percentage as the Pwm1_duty() function only accepts values between (0 – 100).We then Load that Value inside the Pwm1_duty() function.

`LDRValue = ((float)LDRValue/1023) * 100; //convert LDRValue’s value to percentagePwm1_duty(LDRValue);    // Sets PWM duty cycle with the value inside LDRValue`

Load the Code into you Microcontroller and Simulate on Proteus as I did or better still replicate this project in real world.
I hope this instructable reaches out effectively to those embedded systems lovers who do not have the necessary tool to build this project in the real world as well as those who are ready to express this idea in the real world, as I made use of all the necessary components irrespective of the allowance the software provides. Here, I use an LED for the simulation, but the same applies even if we connect an actual bulb but you’ll need to use a relay to control it. Hope You enjoyed the Tutorial. If you have any questions or contributions be sure to place them in the comment section.