Line Follower using msp430g2 launchpad

In this post 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.

Sensors

Theory

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

Components

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.

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

100120141333

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

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.

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.

Code in Embedded C

/*
 *  line_follower.c
 *  Created on	: 10-Jan-2014 4:19:47 PM
 *  Author	  	: Manpreet Singh Minhas
 *  Website		: https://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 );
		}
	}

}

Code in Arduino

/*
 *  line_follower.ino
 *  Created on	: 10-Jan-2014 4:19:47 PM
 *  Author	  	: Manpreet Singh Minhas
 *  Website		: https://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 );
  }
}

I’ve written the code in both embedded C and arduino. You will energia for burning the ‘.ino’ code.

Circuit Diagram

line_follower_circuit

Video

Please visit http://robokart.com/ for the sensors,chassis and other robotics related parts(for people living in India)

7 thoughts on “Line Follower using msp430g2 launchpad

  1. Hi…can you send me the source code of line follower robot using MSP430G2553 microcontroller so that i can compare with my source code.because m nt getting the accurate output. Please send me the code to my mail id.thank you…..

  2. In the embedded C code, can you explain what the if((P1IN&0x03) == 0x03) statement is? I understand what is going on within the if statement and why you are doing the code within the statement, but I’m new to C and am unsure of what the condition is that we are checking there. My understanding is that it’s checking the IR sensors information, but what exactly are those if statements implying?

    • if((P1IN&0x03) == 0x03) statement is a polling check. What it does is checks the inputs from PORT 1 pin 0 and pin 1. This is because we are using just two light sensors for left and right side of the robot. Thus depending on the values from the sensors, decision shall be taken whether to turn left or right. Kindly refer “Line follower logic and concept” for further details. If you need further explanation feel free to comment again. Hope it helps.

Leave a comment