Laser Alarm System (Cont)

Thread Starter

Shagas

Joined May 13, 2013
804
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

Rich (BB code):
#include <avr/io.h> 
#include <util/delay.h> 
#include <avr/interrupt.h> 

int alarmstatus = 0; 
int alarmdelay = 0; 
int Flash_Red_Led = 0; 
int main(void) 
{ 
   TCCR0 = ( 1 << CS02) | ( 1 << CS01 ) | ( 1 << CS00) | ( 1 << WGM01) | ( 1 << COM01 ) | ( 1 << COM00 ); // setting 1012 prescaler , CTC mode and  OC01 Set on Compare 
    
   TIMSK = ( 1 << OCIE0 );  // Enabling Compare interrupts 
    
   sei();  // enabling global interrupts 
    
   OCR0 = 200 ;  //  setting the delay after which the alarm will sound ( this parameter will adjusted by experimentation ) 
    
   MCUCR =  ( 1 << ISC11 ) | ( 1 << ISC10);  // setting the Laser input interrupt  
    
   DDRB = ( 1 << PINB2) | ( 1  << PINB0) | ( 1 << PINB1 );  // Setting the outputs:  PINB2 = Alarm activation  / PINB1 =  Alarm status / PINB0 = Laser status 
    
    
    
    
   // setting the ADC: 
    
   ADMUX = ( 1 << ADLAR) | ( 1 << REFS0);  //  Setting Reference voltage to VCC (+5V)  , Left-shifting ADCH for an 8 bit result 
    
   ADCSRA =  ( 1 << ADEN ) | ( 1 << ADPS1 ) | ( 1 << ADPS0);  // setting a prescaler of 8 for a 125khz adc rate  , enabling the ADC 
    
       
    
   TCNT0 = 0;  // starting timer 
    
    
    while(1) 
    { 
      ADCSRA |= ( 1 << ADSC)  // start conversion 
       
      while (ADCSRA & ( 1 << ADIF)) // waiting for conversion 
      OCR0 = ADCH;   // set OCR0 to ADCH 
       
        if(Flash_Red_Led == 1) // if alarm is triggered 
      { 
         PORTB ^= ( 1  << PINB1);  // flash the alarm LED at about 5hz + interrupt delays ?? 
         _delay_ms(200); 
      } 
       
    } 
} 

ISR(TIMER0_COMP_vect)   // Enabling the alarm when timer reaches 0CR0 
{ 
   PORTB &= ~( 1 << PINB0); // Turn off Laser status LED signifying that the laser has been broken 
    
   Flash_Red_Led = 1; 
    
    
   if(alarmstatus == 0) 
   { 
      PORTB = 1 << PINB2;  // activate alarm 
      MCUCR &= ~  ( (  1 << ISC11) | ( 1 << ISC10) )  // disable Laser interrupts 
      alarmstatus = 1 ;    
   } 
    
   else 
   { 
      alarmdelay ++ ;  // checking if the alarm has been ringing for a preset amount of time 
       
      if(alarmdelay > 700) 
      { 
         PORTB &= ~( 1 << PINB2); // Deactivate the alarm 
         MCUCR |= ( 1 << ISC11 ) | ( 1 << ISC10);  // Turn interrupts back on and resume 
         alarmstatus = 0;  // return variables to initial state 
         Flash_Red_Led = 0; // return variables to initial state 
      } 
   }    
    
    
} 

ISR(INT0_vect)  // Resetting timer with every rising laser pulse 
{ 
   TCNT0 = 0; // reset timer 
   PORTB |= ( 1 << PINB0);  // turn on Laser status  LED 
}
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:

Thread Starter

Shagas

Joined May 13, 2013
804
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 .
 

ErnieM

Joined Apr 24, 2011
8,377
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.

Question 0:

Should I use the internal oscillator or an external 16Mhz XTAL ?
Can I use timer/adc prescalers when using an external XTAL?
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."
 

Thread Starter

Shagas

Joined May 13, 2013
804
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."
Yeah that's what I thought.

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.
Could you link me an example item please?
Do you mean something like this?
http://www.gme.cz/tsop4836-p520-072
 

Thread Starter

Shagas

Joined May 13, 2013
804
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?
 

Thread Starter

Shagas

Joined May 13, 2013
804
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:

tshuck

Joined Oct 18, 2012
3,534
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?
Check out figure 5 in the datasheet.
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?
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.
 

Thread Starter

Shagas

Joined May 13, 2013
804
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:

tshuck

Joined Oct 18, 2012
3,534
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
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.
 

Thread Starter

Shagas

Joined May 13, 2013
804
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.
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
 
Top