Project: Wireless Car Security System

Hi there

I am working a Car security project which controls Door Locks via 433Mhz remote and GSM mobile. The basic Schematic is attached (Mobile phone SE T610 connections not shown). I am facing problems it running smoothly.

When I attach the door lock motor (shown in the circuit) and latch the relay using programming, the ATmega328 resets and the Door lock motor shakes only. However, when there is no door lock motor attached, the LOCK / UNLOCK relay latches smoothly as I expect from the programming result.

My second question is that when I’ll attach this circuit in the car, would the Atmega328 restarts with start of ignition motor (when you know the battery voltage drops and even car stereo restarts) ? If yes, then this circuit would fail and serve no purpose … I want that it should keep itself intact even with the start of car.

Here is the code:

#include <RCSwitch.h>
String readstring;
char c;
RCSwitch mySwitch = RCSwitch();

byte doorstate = 0;  // 1 == lock, 0 == unlock
byte armed = 3; // 1 ==  armed, 2 == partial unarmed, 3 == full unarmed 
byte keyACC = 0; // 0 == Car Off, 1 == Car Start
byte unlockcounter = 0; 
byte dooropen = 0; // 1 == door open, 0 == door close
byte mdoor = 0; // 1 == mdoor open, 0 == mdoor close
byte reset = 0; 

byte starttimer = 0; 
byte code = 0;
byte cc = 0;
byte alarmstate = 0; // 0 == alarmstate off, 1 == alarmstate ON
#define breaksw 3 // Reset Switch 
#define keyatacc 4 
#define m_door 5
#define all_doors 6
#define hood 7 // or KEY IN 
#define windowbutton 8

#define buzzerpin 9
#define immobilizer 10
#define lights 11
#define siren 12
#define doorpos 13 // INPUT


void setup()  
{
    Serial.begin(9600);
    
    pinMode(breaksw, INPUT); // 
    pinMode(keyatacc, INPUT);  // 
    pinMode(m_door, INPUT); // with diode negative
    pinMode(all_doors, INPUT); // with diode negative
    pinMode(hood, INPUT);
    pinMode(windowbutton, INPUT); 
    pinMode(doorpos, INPUT); 
    
    pinMode(buzzerpin, OUTPUT);
    pinMode(immobilizer, OUTPUT);
    pinMode(siren, OUTPUT); 
    pinMode(lights, OUTPUT); 
    pinMode(A4, OUTPUT);
    pinMode(A5, OUTPUT);
    
    immobilize(0); 
    digitalWrite(lights, LOW);
    digitalWrite(siren, LOW);
    
    mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
/*    
    Set Baud Rate = Mobile Baud Rate
    delay(3000);
    Serial.println("ATE=0");
    delay(1000);
    Serial.println("AT*ERIL=0"); // VOLUMNE RING TO ZERO
    delay(1000);
    Serial.println("AT+CPMS=\"ME\",\"SM\"");
    delay(2000);
    Serial.println("AT+CMGF=1");
    delay(1000);
*/
    buzzer();
}

void loop() // run over and over
{
  unsigned long TimerA; 
  unsigned long pm; unsigned long st; unsigned long breaktime;
  String mobilecmd = 0; 
  readstring = 0;
  TimerA = millis();
  
  if (digitalRead(keyatacc) == LOW) 
    keyACC = 1; 
  else keyACC = 0;
  if (digitalRead(all_doors) == LOW) 
      dooropen = 1; 
    else dooropen = 0; 
  if (digitalRead(m_door) == LOW) mdoor = 1; 
    else mdoor = 0;
      
  if (mySwitch.available()) { 
    processremote();
    mySwitch.resetAvailable();
  }
  
  if (armed == 1 && (dooropen || mdoor))
      alarmon();
  
  if (armed >= 2 && (mdoor || dooropen))
    digitalWrite(lights, HIGH);    
  else
    digitalWrite(lights, LOW); 
}

void doorlock()
{
   if (doorstate ==1) return;
   doorstate = 1;   
//   if (digitalRead(doorpos) == LOW) return; 
   digitalWrite(A5, HIGH);
   delay(2000);
   digitalWrite(A5, LOW);
}

void doorunlock()
{
   if (doorstate == 0) return;
   doorstate = 0; 
//   if (digitalRead(doorpos) == HIGH) return; 
   digitalWrite(A4, HIGH);
   delay(2000);
   digitalWrite(A4, LOW);
}

void lightsflash(byte noftimes)
{
   digitalWrite(lights, HIGH);
   for (int i = 0; i< noftimes; i++) {     
     delay(500);
   }
   digitalWrite(lights, LOW);
}

void processremote()
{
   if (keyACC == 1) return; 
   long int value = mySwitch.getReceivedValue();
   switch (value) {
      case XXXXX: // LOCK BUTTON
          doorlock();
          lightsflash(1);
          if (armed == 1) {  digitalWrite(siren, HIGH);  delay(500); digitalWrite(siren, LOW); }     
          armed = 1; 
          immobilize(0);
          unlockcounter = 0; 
          break; 
      case XXXXX: // UNLOCK BUTTON
          digitalWrite(lights, HIGH);
          alarmoff();
          doorunlock();          
          lightsflash(3);
          immobilize(1);
          armed = 2; 
          unlockcounter = 0; 
          break; 
    default: break; 
   }       
}

void alarmoff()
{
//   immobilize(0);
   digitalWrite(siren, LOW);  
   armed = 2; 
   alarmstate = 0; 
}

void alarmon()
{
   immobilize(1); 
   digitalWrite(siren, HIGH);      
   armed = 1; 
   alarmstate = 1; 
}

void buzzer()
{
     digitalWrite(buzzerpin, HIGH);    
     delay(500);
     digitalWrite(buzzerpin, LOW);
}

void immobilize(byte state)
{
 if (state == 1) 
   digitalWrite(immobilizer, HIGH);
 if (state == 0)
   digitalWrite(immobilizer, LOW);

I think the 4N35 photocoupler needs a resistor to protect the input LED. It should not handle 12v.

It have a resistor of 1k ... i forgot to add in the circuit ... should I increased its value to 2k or more ?

However, i never connected 12V to that LED yet ... I am just testing the Locking motor function at present ...