Introduction: Line Follower Using Msp430g2 Launchpad

About: http://about.me/msminhas93
In this instructable I’ll be writing about line follower bot. This post covers how a light sensor works and how to make your own light sensor. All criticisms are welcome.

Introduction
A line follower bot is as the name suggests a bot that follows a line. Now this line can be either a dark one on a white surface or a white line on a black surface. So once you switch the bot ON it will keep on following the path that you create using the line.

Components required:
> msp430 launchpad or arduino board
> DC motors (2)
> Chassis(preferably with holes) (1)
> L clamp (1)
> Wheels (2)
> Castor Wheel(1)
> L293D IC (1)
> IR sensor (2)
> 9V battery (3-4)
(If you are buying those IR sensors then you do not need the materials that are listed below this note.)
> IR transmitter-receiver pair (the normal LED type) (2)
> npn transistor(2)
> resistors 1k (4)
> General purpose PCB or experimenters board (1/2)
> Soldering Iron
> Solder
I own a blog on msp430 launchpad. If microcontrollers fascinate you then do have a look. 
http://learningmsp430.wordpress.com/

Step 1: Sensors

Theory of Light Sensors
For this we’ll be needing a light sensor which may be based on visible light or infrared. The concept used is that different colors absorb different wavelengths of light and reflect different wavelengths. Have you ever wondered why a book that appears red to you is that colored? The physics behind is that a red material will absorb all other colors of white light and reflect the red color. With that being said it will make sense that black color absorbs all wavelengths or colors while white reflects all colors. We will use this concept to make our sensors.

Components required for making Light Sensors
We can make the sensor using the following combinations.

>LED and LDR
>IR LED and IR diode
What remains common in both the combinations is the use of transistor as a switch. We use transistor as a switch to make the sensor give us digital output i.e. high and low.

The theory on transistor as a switch can be found in the following literatures.

> Electronic Devices and Circuit Theory Book by Louis Nashelsky and Robert Boylestad
> http://www.electronics-tutorials.ws/transistor/tran_4.html
> Electronic Devices 9th edition by Floyd
I’ve made one circuit taking reference from Boylestad and the site whose link I’ve posted above. This circuit will give you ~2V when there is no IR light falling on the IR receiver and will give you ~0V when IR light falls on the receiver. So whenever there is any obstacle near the IR led we’ll get logic 0 and whenever there is no obstacle near the IR led we will get logic 1. Note that you can get ready made IR sensors for this application. I’ve given this basic introduction so that those of you who want to know how to make your own sensor get some guidance.

Circuit


Well with that being taken care let’s develop the logic for our line follower.

Step 2: Line Follower Logic and Concept

Well I’ll be using a sensor that I got after I attended an internship on embedded c and advanced robotics. This sensor gives logic 0 when there is no IR light reflected to it and logic 1 when there is IR light reflection. To keep things simple our track background colour is white and the path is black. So when the sensor is on white background it’ll reflect light and when it is on the path it wont reflect light. Now lets develop our logic. We’ll need two sensors in order for this line follower to work. When there is a straight path both the sensors will point on white surface thus giving logic 1. So when we have this condition satisfied we’ll send data 1010 to the motor driver port pins.(I’ve connected the motors in such a way that 10 corresponds to forward movement and 01 corresponds to backward movement. This is done so that there is no confusion.) Now lets imagine there is a smooth left turn on the path. The left sensor will go on the black path first while the right sensor will be on the white background. Thus l=0 and r=1.(‘l’ corresponds to the left sensor data and ‘r’ corresponds to right sensor data.) To stay on track we need to make a left turn. This can be done in two ways. First is to make the left wheel stop so that the right wheel rotates and the bot takes a left turn. Second is to make left wheel rotate in backward direction and the right wheel move in forward direction. On similar grounds when there is a smooth right turn the bot needs to turn right. This can be achieved by same two methods just the motor data is interchanged. So this is the concept behind a line follower.

Step 3: Motor Driver

A microcontroller can supply limited current, so in order to drive any heavy load requiring high voltage and current we need to connect a motor driver IC between the microcontroller and the load which in this case is a motor. So I’ll be using the driver IC L239D. This IC has four H bridges that will allow us to control two motors using the same IC. The connections are pretty simple. You just need to see the data sheet for the connections. If you find any difficulty just Google how to use that IC you’ll get required information. And you can comment here posting your doubts and I’ll revert as soon as possible.

Step 4: Code in Embedded C

/*
*  line_follower.c
*  Created on : 10-Jan-2014 4:19:47 PM
*  Author    : Manpreet Singh Minhas
*  Website  : http://learningmsp430.wordpress.com/
*/

#include <msp430g2553.h>
// P1IN&BIT0 I've connected the left sensor to P1.0
// P1IN&BIT1 right sensor to P1.1
void main()
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= BIT2|BIT3|BIT4|BIT5;// Make P1.2 P1.3 P1.4 P1.5 as output
P1DIR &= ~BIT0|~BIT1; // Make P1.0 P1.1 as input
P1OUT =0;
for(;;)
{
  if((P1IN&0x03) == 0x03)
  {
   P1OUT |= BIT2|BIT4;
   P1OUT &= ~(BIT3|BIT5);
  }
  if((P1IN&0x03) == 0x02)
  {
   P1OUT |= BIT4;
   P1OUT &= ~(BIT2|BIT3|BIT5);
  }
  if((P1IN&0x03) ==0x01)
  {
   P1OUT |= BIT2;
   P1OUT &= ~(BIT3|BIT5|BIT4 );
  }
}

}

Step 5: Code in Arduino

/*
*  line_follower.ino
*  Created on : 10-Jan-2014 4:19:47 PM
*  Author    : Manpreet Singh Minhas
*  Website  : http://learningmsp430.wordpress.com/
*/
void setup()
{

  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);

}
void loop()
{
  int l = digitalRead(2);
  int r = digitalRead(3);
  if(l==HIGH && r==HIGH)
  {
        digitalWrite(4,HIGH);
        digitalWrite(5,LOW); // 12 and 13 for left motor
        digitalWrite(6,HIGH);// 10 and 11 for right motor
        digitalWrite(7,LOW );
  }

    if(l==LOW && r==HIGH)
  {
        digitalWrite(4,HIGH);
        digitalWrite(5,LOW);
        digitalWrite(6,LOW);  //
        digitalWrite(7,LOW );
  }

    if(l==HIGH && r==LOW)
  {
        digitalWrite(4,LOW);
        digitalWrite(5,LOW);
        digitalWrite(6,HIGH);
        digitalWrite(7,LOW );
  }
}

//Note: You will energia for burning the ‘.ino’ code into launchpad. For arduino people this is as it is.

Step 6: Circuit Diagram

These are the connection that you need to make. I've not shown the sensor diagram. But you have to give the data line of your sensor to the input. If you are using the sensor that you have made on your own just tap the output from the collector of the bjt and give it to your port input.
Please visit http://robokart.com/ for the sensors,chassis and other robotics related parts(for people living in India)

At the end I would like to say thank you for reading this instructable. Hope it was informative. I have a blog on msp430 launchpad where I share my knowledge about the same. Here is the link to my blog:
http://learningmsp430.wordpress.com/