EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / How to Connect a reed sensor to a PIC24 or any PIC?

How to Connect a reed sensor to a PIC24 or any PIC?

|

Microcontroller › PIC › How to Connect a reed sensor to a PIC24 or any PIC?

  • This topic has 4 replies, 2 voices, and was last updated 11 years, 6 months ago by AJISH ALFRED.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • November 14, 2013 at 7:03 pm #2668
    Pat ondo
    Participant
    I am working on a project where I have to use two reeds sensors in order to get the velocity of a object travelling horizontally. The reed sensors are switches that close or open when they are near of a magnet. I was planing to use the ADC on the PIC but I read somewhere that in my case I do not have to do it because I would not need any voltage values between 0V and 5V. I will only need the minimum voltage and the maximum voltage value. So, I want to know how I can actually connect those sensors to the micro controller. And what can I do to get the time between the two microcontroller when they close or open?

    Thanks

    November 19, 2013 at 5:15 am #10619
    AJISH ALFRED
    Participant

    Hi,

    You can make any IO pin as input and conncet the Reed swicth just like any normal switch.

    November 19, 2013 at 5:13 pm #10631
    Pat ondo
    Participant

    Thank you,

     

    I did it that way but my I am trying to get the time between the two events. I am using timer and timer interrupt. what will be the best way to do it?

    November 22, 2013 at 8:30 am #10636
    Pat ondo
    Participant

    This is the code that I am using to get the time between reed sensor 1 and 2 and reed sensor 2 and 3.

     

     
    #include “p24fj64ga002.h”
    #include <stdio.h>
     
    _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & 
              BKBUG_ON & COE_OFF & ICS_PGx1 & 
              FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 )
     
    _CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD &  
     
    OSCIOFNC_OFF &IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT )
     
    #define XTFREQ 7372800 // On board Crystal frequency
    #define PLLMODE 4 // On Chip PLL setting (Fosc)
    #define FCY (XTFREQ*PLLMODE)/2 // Intruction Cycle Frequency
    #define BAUDRATE 115200
    #define BRGVAL ((FCY/BAUDRATE)/16)-1
     
    int main(void)
    {
                LATA = 0;
                LATB = 0;
     
    //TRISB controls direction for all PortB pins, where 0 -> Output, 1 -> input.
    TRISAbits.TRISA0 = 1;
    TRISAbits.TRISA1 = 1;
    TRISBbits.TRISB0 = 1;
    int time1; int time2;
    TMR1 = 0; TMR2 = 0;
     
    // Setup Timer 1 control register (T1CON) to:
    //     TON           = 1     (start timer)
    //     TCKPS1:TCKPS2 = 00    (set timer prescaler to 1:1)
    //     TCS           = 0     (Fosc/2)
    T1CON = 0x1000;
      T2CON = 0x1000;
    while(1)
    {
    if(TRISAbits.TRISA0 == 1) //Switch on reed 1 opens
    {    
    TMR1 = 0;
    PR1  = 0;
    if(TRISAbits.TRISA1 == 1) //Switch on reed 2 opens
    {
    PR1  = time1;
    }
    }
    if(TRISAbits.TRISA1 == 1)                       //Switch on reed 2 opens
    {
    TMR2 = 0;
    PR2  = 0;
    if(TRISBbits.TRISBO == 1) //Switch on reed 3 Opens 
    {
    PR2  = time2;
    }
    }
     
    }
    }
     
    is it the right way to write it? I want to get the time between three switches. 
    November 22, 2013 at 1:14 pm #10638
    AJISH ALFRED
    Participant

    Hi,

    Make your code to wait till the switch open like the following;

     

    while(1)
    {
    while (TRISAbits.TRISA0 == 0); //wait till Switch on reed 1 opens    
    TMR1 = 0;
    PR1  = 0;
     
    while(TRISAbits.TRISA1 == 0) //wait till Switch on reed 2 opens
    PR1  = time1;
     
     
    while(TRISAbits.TRISA1 == 0) //wait till Switch on reed 2 opens
    TMR2 = 0;
    PR2  = 0;
     
    while(TRISBbits.TRISBO == 0); //wait till Switch on reed 3 Opens 
    PR2  = time2;
     
    }
     
    I hope this may work!!
  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • 100uF bypass Caps? May 19, 2025
  • Fuel Auto Shutoff May 18, 2025
  • Actin group needed for effective PCB software tutorials May 18, 2025
  • how to work on pcbs that are thick May 18, 2025
  • compatible eth ports for laptop May 18, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise