• Latest Posts

    Subscribe my Youtube channel for mere videos

    Sunday 26 February 2017

    Ultrasonic Distance Sensor with PIC microcontroller

    Ultrasonic Distance Sensor with
    PIC microcontroller







    Ultrasonic distance sensor uses to measure the object distance without physically contact. This type of sensor can be use in robots, machines, vehicles, distance measuring tools and etc.

    Now we will know that how to work ultrasonic sensor and what will the hardware/circuit required. Ultrasonic sensor has four pins 2 are supply voltage pins, one digital signal output pin and forth pin uses for trigger the sensor to get distance. From microcontroller side only single digital input and single digital output pin would be need to communicate with sensor. You can use any of the controller I/O port pins for this purpose.  








    Ultrasonic sensor works by transmitting an ultrasonic burst and providing an output pulse that corresponds to the time required for the burst echo to return to the sensor. By measuring the echo pulse width, the distance to target can easily be calculated. For more information see sensor manufacturer’s datasheet.


    Software working procedure

    1. First set the “trigger” pin for 10us to start ranging.
    2. Wait until “echo” pin not goes to high.
    3. Start timer and incensement until “echo” pin not goes to low.
    4. When “echo” pin goes to low, stop timer.
    5. Calculate time to distance by given formula.




                      Tm = time (us) per increment of timer register
                     Count = total increased register value
                     Distance (centimeter) = (Tm * Count) / 58.82







    Codes:-

    #include <p18f2520.h>

    #define Echo_pin PORTBbits.RB5
    unsigned int rawDist, b;
    int get_sensor_data(void);
    void delay_10us(void);
    int check_TM(void);


    void main()
    {
              TRISB = 0xF1;
              PORTB = 0x00;

              T1CON = 0x10;
              T3CON = 0x00;

              while(1)
              {
                       get_sensor_data();      // Use this function where you need.
                       for(b=0;b<5;b++) for(b=0;b<65000;b++);   // Delay between next                                                                                                      measurement.             
              }
    }

    int get_sensor_data()
    {
              int data;

              // Active sensor
              PORTBbits.RB6 = 0;
              PORTBbits.RB6 = 1;
              delay_10us();
              PORTBbits.RB6 = 0;

              // Get data from sensor
              while(Echo_pin != 1);
              data = check_TM();    
              return (data < 400) ? data : 0;
    }

    void delay_10us()
    {
              TMR3H = 0xFF;           // 10us delay according to 20Mhz crystal
              TMR3L = 0xCE;
              T3CONbits.TMR3ON = 1;
              while(PIR2bits.TMR3IF == 0);
              T3CONbits.TMR3ON = 0;
              PIR2bits.TMR3IF = 0;
    }

    int check_TM()
    {
              int a;
              if(Echo_pin == 1)                   // Check ECHO pin for high.
              {
                       TMR1H = 0x00;
                       TMR1L = 0x00;
                       PIR1bits.TMR1IF = 0;
                       T1CONbits.TMR1ON = 1;
                       T0CONbits.TMR0ON = 0;     // Timer register value increse in                                                                                         every 400ns         
                       while(Echo_pin != 0);            // Timer register value will increase                                                                                       unilt ECHO pin is high.
                       T1CONbits.TMR1ON = 0;
                       T0CONbits.TMR0ON = 1;
                       a = (TMR1L + (TMR1H * 256));
                       a = (a / 2.5)/58.82;       // 400ns time per increment of timer register.
                       a += 1;
                       return a;              
              }
              else return 0;

    }







    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel