I just completed the circuit ,attached in the picture below, and firstly it works fine but as I put it in enclosure it starts malfunctioning. It triggers the relay on digital pin 13 automatically as I move my finger close to reset pin of atmega328p. I thought that there was a problem in the soldering, but after making it again on a fresh perfboard the problem persists. I think that the problem is related to reset function of atmega328p, and I don't know much about it. The standalone atmega that I had made is working fine when I tested it with LEDs, they are controllable with IR remote.The code that I had used is:-
#include <IRremote.h>
int RECV_PIN = 11;
int r3 = 13;
int r2 = 12;
int r1 = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(r3,OUTPUT);//Pin For relay3
pinMode(r2,OUTPUT);//Pin For relay2
pinMode(r1,OUTPUT);//Pin For relay1
}
void loop()
{
if (irrecv.decode(&results))
{
if(results.value==551520375)//<--Replace the button 1 decimal value here
{
if(digitalRead(r3)==LOW)
{
digitalWrite(r3,HIGH);
}
else{
digitalWrite(r3,LOW);
}
}
if(results.value==551504055)//<--Replace the button 2 decimal value here
{
if(digitalRead(r2)==LOW)
{
digitalWrite(r2,HIGH);
}
else{
digitalWrite(r2,LOW);
}
}
if(results.value==551536695)//<--Replace the button 3 decimal value here
{
if(digitalRead(r1)==LOW)
{
digitalWrite(r1,HIGH);
}
else{
digitalWrite(r1,LOW);
}
}
if(results.value==551489775)//<--Replace the button 4 decimal value here
{
if(digitalRead(r1)==HIGH)
{
digitalWrite(r1,LOW);
}
if(digitalRead(r2)==HIGH)
{
digitalWrite(r2,LOW);
}
if(digitalRead(r3)==HIGH)
{
digitalWrite(r3,LOW);
}
}
irrecv.resume();
}
}
Is there any problem with my code?? Kindly help me out with this problem. I will be very thankful of yours.
Can't spot any problem with the code as such, but you haven't properly described your system.
What are your two power supplies? You haven't shown the essential 0.1 µF capacitors between VCC and ground on each side of the mega328. If you believe that the reset line is causing trouble, put a 0.1 µF capacitor from pin 1 (the reset, that is,) to ground.
Why are you using PNP transistors on the secondary 5 V supply rather than NPN transistors on the ground side of the relays? Not critical, but just curious and it means the relay supply is confined to be 5 V. Where are the "kickback" diodes across the relay coils?
And you need a capacitor - 470 µF or so - across the relay supply close to the transistors and relay coils. Probably the same on the circuit board for the logic power supply. (With proper design of this, and a proper power supply, you can operate the logic and the relays from the same supply.)
All wiring must be paired - the power supply lines must be in twin cable (ribbon) or twisted together, as must the relay wires and those to the IR sensor, wherever these depart from the board. This is also a reason for keeping the construction of the board reasonably "tight".
Thanks a lot Paul for giving me your time. Yes I didn't used '0.1 µF capacitors between VCC and ground on each side of the mega328' because I made that standalone circuit with the help of 'ArduinoToBreadboard' circuit given on the Arduino tutorial (pic attached below) and there is no capacitor used between Vcc and GND. But I will definitely update my circuit with that. Also I had already tried to put a capacitor between reset and GND, but it doesn't work.
Actually I used NPN transistor only (BC547) in the circuit but when I made a correct transistor as a switch circuit with that, it didn't worked and when I use it like a PNP transistor's circuit(i.e. connecting positive supply to the connector and emmiter) , it worked fine.
Oh ya... sorry I forgot to mention diodes across relays in my circuit diagram, but I had used that.
So, if I use a 470uF capacitor between relay supply and 0.1 uF capacitors between Vcc and GND of atmega, should I able to use the same supply (of atmega) with relays too?
My perfboard circuit is compact i.e. relays are not very far from atmega, is that a problem too? should that interrupt the system.
I am using 5V mobile chargers for the power supplies. Also I will make it clear that the same circuit worked fine when built on breadboard.
Now, I think that maybe the less distance between relay and atmega is interrupting.
Thanks for reply Boffin. Reset pin is connected to +5 v via 10k resistor. Also i want to inform you that when i made the same circuit without relays, connected LED on the respective digital pins. For a minute it worked fine but as i move my finger close to reset pin (not even touched it) it started malfunctioning i.e. LED automatically turn off after a fraction of second as i turn them on through remote, even after some seconds it stop working completely. Hence I am confirmed that the problem is in standalone atmega itself.