WHAT'S NEW?
Loading...
Showing posts with label atmega. Show all posts
Showing posts with label atmega. Show all posts


Wouldn’t it be amazing if the light in a room gets automatically switched on when there are people in the room and automatically gets switched off when the last person leaves the room!! This would save a lot of energy. Especially in bathrooms where we always forget to switch off the lights. Well this is one such projects which used Atmega 168 as micro-controller and 2 LDRs as sensor.

APPARATUS


•    Atmega 168
•    2 LDR
•    4 resistance 2 of 100kohm 2 of 330ohm
•    Relay 12v
•    Transistor 2N222
•    16*2 LCD
•    Potentiometer 10k
•    12V source
•    5v source
•    Light source for the LDRs.
•    2 capacitors 1uF

Circuit Diagram

 LDC pinout diagram:



Code:

AVR C code

main 

lcd header file 

Logic

 

I have used the ADC in free running mode. The value from both the LDRs are read and then converted to binary form depending on whether it is above threshold value (1) or below (0). Depending upon which LDR is cut first, we could find whether the person is entering of leaving the room. If LDR 1 is cut first then I assumed that the person is entering while when LDR 2 is cut first then the person is leaving the room.
How to write this in program? That is the main question which might be eating your brain now, isn’t it? Let me help you out.
Consider the light is falling on the LDR continuously. The value will be below threshold or binary 0. The difference between them will be 0. If have set the value of previous difference to 0 considering there is no person in the room initially. Let the person enter the room. First LDR 1 will be cut which means it will go high(1). Difference will be 1. As long as it is high it will be 1 but if you calculate the difference of difference, it will be 1 only once when the value just changed and then 0. After this both LDR1 and LDR 2 will be high. Third step LDR1 will go low and LDR 2 will still be high. Finally both LDR will be low. If you observe the sequence of non-zero values in the array diff_diff (difference of difference), it follows a regular pattern. Same with the case when going out. Now I have used this result to increase or decrease the counter. When the value of counter is less than 1, MCU sends the command to switch off the light (PC3).


The need of transistor relays and capacitors that I have used had been explained in my last project with lighting automation using sound (clapping). LCD connection is just for fine tuning purpose. You won’t require it once you are done. So you can remove the codes related to it.

More Intelligence you can add

If you want to use it only in night time not in day time. Then you could connect a third LDR and read it at regular interval using interrupt. When you find its value above which you can declare its night or evening, set a flag to 1 and 0 otherwise. In the main loop, put the while content inside another while(1). Change the condition of the inner while to check for flag==1. And you are done.

One another way could be to use a real time clock or even timer to set when to use it and when not to.

Or the simplest way I would say is to connect a switch to live wire in relay. Switch it off in day time and switch it on at night when you want the automation to work for you. Simple as that!


YouTube Link:








Clap to switch the light on and off! This is a very simple atmega168 based project which uses a microphone as a sensor to sense the sound produced through clapping and toggles the output(here the CFL).


Equipment that you will require are:

1.    Atmega 168
2.    Relay 12v
3.    Microphone
4.    Capacitor 1uF
5.    Resistor 3kohm {*depends upon the microphone )
6.    Transistor 2n2222
7.    Resistor -2---330ohm
8.    Wires


Circuit Diagram:




Code:

C code compiled in winavr

Explanation:

Before going to the explanation of the codes, I would like to explain how and why the various resistance used in circuit are chosen.
First of all let us take a look at the microphone. If we connect a multimeter to its ends in resistive mode we will find that its resistance changes on depending on the sound that it receives. If we connect it to a constant resistance and a dc voltage source in series, then we would get a varying voltage at the junction of the resistance and mic. That voltage is fed to the ADC and thus our MCU is able to sense the change in the sound produced (like clapping, whistling, screaming etc.). The value of the resistance should be comparable to the resistance of the microphone.
Now coming to the output. The output pin has 5v output. This voltage is not sufficient for driving a relay of 12v rating. One option is to use a 5v relay but that would increase the power consumption (in mA). Best option is to use a transistor and connect the base terminal to the output pin. This would reduce the power consumption to uA. Also an amplified voltage can be given to the relay for its proper operation. So we have used the transistor. Now important point is why do we require a relay? Appliance we are going to operate works on 220v ac. How do you expect to drive it with just 5v that too dc. Here comes the role of relay. It allows you to switch an ac dc high voltage through small dc voltage.
Finally coming to the logic and the coding.  The analog value of the sensor is calibrated linearly on a 10bit scale. A threshold value is marked depending upon the change in the voltage across the sensor on clapping. Value above the threshold value is set to 1 and below is 0. It is compared with the previous value (difference) which is initially set to 0. Following it, is an if condition tester which tests for the difference greater than 0 and toggles the output pin. This ensures that the value doesn’t toggle when the value returns from high value to low value. Finally the present value of adc is assigned to the previous value and the loop goes on.
* Capacitor in the diagram is in series with the sound sensor as described in its datasheet to reduce the noise.


Video:





The digital clock that we are going to make operates at frequency 4MHz and is programmed using interrupt. It has two buttons as in wrist watches. One of toggling between the hour minute and second value and the other for changing those values.
The PORTC pins have been used as control pins which is used to select which segment will display which data. The PORTD pins have been used as segment data pins which instructs the segment which data to display.





Circuit diagram for digital clock is given below:








Code :

C code for digital clock written in WINAVR 


 

Explanation:
  • The program has been written making use of interrupt. The MCU is clocked at 1Mhz.
  • sei() enables global interrupt.
  • Pulling cs10,cs11  high sets prescaler to 64. This means that the clock counts 1Mhz/64 times ie.
  • 15625  times in 1second.
  • Pulling wgm 12 high sets CTC mode (clear timer on compare). It sets the count to 0 as the set count OCR1A is reached.
  • As OCR1A  value is reached, ISR(interrupt service routine) is called.
The program is written such that it updates the value of a global array(which contains value of hour min and seconds) every second by calling ISR. The while loop is an infinite loop which keeps on updating the value on the segment at a very high frequency(1Mhz). The while loop also contains the condition for checking for the condition of button press which takes it to two different function. One for toggling between the elements of array and the other for changing them.
Video Link: