Tutorial:How Much I`ve got Left- The MSP430 Way

Ah.. Battery. The Best thing about MSP430 uC is that one doesnt need to give a second thought as far as powering the controller is considered. This guy accepts almost everything below 4V  (when nicely put and harshly i have used it at 4.2V which  is Voc of a 3.7 Li-ion battery used in Cordless phones in this part of the world). Added to it, one can exploit the low power modes to conserve the battery power.

That all been said..One question still persists in every battery powered device is “How Much I`ve Got?”.  This question has been answered by the designers of the chip by  incorporating an internal voltage sensor into the chip,whats better is that the output can also be read as ADC input which opens a lot of great possibilities of voltage processing to disable certain voltage related features on a product/project.

Now lets get digging . A closer look at the user`s guide page no.-561 :


So, the 11th channel is (Vcc-Vss)/2. Vss is GND or 0V . Hence, What the ADC will return will eventually be  Vcc/2 .

Thats pretty much, now you need to just code and read this value from the internal channel always or upon request. But there still is a catch.. Internal reference is  required because if you choose the default Vcc and Vss reference which will pretty much fall as you use your gadget so measuring the voltage will be a lunatic idea in this case. For the purpose , we have two voltage references in the device i.e. 1.5V and 2.5V. Its obvious that we will choose 2.5V as if Vcc=3.7V , then the ADC output incase of 1.5V ref will be 3.7/2=1.85 .So, the value will ride on 1023 all the time , hence , we choose the 2.5V reference.


However, there seems to be another draw back of 2.5 V reference, which is that at the 2.2V point or voltages below 2.5V , the reference will not be generated. But that can be neglected as most hobby parts dont work at such low voltages. A more robust strategy can be adopted as follows: Whenever the voltage falls below/nears the 2.5V point, we can quickly stop the ADC and change the reference to 1.5V

Further, on design pov , battery status need not be always read, it can be triggered by suppose a button press and indicated by LED flashes this is when you require the minimalist hardware in place for this task.. Of course we can use LCD displays to display exact voltage but thats usually an overkill as Battery is perceived better as a level than a value.

Now lets get started and code the application, i wont cover the idea of switching the threshold/reference as far as code is concerned

The code is available on my git here. This is a threshold one only!

Here is the code to blink the LED based on the voltage levels:

//Coded by: Rohit Gupta on 16th June`13 at 0127hrs


#define SWC BIT3
#define LED BIT0
volatile float voltage;

void blink(int t)
{   int i;
	for (i=0;i<t;i++)


void delay_ms(unsigned int ms ) //Delay_MS
      unsigned int i;
      for (i = 0; i<= ms; i++)
        __delay_cycles(5000); //Built-in function that suspends the execution for 500 cycles
void main()
	P1DIR&=~SWC; // Input
    P1IE|=SWC; // Interrupt Enabled
    P1REN|=SWC; // Internal Pull Ups On // Do not use internal pull ups on Launchpad V1.4
    P1OUT|=SWC; // Yay! Its a pull up!
    P1IFG&=~SWC; //Clear Flag if not set!



#pragma vector=PORT1_VECTOR  //ISR Sub-routine
__interrupt void Port_1(void)
{   unsigned int value=0;
		ADC10CTL0=SREF_1+REFON+REF2_5V+ADC10ON+ADC10SHT_3; // Using internal reference of 2.5V
	    ADC10CTL1=INCH_11+ADC10DIV_3;   // Internal Voltage
	    __delay_cycles(1000);                   // Wait for ADC Ref to settle
	    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
	   while (ADC10CTL1 & BUSY);             //waiting for conversion to finish
	        value = ADC10MEM;                    //fetching adc value
	        voltage=(value*2*2.5)/1023;           // Converting To Legit Voltage Values in Volts

	  else if (voltage>=3.0 && voltage<3.5 )
	  else if (voltage>=2.7 && voltage<3.0)
	  else if (voltage>=2.2 && voltage<2.7)

	  P1IFG&=~SWC;	 //clear flag


The video grab when you run the code and “do the watch expression thing” to see the value of voltage variable is as follows:


PS: This method is applicable only when you are measuring battery directly connected across the power pins of the MSP430 not thru a regulator. For that case, you will need to use the resistor divider+snubber circuit into an ADC Channel externally.

Here is a detailed video from compiling the above code to running it and pressing the button. You can certainly test by bread-boarding the MSP430 and apply different voltages as i did with my bread board power supply


Rohit Gupta

Touchscreen setup

Tiano-MSP430 based TouchScreen Piano

Touchscreens..huh.. They are all around us..Phones ,tablets, phablets and now even laptops. Most of them have a capacitive touchscreen where the change in capacitance at a point is used to sense it and then perform the corresponding action. Capacitive ones are multi point sensing. They sense more than 2 points and hence we get those pinch to zoom features etc.

On the other hand , almost archaic version on touchscreen is the resistive version. That are pretty easy to interface with almost any microcontroller with an ADC. In this project i did over the weekend i tried to interface a touchscreen ( resistive one..) ,available at mobile repair shops in Delhi with a MSP430G2553 (Although MSP430G2231 would also do)  to make a piano.

Touchscreen setup

Pre-requisites: The touchscreen may not be easier to get. I got mine from a mobile repair shop in LR. It is a 4-wire Touchscreen, pretty common with horribly thin contacts that took a lot of patience to solder.There is a lot of literature available on Touchscreens on the internet. I found two app notes that were best. First one from Atmel , that explained the working of the touchsreen in a pretty clear way and one from TI that some what helped in the software part.

You must read the atmel one ( atleast the first few pgs to get how it works). Its essentially a resistive voltage divider. The value of the touch point is determined by the ADC value read.


From the illustration above(borrowed from TI App Note). One can easily see how the co-ordinates are read. In order to find out, what wire on your touch sreen corresponds to X and Y meshes. You can use a multi-meter first to measure the resistance between pins ,usually the alternate pins belong to same axes. in my case it was X-,Y-,X+,Y+  then to differentiate between x and y , one probable method will be to power the touchscreen i.e. giving the drive voltage and then find out the voltage at any other terminal w.r.t. ground and see whether is changing horizontally or vertically in your reference plane.

CAUTION: Be careful about the current while powering the touchscreen externally!

After this coding part is pretty simple. For the algo refer to page no.7 on the TI App Note. If you plan to run your end application on a battery and you have hard-coded the co-ordinates and the Vdrive is sourced externally i suggest that you sample the Vdrive as well . If you are using  microcontroller pins to supply Vdrive then this can be neglected.

I have a desire to learn to play music but unfortunately due to my low patience levels , i have failed to keep up. 😀

So, i made this Piano using the MSP430 Launchpad and a touchscreen along with a speaker. The code algo is pretty same as TI App Note Suggests and i encourage the reader to code it himself . Any difficulties can be discussed. It uses the tone library i posted earlier to generate the solfeggio.

Here is the shaky and horrible video i made:

Cheers! 😀


Tutorial: Using the Internal Temperature Sensor on a MSP430

The value line series (g series) chips that come with the MSP430 Launchpad are pretty cool and has almost all the required peripherals to work with..The best on is ( for those with the newer version v1.5 or later, that comes with pre-soldered Male headers ) MSP430G2553, It has pretty much everything to get you started. Since, I was a little curious i bought the MSP430 launchpad quite a while back..Mine came with a g2231 and g2211..Of which G2231 is the one that has an ADC. The MSP430 boasts of an internal temperature sensor which is probably new to me who has worked earlier on AVR. So, I went on and tested the ” <Accuracy>”  of the sensor.

For you to proceed you need to have this (user`s guide) and refer the pages 559-564.


So setting the bits in the INCHx to 0b1010 (i.e 10 in decimal) the MSP430 allows us to use its internal temperature sensor.

We have chosen the reference voltage to be 1.5V in order to have an ADC Value that is more or less immune to the Voltage Fluctuations.

The code is as follows:

void tempInit()
	ADC10CTL0=SREF_1 + REFON + ADC10ON + ADC10SHT_3 ; //1.5V ref,Ref on,64 clocks for sample
	ADC10CTL1=INCH_10+ ADC10DIV_3; //temp sensor is at 10 and clock/4
int tempOut()
	int t=0;
	__delay_cycles(1000);              //wait 4 ref to settle
	ADC10CTL0 |= ENC + ADC10SC;      //enable conversion and start conversion
	while(ADC10CTL1 & BUSY);         //wait..i am converting..pum..pum..
	t=ADC10MEM;                       //store val in t
	ADC10CTL0&=~ENC;                     //disable adc conv
	return(int) ((t * 27069L - 18169625L) >> 16); //convert and pass
void main(void)
{   volatile int temp;    //initialise
	WDTCTL = WDTPW + WDTHOLD; //stop..bow..boww
	tempInit();//initialise adc
		__delay_cycles(500); //wait and set break point
		temp=tempOut();           //read temp
		__delay_cycles(500);  //wait and set breakpoint


Code is under this licence .  😀

So, when you flash this code using CCS the breakpoints need to be set(Not compulsory et`all). What breakpoints really do is that it stops the program there and allows you to see the value of the watch expression and then when you press the pause/play button again..it will run until the next breakpoint..Its a pretty cool feature..and many use it to analyse code behavior.

This is how you set a breakpoint in the CCS-Edit window:


Just click on the bar next to the line number bar..(Currently highlighted in blue).

These are the results you get by setting the “Watch Expression” to the Temp variable..


The temperature it reads is 23 Degrees Celsius..while my room temperature measured by a standard thermometer is  20 Degrees. which is a relative error of 15%. So, scientifically that`s not accurate..but i dont think that is what it is designed for..I think it is designed for just to give an “Idea” of what the temperature is about..So we can see if we are getting colder or hotter.

The code is pretty well commented and i find it self-explanatory..But still if you have any query ..I am just a “Comment” away!

You can also find the complete CCS project in this repo on github!

Cheers and Greetings!

Indian Tinker!


TI MSP430 Dice( and other experiments)

7 Segmented Displays are pretty low tech and have been around for quite a while.They have a lot of wiring clutter with them..But still they are by far the most easiest(and cheapest) way to give out a (alpha)numerical output..I had interfaced 7segs with MSP430 a while back directly but owing to the low current capabilities of MSP430 the display wasn`t very good and clear . We were recently ‘taught’ about the BCD-7 segmented display in our semester..So i thought about interfacing the 7 segmented with the CD4511..This will save some precious pins and plus drive the 7 seg directly. The CD4511 IC can run from 3V Supply..So no power supply issues and the logic high(input) is about 2V which is also apt as on MSP430 we have this about 3.3V.

The datasheet is available  here

The only thing that was bad was the wiring clutter  has increased and there were (as always) a few loose connections.After a few minutes,I was up and running with the display.

The Fritzing Schematic is given below:


Do everything according to the picture above for the code to run. If the Schematic is not clear..then refer to datasheet linked above.Everything has been done according to it..The LT(bar) and BI(bar) have been connected to +VCC and LE to ground.The display is powered from the ‘board’.

The connections (from the MSP430 end to CD4511) are:

P1.4 -D




#define D BIT4
#define S BIT5
#define B BIT6
#define A BIT7

The code to drive the CD4511 “According” to the above config is:

void CD4511(int numb)
				case 0:P1OUT&=~(B+A+S+D);
				case 1:P1OUT|=(A);
				case 2:P1OUT|=(B);
				case 3:P1OUT|=(A+B);
				case 4:P1OUT|=(S);
				case 5:P1OUT|=(A+S);
				case 6:P1OUT|=(S+B);
				case 7:P1OUT|=(S+B+A);
				case 8:P1OUT|=(D);
				case 9:P1OUT|=(A+D);

Just to clarify :The code uses ‘S’ instead of ‘C’ because C was being used as an internal macro in the header file!

The video is down here..It first shows the simple counter..then the push to change count and then the Dice.
To get the complete source code..you can comment below..The comments are moderated so you email will not be leaked.
Thanks for passing by!
#Sorry for the bad cam! 😛

Connection Diagram!

Tone Library for MSP430

We all like things that sound..About a month ago, A friend of mine asked me to make her a musical card..which she wanted to gift to a friend who is a guitarist( She herself knows Piano). She wanted to dedicate the song she made to her..So, she gave me the tones and durations of each tone..So that i can code it and play the tune..Since, Its all monotonic ..so it didnt felt like a piano..but the tone was clear.

Connection Diagram!

The one I gave her had a MSP430g2211. Just to test the tune settings earlier, I had played Happy Birthday..By borrowing the notes from a website.

The notes were:

c c d c f e
c c d c g f
c c highc a bflat g
a a bflat f g f

Earlier.. I made it using the traditional delay way. But later, I figured out how to do it using the Hardware PWM. The trick is that every note has a particular frequency.If we set the channel for  half the duration and then reset it for the next half. We get a PWM of 50% Duty cycle at the given frequency..Taddaa..Thats all..instead of generating  a sine wave, we are making a square wave.

The exact tested frequency for the notes are available here !

This is the function i coded:

void tone(int tone,int duration)  //Tone macro and duration in millisecs.Play other frq simply divide it (smclk/notef) to get the counter period and feed it into the function
{   int i;
	{ CCR0=tone;       //Tone length
	  CCR1=tone/2;     //Half Tone ON and Half tone OFF i.e 50% duty!

Now another problem ..was that how to stop the Tone..since it(tone generation) is not done using software bit-banging ..I can`t set the Output to Zero just by clearing the bit..but we can definitely make the period zero. This is how i coded the notone function:

void notone(int i)        //To stop the tone can`t use delay_ms as its a hardware PWM not software!

	CCR0=0; //make main period zero..it doesnt work when you set CCR1 to zero because it will always set-reset at zero.

Now we need a toneInit() Function..to initialize the PWM:

void toneInit() //Initialise The Timers

CCTL1 = OUTMOD_7; // CCR1 reset/set

TACTL = TASSEL_2 + MC_1; // SMCLK, up mode

Hope You all like it. If anyone wants the Complete Code..He/She can comment with the email id. The comments are moderated.So don`t worry about your mail ids going public. 🙂
Here`s the video..Wish a geek Happy Birthday!

I know the camera is bad! The tone can be tinkered/tuned a bit..I leave to the reader.. 😛

UPDATE: 30th Nov`12

Pingback: Littlebirdelectronics.com

Update 6th Dec:

You can fork the code from github here.


Deciphering almost any Automotive Combination Switch Assembly

I was recently working on a project that involved using a combination switch assembly used “normally” in the cars for Indicating Directions, Wipers, Headlights and Horns! For those who work on such projects which involves using things which are made for something else to do things that they usually dont do..this might be the post that can get u going and you can Decipher the full console/part in about 10-15 minutes.

Usually the generic way is to check each and every connection using a DMM as a continuity tester and figure out which switch closes which connection..which is very lengthy and often makes one impatient.

Here are a few tips/steps/way outs/notes/whatever:

  1. Open the console(only the outer covering) to see where the wires land up. Never ever open a switch assembly because it might contain some springs or mechanical arrangement that might be difficult to re-assemble.

Rear Assembly “Almost Exploded”

Front Exploded View

2. In many of these Assemblies the switches for a common purpose are base colored the same while to distinguish between them line colors are given. Like in the photo below the green wires are for Indicators wherein the all green wire is Common, the Green-Red               ( Syntax: Base Color-Line Color) is the Left indicator and Green-Yellow is the right indicator. Once you find wires of common base colors ..probe one without any line color to ones with a line color but same base color. and do some mechanical movements..a beep will show the contact made..then carefully document it in a diary/notebook.

Wire Harness Connector

3.When you have still some connections left to decipher ..Follow the following algo:

  • Initiate a mechanical switch and analyse the movement..because it is obvious that somewhere in the world in your hands a contact is being made..it might be that some copper strips be making contact when a plastic bar presses the strip..or a metallic piece moving on a PCB pad.
  • Once you figure where the action takes place..probe the point and There you get the missing links.
  • Now if there are some more wires left..lets follow the principal of exclusion and work backwards..trace the wires back to the point of contact and see what sort of mechanical action produces that kind of a reaction.

PCB Pads and the copper strip for Wiper control and the Green-Red Wire of the Indicator System

Copper Strips and the black contactor that makes and breaks!

Hope you like the post and manage to decipher a few assemblies. Keep me posted on how this helped.

Happy Tinkering!