Sunday, 11 August 2013

Automatic Streetlight control using LDR and PIC (Code + Proteus Simulation)

This post will provide a working example of how to control an external high power circuits with relay. I just applied this for controlling a streetlight. This is an automatic digitally controlled streetlight. Let me clear that I used DC bulb and DC battery just for simulation because Proteus has limited resources. You may use AC source and AC bulb with a good high current relay according to your design requirement.

You may download code and Proteus Simulation from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Proteus Simulation on v8.0. This code is written in C Language and will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code.

In this post I supposed that you know following things if not then please go through following posts before proceeding with this.

·         How to Blink an LED. (Click here)
·         How to configure ADC module. (Click here)
·         Digital Lightmeter. (Click here)

Proteus Simulation:

            In the following figure 1. I used a transistor for controlling relay with a diode for protection against reverse current and spikes created by the relay inductor. This relay is acting as a switch for controlling an external circuit for instance I am controlling bulb. LDR interface is same as describe in previous post. In this circuit I just controlling a relay and that ultimately controls the bulb.

Figure 1. Schematic Proteus.

In the following figure 2. Animation shows that when I increase intensity of light on LDR above 94% the bulb turned off and below it is turned ON.

Figure 2. Proteus Simulation.


In the following figure 3. This is main function in which I first configure ADC module with internal reference voltage and channel 0 (AN0) then I configure the PIN for controlling an external switch (relay). And a loop which only calls a function named StreetLight() which is responsible for controlling high power circuit.

Figure 3. Main function Streetlight.

In the following figure 4. We have StreetLight()  which has potential divider formula to find out the resistance changed, due to intensity of light falling on it. Now the second formula is for converting this resistance as a percentage of light intensity. Now I may switch OFF my light when I detect that light intensity is greater than 94% you may change this according to your design requirement.

Figure 4. Streetlight Function.

At some instance you may feel that this post is awkward. As I am just controlling a normal relay with a microcontroller this may be done by OpAmp . So this post is just to explain how to control a relay using microcontroller. You may use this technique in verity of different applications designs. I just tried to make it clear with the help of an example nothing else.


You can download Code (MPLAB X and C30 compiler) and Proteus (v8) Simulation. Click here

Reading Suggestions:

·         Over Voltage Protection
·         Step by step home made PCB tutorial

For all topics Click here:

That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.

All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 
© Copyright 2013 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...