Friday, January 11, 2013

Quad IR Beam Detector


On the Model Rail Radio Podcast Episode: Model Rail Radio #67: DCC is Not a Solution (to World Peace) [December 8, 2012]

 Terry Terrance introduced his concept on using Texas Instruments MSP430 Launchpads for the development of Open Source / Group Sourced model railroading animation and projects.

 I of course was intrigued by this and just had to throw my hat in the ring and play with this idea.  While I am known to throw myself very deep into projects like this the intention is to develop projects that will be easy for the non technical model railroader to use.  

 I have spent a great chunk of time getting myself familiar to how these little gems work and getting at best a novices understanding on coding in the Code Composer Studio software.  I started playing with the MSP430's in regards to some robotics projects I had pending and moved into developing a project toward Terry's cause.

 My project of choice was to start off making a Quad (4) IR Beam detection project that can be used to detect the presence of trains in four areas.  This would be useful for signaling or triggering other animations.

 Here is a video of my final testing, I would suggest blowing up full screen.

   






 As complicated as this may all appear to those who are not versed with electronics it is certainly not something to be intimidated from.  I am not showing all of these technical details to frighten anyone away, but for the benefit of those who do comprehend it as a whole.

*********************************************************************************************

Moderator's Notes:


In the type of optical detector train detector that Hoffy is describing is best described as an  “Interrupter”.  A light source (lamp, visible or Infrared LED) is aimed across the rails. On the opposite side of the tracks from the light source is some form of photo detector (photovoltaic cell, photo-resistive cell, photodiode or photo transistor). When the train comes along it breaks the beam between the light source and the detector and the train’s presence is detected. Don’t be put off by the technical terms within the parenthesis above; the key concepts are: light source, light detector and breaking the beam.



Figure 1 illustrates the principles. In Fig. 1a the light source shines perpendicularly across the rails and is received at the light detector. The train comes along and breaks the beam, which results in the train being detected. This illustration also points out a shortcoming of the interrupter detector. At each gap between cars, the detector will “drop out” and give a clear indication. This can be addressed in a couple of ways. A delay can be built into the circuit or into the LaunchPad code  to hold the indication for a time to allow the next car to move into position. When the train has finally passed, this delay will time-out and give the clear indication (sounds like a good candidate for customization to me!).

A strictly mechanical method to address the problem is indicated in Fig. 1b. The source/detector pair is set across the rails at an angle such that the largest gap between cars will not expose the detector. Setting up the source/detector pairs in this way avoids any further electronic or software complications.

*********************************************************************************************

 The code is pretty straight forward with not alot of surprises ot complicated processes.  DO NOT COPY AND PASTE THIS CODE FROM THIS POST.  the entire project package is attached below in a zip file. 

/*
* Quad Infrared Detector Version 1.2
* COPYRIGHT © 2013 S.D. "Hoffy" Hofmeister
* http://www.hoffysworld.com
* Provided under a Creative Commons Attribution, Non-Commercial Share Alike,3.0 Unported License
* TARGETED TO MSP430 LANUCHPAD W/MSP430G2553 PROCESSOR
*
* Design Notes:
*
* This code is designed to control 4 IR Sensors and light 4 independent indicator LEDS to signal that an object as been detected.
*
* Distance between Tip ofEmitter and Tip of Detector has only been tested upto 3.5 Inches under incandescant and floroescant
* lighting conditions with no failures.
*
* Circuit Pinout:
* PIN 1.0 = Cathode of IR Receiver #1 > Anode to Ground
* PIN 1.1 = UNASSIGNED - UART
* PIN 1.2 = UNASSIGNED - UART
* PIN 1.3 = Cathode of IR Receiver #2 > Anode to Ground
* PIN 1.4 = Cathode of IR Receiver #3 > Anode to Ground
* PIN 1.5 = Cathode of IR Receiver #4 > Anode to Ground
* PIN 1.6 = Anode of Indicator LED #1 \
* PIN 1.7 = Anode of Indicator LED #2 \ Cathodes to ground
* PIN 2.0 = Anode of Indicator LED #3 /
* PIN 2.1 = Anode of Indicator LED #4 /
* PIN 2.2 = UNASSIGNED
* PIN 2.3 = UNASSIGNED
* PIN 2.4 = UNASSIGNED
* PIN 2.5 = Circuit Power Indicator
*
* PINS 1.1, 1.2, 2.2, 2.3, 2.4 are left open for integration into other projects
*
* Note Anodes for the IR Emitters connect to VCC and Cathodes to Ground
*/


#include   <MSP430 LIBRARY FILE>

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P2DIR |= BIT5; // Circuit Power Indicator
P2OUT |= BIT5; // Used to Trouble Shooting

//Configure IR Detectors

P1DIR &= ~BIT0; // Port 1 P1.0 (IR Detector #1) as inputP1REN |= BIT0; // Enable Port 1 P1.0 (IR Detector #1) pull-up resistor
P1SEL &= ~BIT0; // Select Port 1 P1.0 (IR Detector #1)

P1DIR &= ~BIT3; // Port 1 P1.3 (IR Detector #2) as inputP1REN |= BIT3; // Enable Port 1 P1.3 (IR Detector #2) pull-up resistor
P1SEL &= ~BIT3; // Select Port 1 P1.3 (IR Detector #2)

P1DIR &= ~BIT4; // Port 1 P1.4 (IR Detector #3) as input
P1REN |= BIT4; // Enable Port 1 P1.4 (IR Detector #3) pull-up resistor
P1SEL &= ~BIT4; // Select Port 1 P1.4 (IR Detector #3)

P1DIR &= ~BIT5; // Port 1 P1.5 (IR Detector #4) as input
P1REN |= BIT5; // Enable Port 1 P1.5 (IR Detector #4) pull-up resistor
P1SEL &= ~BIT5; // Select Port 1 P1.5 (IR Detector #4)

//Configure Outputs
P1DIR |= BIT6; // Port 1 P1.6 (Indicator #1) as output
P1OUT &= ~BIT6; // Port 1 P1.6 (Indicator #1) Set to off State

P1DIR |= BIT7; // Port 1 P1.7 (Indicator #1) as output
P1OUT &= ~BIT7; // Port 1 P1.7 (Indicator #1) Set to off State

P2DIR |= BIT0; // Port 2 P2.0 (Indicator #1) as output
P2OUT &= ~BIT0; // Port 1 P2.0 (Indicator #1) Set to off State

P2DIR |= BIT1; // Port 2 P2.1 (Indicator #1) as output
P2OUT &= ~BIT1; // Port 2 P2.1 (Indicator #1) Set to off State

// Let's Get Down to Business
while( 1 ) // While this value remains equal to 1 the code will loop continuously, there is no code written to
// change this state in this particular program.
{

//Detector #1
if( (P1IN & BIT0) > 0) // When IR Detector #1 detects an object breaking the IR Beam
P1OUT |= BIT6; // Set LED Indicator #1 to ON
else // Otherwise
P1OUT &= ~BIT6; // Set LED Indicator #1 to OFF
//END of Detector #1

//Detector #2
if( (P1IN & BIT3) > 0) // When IR Detector #2 detects an object breaking the IR Beam
P1OUT |= BIT7; // Set LED Indicator #2 to ON
else // Otherwise
P1OUT &= ~BIT7; // Set LED Indicator #2 to OFF
//END of Detector #2

//Detector #3
if( (P1IN & BIT4) > 0) // When IR Detector #3 detects an object breaking the IR Beam
P2OUT |= BIT0; // Set LED Indicator #3 to ON
else // Otherwise
P2OUT &= ~BIT0; // Set LED Indicator #3 to OFF
//END of Detector #3

//Detector #4
if( (P1IN & BIT5) > 0) // When IR Detector #4 detects an object breaking the IR Beam
P2OUT |= BIT1; // Set LED Indicator #4 to ON
else // Otherwise
P2OUT &= ~BIT1; // Set LED Indicator #4 to OFF
//End of Detector #4

} End of While

} // END OF MAIN

3 comments:

  1. Cool. So, where do I get the breadboards? Does Radio Shack have them?

    ReplyDelete
    Replies
    1. Yes, Radio Shack has them as do most electronics experimenter's distributors.

      Delete
  2. excellent work dude!!....your presentation is much impressive

    ReplyDelete