Category Archive: Line Follower

Feb 19

Line Follower

I made this project a few years ago with some classmates in school.

After we completed our Cruiser Micromouse project, we had a spare sensor pcb to make a line follower. The school had an spare chassis for the same robot so we decided to build one. We designed the main pcb for this robot ourselves. It is based on a PIC16F876A.

The line follower should follow a line. The line is detected by sensors that are mounted on the front of the robot. These sensors emit infra-red light to the surface, the white surface reflects the ir light, while the black line absorbs it. The reflected light is detected by photo-transistors which are connected to the analog inputs of the pic. If the underground is white then the value is smaller than a set value and the line follower should adjust to the line. The 2 outer sensors are not used because the line never gets there. We have put 4 led’s on the pcb for debugging : Led 1 is the power led, led 2 lights up if it turns to the right, led 3 if it drives straight forward and led 4 if it turns to the left

 

Circuit main pcb:

 

Circuit sensor pcb:

 

Video of the Line Follower in action:

 

Code (Picbasic):

'*************************************************
'*Naam   : Lijnvolger                            *           
'*Auteurs: Coenen Stijn, Thomas Rob,van Roy Stijn*
'*Datum  : 18/02/2009                            *             
'*************************************************
Device 16F876A
Config      WDT_OFF, PWRTE_ON, HS_OSC, lvp_off
XTAL        = 20

Declare ADIN_RES 10       ' 10-bit result required 
Declare ADIN_TAD FRC      ' RC OSC chosen 
Declare ADIN_STIME 50     ' Allow 50us sample time 

Dim sensor_waarde As Word

Dim detect_links2 As Bit
Dim detect_links1 As Bit
Dim detect_midden As Bit
Dim detect_rechts1 As Bit
Dim detect_rechts2 As Bit


ADCON1 = %10000010
'
Symbol sensorRU = PORTA.0
Symbol sensorRB = PORTA.1
Symbol sensorM = PORTA.2
Symbol sensorLB = PORTA.3
Symbol sensorLU = PORTA.5
TRISA=1
'
Symbol IR_led1 =PORTB.2
Symbol IR_led2 =PORTB.3
Symbol IR_led3 =PORTB.4
Symbol led4 = PORTB.5
Symbol led3 = PORTB.6
Symbol led2 = PORTB.7
TRISB = 0
'
Symbol motor1A = PORTC.7
Symbol motor1B = PORTC.6
Symbol motor2A = PORTC.5
Symbol motor2B = PORTC.4
TRISC=0
'
Symbol motor_en = PORTC.2
'
Clear
'
HPWM 1,190,1221 
motor1A = 0
motor1B = 0
motor2A = 0
motor2B = 0         
DelayMS 3000
  '**********************************************  
  'afstemmen van IRled's en sensoren.
  'Kijken welke led er wanneer 1 is.
  GoTo over_sub
    sensorinl:
        '*************************************
        '* L2 **** L1 * L0 * R0 * R1 **** R2 *
        '*************************************
        '  A4      A3     A2      A1      A0
        '  15      15     30      20      70
        sensor_waarde = ADIn 0
        If sensor_waarde < 70 Then
            detect_links2 = 1    
        Else
            detect_links2 = 0
        EndIf
        sensor_waarde = ADIn 1
        If sensor_waarde < 15 Then
            detect_links1 = 1 
        Else
            detect_links1 = 0       
        EndIf
        sensor_waarde = ADIn 2
        If sensor_waarde < 30 Then
            detect_midden = 1
        Else
            detect_midden = 0 
        EndIf   
        sensor_waarde = ADIn 3
        If sensor_waarde < 15 Then
            detect_rechts1 = 1    
        Else
            detect_rechts1 = 0
        EndIf
        sensor_waarde = ADIn 4
        If sensor_waarde < 15 Then
            detect_rechts2 = 1 
        Else
            detect_rechts2 = 0       
        EndIf

    Return
  '**********************************************      
  'Vooruit gaan    
    vooruit:
        motor1A = 0
        motor1B = 1
        motor2A = 1
        motor2B = 0 
        led2 = 0
        led3 = 1
        led4 = 0  
    Return
  '**********************************************     
  'een stuk naar links draaien    
    Links_draaien_weinig:
        motor1A = 0
        motor1B = 0
        motor2A = 1
        motor2B = 0
        led2 = 0
        led3 = 0
        led4 = 1
        DelayMS 5
        GoSub vooruit                  
    Return
   '**********************************************     
   'een stuk naar links draaien     
    Rechts_draaien_weinig:
        motor1A = 0
        motor1B = 1
        motor2A = 0
        motor2B = 0
        led2 = 1
        led3 = 0
        led4 = 0
        DelayMS 5
        GoSub vooruit                  
    Return
'**********************************************      
'een stuk naar links draaien   (langere delay)   
    Links_draaien_veel:
        motor1A = 0
        motor1B = 0
        motor2A = 1
        motor2B = 0
        led2 = 0
        led3 = 0
        led4 = 1
        DelayMS 100
        GoSub vooruit                  
    Return
'**********************************************    
'een stuk naar rechts draaien (langere delay)     
    Rechts_draaien_veel:
        motor1A = 0
        motor1B = 1
        motor2A = 0
        motor2B = 0
        led2 = 1
        led3 = 0
        led4 = 0
        DelayMS 100
        GoSub vooruit                  
    Return
'**********************************************   
'alle sensoren op 1 zette
over_sub:
   IR_led1 = 0
   IR_led2 = 0
   IR_led3 = 0
   GoSub vooruit
'**********************************************      
'oneindige lus  
While 1=1
    Main:
'**********************************************      
'kijken welke sensor(en) er 1 is/zijn
    GoSub sensorinl
    If detect_links1 = 1 Then
        GoSub Rechts_draaien_weinig
        GoTo Main
    EndIf       
    If detect_rechts1 = 1 Then
        GoSub Links_draaien_weinig
        GoTo Main
    EndIf     
'**********************************************   
Wend
End

Files

Code: Download