Laser Alarm System (Cont)

Discussion in 'Embedded Systems and Microcontrollers' started by Shagas, Aug 8, 2013.

  1. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Hello again!

    This is a continuation of the thread I have posted on the Projects Forum section. As I am now concerned about code , I have decided to move it here.
    I posted this same thread on AVR-freaks , but i'm interested to hear the input of the people on AAC.
    I'm trying to make this thing as realiable as possible so any comments are welcome.

    *******


    So i've been working on a simple laser alarm system that I'm going to be installing in my parents house and after some nice suggestions from some people on this forum I have completed my code for it.
    Enclosed I have a Block-diagram of the system in a PNG.

    I am not sure what Micro i'm going to be using , but for now I've programmed the code to suit the Atmega32 which is currently sitting on my breadboard .

    Functionality:

    I am going to be sending an IR laser which will be modulated at 10-40khz (Haven't decided on that yet , suggestions?)
    and recieved by an IR sensor on the other side of the garden. The reciever is going to be fed into a schmitt trigger via ac coupling to get rid of ambient light ( the moon and some street lights which are about 20 meters away )and I should be getting a 0-5V 'square' signal into the AVR INT0 pin.

    The idea is that I have a timer running and everytime the AVR recieves a pulse from the laser ( 40khz ), it resest the timer.
    If the Laser beam is obstructed , then the timer will count up to OCR0 (which will be pre-adjusted by a pot and read by the ADC) and when a compare happens then the alarm will be activated for a preset amount of time (ie 6-10 seconds) and then deactivated .
    The avr will only be sending a logic voltage to the alarm circuit ( which is analog ) and it will turn it on / off .
    There also a few LED's : Laser status , POWER on( from psu) and ALARM led which will flash at about 5hz when the alarm is activated .
    I've also included a manual shutdown button which will be connected to RESET for testing and calibration purposes

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <avr/io.h>
    4. #include <util/delay.h>
    5. #include <avr/interrupt.h>
    6.  
    7. int alarmstatus = 0;
    8. int alarmdelay = 0;
    9. int Flash_Red_Led = 0;
    10. int main(void)
    11. {
    12.    TCCR0 = ( 1 << CS02) | ( 1 << CS01 ) | ( 1 << CS00) | ( 1 << WGM01) | ( 1 << COM01 ) | ( 1 << COM00 ); // setting 1012 prescaler , CTC mode and  OC01 Set on Compare
    13.    
    14.    TIMSK = ( 1 << OCIE0 );  // Enabling Compare interrupts
    15.    
    16.    sei();  // enabling global interrupts
    17.    
    18.    OCR0 = 200 ;  //  setting the delay after which the alarm will sound ( this parameter will adjusted by experimentation )
    19.    
    20.    MCUCR =  ( 1 << ISC11 ) | ( 1 << ISC10);  // setting the Laser input interrupt  
    21.    
    22.    DDRB = ( 1 << PINB2) | ( 1  << PINB0) | ( 1 << PINB1 );  // Setting the outputs:  PINB2 = Alarm activation  / PINB1 =  Alarm status / PINB0 = Laser status
    23.    
    24.    
    25.    
    26.    
    27.    // setting the ADC:
    28.    
    29.    ADMUX = ( 1 << ADLAR) | ( 1 << REFS0);  //  Setting Reference voltage to VCC (+5V)  , Left-shifting ADCH for an 8 bit result
    30.    
    31.    ADCSRA =  ( 1 << ADEN ) | ( 1 << ADPS1 ) | ( 1 << ADPS0);  // setting a prescaler of 8 for a 125khz adc rate  , enabling the ADC
    32.    
    33.        
    34.    
    35.    TCNT0 = 0;  // starting timer
    36.    
    37.    
    38.     while(1)
    39.     {
    40.       ADCSRA |= ( 1 << ADSC)  // start conversion
    41.        
    42.       while (ADCSRA & ( 1 << ADIF)) // waiting for conversion
    43.       OCR0 = ADCH;   // set OCR0 to ADCH
    44.        
    45.         if(Flash_Red_Led == 1) // if alarm is triggered
    46.       {
    47.          PORTB ^= ( 1  << PINB1);  // flash the alarm LED at about 5hz + interrupt delays ??
    48.          _delay_ms(200);
    49.       }
    50.        
    51.     }
    52. }
    53.  
    54. ISR(TIMER0_COMP_vect)   // Enabling the alarm when timer reaches 0CR0
    55. {
    56.    PORTB &= ~( 1 << PINB0); // Turn off Laser status LED signifying that the laser has been broken
    57.    
    58.    Flash_Red_Led = 1;
    59.    
    60.    
    61.    if(alarmstatus == 0)
    62.    {
    63.       PORTB = 1 << PINB2;  // activate alarm
    64.       MCUCR &= ~  ( (  1 << ISC11) | ( 1 << ISC10) )  // disable Laser interrupts
    65.       alarmstatus = 1 ;    
    66.    }
    67.    
    68.    else
    69.    {
    70.       alarmdelay ++ ;  // checking if the alarm has been ringing for a preset amount of time
    71.        
    72.       if(alarmdelay > 700)
    73.       {
    74.          PORTB &= ~( 1 << PINB2); // Deactivate the alarm
    75.          MCUCR |= ( 1 << ISC11 ) | ( 1 << ISC10);  // Turn interrupts back on and resume
    76.          alarmstatus = 0;  // return variables to initial state
    77.          Flash_Red_Led = 0; // return variables to initial state
    78.       }
    79.    }    
    80.    
    81.    
    82. }
    83.  
    84. ISR(INT0_vect)  // Resetting timer with every rising laser pulse
    85. {
    86.    TCNT0 = 0; // reset timer
    87.    PORTB |= ( 1 << PINB0);  // turn on Laser status  LED
    88. }
    89.  
    90.  
    What do you guys think? Will there be any issues?
    I'm thinking of using an attiny85 , I think that it
    has all the necessary requirements for this project.



    Question 0:

    Should I use the internal oscillator or an external 16Mhz XTAL ?
    Can I use timer/adc prescalers when using an external XTAL?

    Thanks in advance for your time and Help

    Tim

    Here is an Updated Block-diagram of the system:
    laseralarm.png
     
    Last edited: Aug 8, 2013
  2. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    There is one thing that comes to mind that would cause an issue , and that is if a lightning strike was to saturate the sensor for long enough that the timer counts up to OCRA then there would be a false alarm .

    For this purpose I have decided to encase the sensor in a cylindrical tubing protruding an inch or so from the sensor .
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    One of the ubiquitous IR receiver modules would be better for this then anything you can craft up yourself. For a buck or two you buy a ton of design experience in eliminating false outputs. They operate looking for about a 40KHz signal so your laser should be fine, and their output goes right into a digital pin.

    Just about any micro that has enough I/O pins can handle this task.

    The internal modules (timer, adc, etc.) don't have a clue what is running the oscillator, so they will all work.

    Using the internal or external oscillator will just vary how accurate the timeout is, which should be a big "don't care."
     
    Shagas likes this.
  4. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Yeah that's what I thought.

    Could you link me an example item please?
    Do you mean something like this?
    http://www.gme.cz/tsop4836-p520-072
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Those work great! I've used those in the past on projects. I assume this is what Ernie suggested...


    These devices are sensitive to a range of frequencies and will not care if you use an internal or external clock source...
     
    Shagas likes this.
  6. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Yeah i'm reading the datasheet and they seem to have some good features like TTL/cmos compatibility and ambient light immunity , Ir filter etc .
    But they do seem constricted to that 36 khz carrier . Are you sure it is tolerant to +- a few khz?
     
  7. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    I'm undecided whether I should use a Linear or an SMPS
    unit . There wouldn't be any issues using an SMPS with a micro am I right? I read that they are noisy , will they affect performance or reliability? Or will it work without any special filtration circuitry?

    Also , I just got an idea of using an IR laser from cd/dvd player. What do you guys think?
     
    Last edited: Aug 9, 2013
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Check out figure 5 in the datasheet.
    Does a CD player use IR? I thought it used a red laser?:confused:

    With such a small load, I can't imagine any performance hit in using switching vs. linear regulators. I've used switching regulators with microcontrollers with no noticeable, adverse effects.
     
  9. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    I think it depends on the cd player although i've seen some with IR in the past if I remember correctly .
    Well I got a few older drives from a guy a few rooms down the hall so i'm going to open them up and see .


    Thanks for the info tshuck, i'll check out the datasheet

    Also , someone on AVR freak suggested that I use an IR LED instead of a laser . Going to try that out
     
    Last edited: Aug 9, 2013
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Really, a LED is only effective if you are not too far away. The laser has the advantage of being a focused beam and is able to travel much father than the non-coherent light of a LED, but again, that depends on application.

    Cost will be more with a laser, but if you already have one, I'd say use it.
     
    Shagas likes this.
  11. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    I managed to salvage a laser from CD /dvd driver .
    It says on the box 'Class 1 laser product' which according to laser safety is a perfectly safe laser to use and even look at staight on. But it also says below that : "class 3B radiation" whose direct exposure according to laser safety regulation is extremely harmfull to the eyes . So which is it???? class 1 or class 3B ?

    I'm guessing 3B .

    I read that there are cd drivers with 10 mw diodes so I'm going to try one of those . I'm having trouble finding an IR laser diode under 100 mw :/


    Also , I'm going to try see if I can focus the IR LED diode with a lens , see if it is reliable at 7-10 meters
     
  12. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...that's how those cheap 99 cent laser pointers do it...
     
  13. Shagas

    Thread Starter Active Member

    May 13, 2013
    802
    74
    Really? I thought they used laser diodes. I'll check it out.
     
Loading...