This happens all the time to me; In rainy seasons, the daylight will not be sufficient in my room. I can say 60% of the regular light I would get on a sunny day. Even with all the windows and doors opened, I won't get sufficient lighting.
If I turn on the LED bulb, the light is kinda more than enough. I would get a part of sunlight anyway, upon that the LED light will be too much. So, I always wanted to control the brightness of the LED remotely.
This feature is available in advanced home automation technologies. But, we don't have such technology built into our home yet. I had a basic training kit of Bolt IoT. I was familiar with using the Light Intensity Sensor, Temperature Sensor, LEDs and Buzzers in Bolt IoT platform.
I decided to build that system for my final project using the tools which are provided in the basic training kit. I became successful in building the system. The LED turns on if the intensity is below 1000. The brightness of the LED gradually increases as the intensity decreases. If the current room light intensity is below 300, the LED will turn on with maximum brightness.
Firstly, the connections are made using a mini breadboard and Bolt IoT Wi-Fi module. The LED is connected to "1" pin of the Bolt module and the Light Sensor is connected to the "A0" pin. A 10k ohm resistor is connected along with the light intensity sensor to reduce the voltage as the light falling on the sensor decreases.
The whole system is coded in Python using the boltiot module. The Bolt device is connected with the Private API key and Device ID. The configuration for the system is saved in a separate python file called config.py.
intensity = autoLED.analogRead("A0")
intensityData = json.loads(intensity)
For every fetch, the gap of 10 seconds is given. The program automatically fetches the intensity available in the room for every 10 seconds.
global sensor_value
sensor_value = int(intensityData['value'])
print("Data retrieval successful...")
print("Current light intensity = " , sensor_value)
controlLED()
time.sleep(10)
Firstly, the intensity data is read with the function analogRead(). Then the data is processed to a globally declared variable. The same global variable is used in the function controlLED() to set the brightness of the LED according to the processed intensity data.
In the ControlLED() function, there are 3 possibilities.
if(curIntensity > 1000):
LED = autoLED.analogWrite("1", "0")
print("Current LED brightness: 0%")
If the available intensity is more than 1000, it is very much possible that the external light is not required. So, the LED will be kept off.
elif(curIntensity < 350):
LED = autoLED.analogWrite("1", "255")
print("Current LED brightness: 100%")
If the available intensity is below 350, it's understood as the room required maximum light since it's too dark. So, the LED brightness is set to 100%. The LED will be up with its maximum value.
else:
unit = 255/1024
value = 1024 - curIntensity
value *= unit
res = int(value)
LED = autoLED.analogWrite("1", str(res))
brightness = (res/255) * 100
limitedBrightness = round(brightness, 1)
print("Current LED brightness: " , limitedBrightness , "%")
- The third condition is the trickiest. The maximum possible brightness for the LED is having the value 255. The 255 will be broken into 1024 pieces. 1 piece = 1 unit.
- The maximum possible intensity value is 1024. We are subtracting the current intensity from the maximum value in order to obtain the difference. say the current intensity is 800. So, we are subtracting the current intensity that is 800 from the maximum possible intensity 255 which results in the number 224.
- Now, we want to know how much brightness is required for the available intensity. We use a simple mathematical trick to get that. We are multiplying the value digit which is 224 from 1 unit. With this, we get a number which is the brightness value to be passed.
- The value is converted into a whole number by typecasting. Then the value is passed to the
analogWrite()
function along with the port number. - The percentage of brightness is calculated using the formula
(res/255) * 100
. - The percentage is been rounded off for 1 decimal point using the
round()
function. The results are printed.
Comments