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.


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.



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.


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.



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		:

#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;
		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		:
void setup()


void loop()
  int l = digitalRead(2);
  int r = digitalRead(3);
  if(l==HIGH && r==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(6,LOW);  // 
        digitalWrite(7,LOW );

    if(l==HIGH && r==LOW)
        digitalWrite(7,LOW );

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

Circuit Diagram



Please visit for the sensors,chassis and other robotics related parts(for people living in India)


Thermometer using LM35

Hello there in this post I’ll be talking about how to interface a temperature sensor to msp430 launchpad. So we’ll be requiring a temperature sensor for this project. I am using LM35 IC. Well as I always say that one needs the datasheet of the IC he is using. Here is the link to the datasheet. The LM35 series are precision integrated-circuit Calibrated Directly in ° Celsius (Centigrade) temperature sensors, with an output voltage linearly proportional to the Centigrade temperature. Thus the LM35 has an advantage over linear temperature sensors calibrated in ° Kelvin, as the user is not required to subtract a large constant voltage from the output to obtain convenient Centigrade scaling.

• Linear + 10 mV/°C Scale Factor

• 0.5°C Ensured Accuracy (at +25°C)

• Rated for Full −55°C to +150°C Range

• Suitable for Remote Applications

These are few features of this sensor. The pin out is given below.


Please take care while connecting the +Vs and GND. The given view is bottom view. So just hold the IC properly to identify the terminals. I connected them thinking that this was top view(idiotic on my part), but still so that you don’t make the same mistake I am sharing this.

The connection diagram is given below.


We have a voltage reading available from lm35. So we use the inbuilt 10 bit ADC of msp430g2553. Using this ADC we can get a digital value for the analog voltage. Now we need to find the relation between this digital value and the centigrade scale. I’ve chosen 3.6V as the Vref, so 1024 corresponds to 3.6V. Now the scale is  10 mV/°C, so 3.6V corresponds to 360°C.


So we need to multiply the digital reading by 0.35, but since there is no inbuilt multiplication hardware we multiply by 35 then divide by 100 to avoid floating multiplication. Now all that is left is to display this reading on an LCD.

I’ve started using Energia recently so I’ll posting its code here as well. Regarding the ADC initialization I’ll be writing a separate tutorial for that. (Note the LCD header file used has a new function send_integer to send integer values.)

The circuit:
LCD pin              Connect to
01 – GND             GND, pot
02 – VCC             +5V, pot
03 – Contrast        Pot wiper
04 – RS               P1.4
05 – R/W              P1.5
06 – EN               P1.6
07 – DB0             GND
08 – DB1             GND
09 – DB2             GND
10 – DB3             GND
11 – DB4             P2.0
12 – DB5             P2.1
13 – DB6             P2.2
14 – DB7             P2.3
15 – BL+             +5V
16 – BL-             GND

Temperature sensor output goes to P1.0 (A0) pin.

CCS Code

*  temperature_sensor.c
*  Created on     : 01-Jan-2014 1:26:21 PM
*  Author         : Manpreet Singh Minhas
*  Website        :
#include "lcd.h"
void main()

WDTCTL = WDTPW|WDTHOLD; //Stop watchdog timer
lcd_init();// Initialize the LCD
ADC10CTL0 |= ADC10ON;//ADC setup
ADC10AE0  |= BIT0|BIT1;
TA0CCR0 = 0x1000;
TA0CTL = TASSEL_1|ID_3|MC_1|TAIE;// Timer setup
_BIS_SR(LPM3_bits + GIE);// Enter Low power mode

#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A(void)
int temp = (ADC10MEM*35)/100;

lcd.h code

// Author : Manpreet Singh Minhas
// This file is for 4 bit mode LCD interfacing with msp430g2553 chip
// 16x2 LCD is used   
#include <msp430g2553.h>
#define DR     P1OUT = P1OUT | BIT4  // define RS high
#define CWR        P1OUT = P1OUT &(~BIT4)// define RS low
#define READ       P1OUT = P1OUT | BIT5  // define Read signal R/W = 1 for reading
#define WRITE      P1OUT = P1OUT &(~BIT5)// define Write signal R/W = 0 for writing
#define ENABLE_HIGH     P1OUT = P1OUT | BIT6  // define Enable high signal
#define ENABLE_LOW      P1OUT = P1OUT &(~BIT6)// define Enable Low signal
unsigned int i;
unsigned int j;
void delay(unsigned int k)
        for(i=0;i<100;i++); } } void data_write(void) { ENABLE_HIGH; delay(2); ENABLE_LOW; } void data_read(void) { ENABLE_LOW; delay(2); ENABLE_HIGH; } void check_busy(void) { P2DIR &= ~(BIT3); // make P2.3 as input while((P2IN&BIT3)==1) { data_read(); } P2DIR |= BIT3; // make P2.3 as output } void send_command(unsigned char cmd) { check_busy(); WRITE; CWR; P2OUT = (P2OUT & 0xF0)|((cmd>>4) & 0x0F);   // send higher nibble
        data_write();                              // give enable trigger
        P2OUT = (P2OUT & 0xF0)|(cmd & 0x0F);      // send lower nibble
        data_write();                            // give enable trigger


void send_data(unsigned char data)
        P2OUT = (P2OUT & 0xF0)|((data>>4) & 0x0F);    // send higher nibble
        data_write();                                // give enable trigger
        P2OUT = (P2OUT & 0xF0)|(data & 0x0F);       // send lower nibble
        data_write();                              // give enable trigger

void send_string(char *s)
void send_integer(int a)
        int temp;
        int rev=0;
        int dummy =a;
         while (dummy)
              rev = rev * 10;
              rev = rev + dummy%10;
              dummy = dummy/10;
            rev /=10;
void lcd_init(void)
        P2DIR |= 0xFF;
        P1DIR |= BIT4|BIT5|BIT6;
        P2OUT &= 0x00;
        send_command(0x28); // 4 bit mode
        send_command(0x0E); // clear the screen
        send_command(0x01); // display on cursor on
        send_command(0x06); // increment cursor
        send_command(0x80); // row 1 column 1

Energia Code

#include LiquidCrystal.h;
*  temperature_sensor.c
*  Created on    : 01-Jan-2014
*  Author          : Manpreet Singh Minhas
*  Website        :

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5);
byte degree[8] = {

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.createChar(1, degree);

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:

int temperature = analogRead(A0);
int temp = (temperature*35)/100;