Friday, March 13, 2015

Controlling a 120 VAC lamp

Suppose that instead of a low voltage LED as outlined in my last post we want to control a household lamp?  The microcontrollers we are using are DC devices and in any case they couldn't supply the voltages or current directly that a household lamp requires.

There is an easy solution and in fact the same identical code we used to blink an LED can be used to blink a lamp.  In fact, you could use it to control all sorts of things - not just lamps.  Here is the schematic:




















Bill of materials:
MSP-EXP430F-5529LP or other LaunchPad
PowerSwitch Tail II
Jumpers

Connections:
PowerSwitch Tail II:
Plug             120 VAC Household Receptacle
1: +in           MCU digital pin 2
2: -in            MCU GND
3: Ground    not used

Lamp:
Plug            PowerSwitch Tail II Receptacle

That's it - run the same code as the LED example and if you hooked up the digital pin the same it should work.  A couple of suggestions...

  1. There is an LED on the PowerSwitch Tail II.  Before plugging in the PowerSwitch test it with power only to the MCU.  It will blink the LED on the PowerSwitch and let you know if things on the low voltage side are working.
  2. Don't leave this blinking once per second for an extended period unless you want to burn it out.  The PowerSwitch is an optically isolated mechanical relay and the relay is rated for 100,000 cycles.  That is less than 28 hours at one switch per second.  On the other hand, if you are turning it on and off once a day it is about 137 years.  The video below shows it blinking once a second.
Here is the same code as the LED example, but with the variable names, comments, and delay between blinks changed.

Code:

/*
  Turns a lamp on and off repeatedly using a PowerSwitch Tail II optically isolated relay.
 
  This example code is in the public domain.
*/

const int PSTII = 2;         // note that this is the same as P6_5 on the 5529
                             // Pin 2 should work on most LaunchPads
                             // Connect the pin to "1: +in" on the PowerSwitch
                             // Connect GND to "2: -in" on the PowerSwitch
                             // "3: Ground" on the PowerSwitch is not used
void setup()
{                
  pinMode(PSTII, OUTPUT);    // initialize the digital pin as an output. 
}

void loop()
{
  digitalWrite(PSTII, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(10000);               // wait
  digitalWrite(PSTII, LOW);   // turn the LED off by making the voltage LOW
  delay(10000);               // wait

}



Wednesday, March 11, 2015

Hello World, or blinking an LED

Let's get started on the basics - blinking an LED with the LaunchPad.  Along the way we will discuss some of the differences between Arduino boards and LaunchPads.  I'll be using the MSP-EXP430F5529LP (a favorite of mine with lots of capability at a great price), but will point out differences between it and some of the other LaunchPads.

First, get Energia installed if you haven't already done so.

Here is the schematic:

 
Not much too it really but let's discuss how it differs from Arduino...
  1. The pin connections on the top of the LaunchPad are male instead of female - you are going to need male-female jumpers instead of male-male jumpers to get over to a breadboard.  Note that some LaunchPads do have male on one side of the board and female on the others however which is a plus.
  2. The pin arrangements are different than Arduino.  Keep this in mind!  I like to use the nomenclature shown in the schematic although Energia allows alternate naming and I am going to call it Pin "2" here.  Note that on the board it is labelled P6.5 (uses a period instead of an underscore).  If you do want to use this nomenclature you must use an underscore, i.e. P6_5 or it won't compile. 
  3. The LaunchPads have LEDs on the boards which you can use.  In the case of the 5529 there are two which you can address with the names RED_LED and GREEN_LED.
Bottom line - get familiar with the diagrams for your LaunchPad and make sure your pin declarations are correct!  This caused me way too much consternation when I was getting started.

Note also that LaunchPads for the most part are 3.3 V devices (the MSP430FR5969 is a 1.8 V device).  It isn't going to matter in this case but 3.3 V logic frequently won't work with 5 V chips and 5V input can damage a 3.3V device.  Pay close attention to this!

I'm not going to provide beginner level details on code - there are plenty of other sites for that.  I will provide working code and a bill of materials with links to where you can get selected items.

Bill of materials:
MSP-EXP430F5529LP or other LaunchPad
Breadboard
Male-Female Jumpers
LED
330 ohm (or so) Resistor

Connections:

LED anode      MCU digital pin 2 (P6_5 in this example)
LED cathode   330 ohm resistor and then GND

Code:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
*/

 
const int LED = 2;          // note that this is the same as P6_5 on the 5529
                            // Pin 2 should work on most LaunchPads

void setup()
{                
  pinMode(LED, OUTPUT);     // initialize the digital pin as an output. 
}

void loop()
{
  digitalWrite(LED, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(LED, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}


Why LaunchPad?

Lots of reasons:
And there are many more.  I especially like the power to price ratio of these MCUs and they are easy to embed - see this video for example.

Sites I Frequent

These are sites I frequently visit and can recommend.  Here they are, in the order I more or less discovered them:

Arduino:  The educational / hobbyist / maker movement has benefited greatly from Arduino.  There is a lot of good stuff on their site and the internet is full of good (and not so good) support.  This is a very good place to start if you are new to microcontrollers (but this is a blog about the TI LaunchPad series so let's move on)

SparkFun:  I got started in microcontrollers with a "SparkFun  Inventor's Kit" and SparkFun RedBoard (an Arduino Uno compatible).  They have a large selection of hobbyist material and documentation for their products.

Adafruit:  Great products.  Great documentation.  Great service.  This is a really good place to go for Arduino, Raspberry Pi, and Beaglebone Black.  Many of their products can also be used with the Texas Instrument Launchpads.

Texas Instruments:  The Texas Instrument LaunchPad lineup offers incredible value and capability.  Read more about them here.

Energia:  Energia is a fork of Wiring and Arduino for various Texas Instruments LaunchPads - if you have some experience with the Arduino, this site is going to look familiar.  And the great thing is that moving to Code Composer Studio, TI's full featured development environment, is available if you need the additional capability.

Mouser:  I love these guys.  If you need one 10 cent resistor they will ship it to you.  And they carry an amazing array of products, all with datasheets - including the TI LaunchPad series.

Pololu:  I recently started using Pololu for robot components and have been pleased.  They have some interesting products with good range - especially in motors and motor controllers.

Addicore:  They don't have as large a selection as the companies above but what they do have is great value (free shipping on orders larger than $25 too!).  They are very quick and responsive - the best service I have received.  You will also find some of their items on Amazon (at a slightly higher price).

What this blog is about....

I'm a mechanical engineer, new to microcontrollers.  I started out with Arduino, but one day found myself with a new TI MSP-EXP430G2 LaunchPad and not much idea of what it could do.  What I've found is that in the LaunchPad series there is a board that can do anything Arduino can do and a whole lot more.  Just one problem... the support at the beginner level is not as developed as that for Arduino.

My objective with this blog is to document some of my experiences with the hope it will ease the process for others getting started with the LaunchPad series.  The LaunchPads I tend to use the most are the MSP3200 (for internet) and the MSP-EXP430F5529LP (for general purpose).  In addition, I can see use for the MSP-EXP430G2 where I want to embed a small board into something.  I have a MSP-EXP432P401R now and I look forward to learning a bit more about the real time operating system (RTOS).