Blinking Multiple LED's with MSP430 Energia

Now that you know how to blink one LED, what's the next step?
Blinking multiple LED's!

2012-10-19 10.51.57

This post is a follow up from the previous Post- Blinking one LED found here.
(There is a more in depth presentation about detailed set up if you weren't able to do the exercise before, or you can comment and ask questions!).


Blink Multiple LED's
Level: Beginner
Exercises Covered:
Blink 1 LED
Change Pin on Microcontroller for Output
Add LED's to the Blink LED Program

Theory Lessons:
What are Pins (on a Microcontroller)? 
Parts of an Energia Program-Sketch 



What you will need:

Material Previous? Additional
1 MSP-EXP430G2 Board + the USB Cable Buy Here 1
Computer running version of Energia (to install go here): 1
Breadboard (2 x3 ) or larger like this one 1
5mm LED of any Colour (I chose high intensity green and got mine here) 1 + 6 More for a total of 7
Resistor for LED any value under 1K Ohms - (I chose 560 ohms) and got it from a pack like this 1 +6 More for a total of 7
Jumper Wires (depending on your LaunchPad headers). Mine are M/F here are some similar 2 +7 More for a total of 9

2012-10-19 09.04.41

You really don't need to get a whole lot of extra from the previous post.


Concepts Used:
How to Wire Up a Breadboard
How to Wire a Breadboard with an LED
Using Energia Examples

Concepts to Learn:
Wiring more than 1 LED
microcontroller pin out
changing and modifying a program




Getting Started:

-verify you LaunchPad is working.
Plug your LaunchPad into the computer.

v1.5


**If your LaunchPad is brand new, the LED should blink red and green back and forth.
**If your LaunchPad is from the previous post, then the indicator LED will be lit up (LED2).


Wire up your breadboard.
Connect 1 of the jumper wires from the side of the breadboard you are using - (blue) to the GND terminal on the LaunchPad.
This is providing power the the breadboard.


In the previous post you wired up 1 LED.

2012-10-19 09.28.52


Start with connecting a 560K resistor from the negative column to the row you will be connecting the negative of the LED.
(suggest from the Blue (-) rail one end of the resistor and connect the other end of the resistor to row 3 column c).
Hold up the LED, there is a long leg and a short leg.


borrowed from A Modular Life

orient the LED so the short leg is going into row 3 and the long leg is going to row 2- ( suggest plugging into column e).
Then you will connect the jumper wire. This will be your positive. Connect the jumper wire from the positive of the LED (remember we plugged it into row 2, so you will want to plug the jumper into one of the 4 remaining spots on row 2).

2012-10-19 09.05.43


Normally you would connect the jumper wire to the pin on the LaunchPad, but in this case we're going to leave that last wiring until later.
However, we will use this opportunity to test the LED.

2012-10-19 09.08.07

Take the jumper (with the female end) and touch it to the VCC pin of the LaunchPad. You should see the LED light up.
Keep it disconnected and move to the next LED.



**For help here, look at this tutorial on how to use a breadboard and wire 1 LED by ScienceOnline here


Wire up the rest of the LED's, testing each one of them as you go along (touching the end of the positive jumper to VCC) and verifying that the LED does work.
This is how I wired mine:


LED A
LED B
LED C
LED D
LED E
LED F
LED G
+ or Cathode (longer lead of LED)
e2 e6 e10 e14 e18 e22 e26
- or Anode (shorter LED Lead)
e3 e7 e11 e15 e19 e23 e27
560K Resistor side 1
c3 c7 c11 c16 c19 c23 c27
560 K Resistor side 2
-3 -7 -11 -16 -19 -23 -27
Jumper Wire breadboard side)
a2 a6 a10 a14 a18 a22 a26
Jumper Wire LaunchPad Side
? ? ? ? ? ? ?

blink multiple LED



------------

Exercise1: Blink 1 LED If you did the previous post, you may skip this section
Now that you have the hardware all wired up, you're ready to start to connect the LaunchPad to the wires.
The first is to open up the Blink LED Example and load and execute the program.
If you have it open from the previous post, feel free to use that, but the first few steps may repeat what you have already done.

- Open Energia

Load the Blink LED Program.
This turns on the LED for 1 second then turns it off for one second.
- File--> Examples--> Basic--> Blink LED
- hit the LOAD Button
Screen Shot 2012-10-19 at 4.36.33 PM
-Let the load complete.
-See the LED on the LaunchPad blink for 1 second then off for 1 second.



Exercise2: Change the pin for the LED:
-now go back to Energia and notice the following lines of code.

Screen Shot 2012-10-17 at 10.10.25 AM

This is to blink only 1 LED.
If you notice it, pin 14 is what is called out.
Go through each line of the code and change it to pin 3.

Screen Shot 2012-10-17 at 9.44.45 AM

When you are done, hit LOAD.
-See the LED Turn off.
Now that the LED has turned off, this means that your program has loaded.
Instead of turning Pin 14 on and off... it is now turning on and off Pin 3.
But, you don't have anything connected on Pin 3.

So, Let's wire something to Pin 3, but first, we need to know what are Pins.



THEORY: What are Pins?
When you look at your Energia Sketch, you see that it declares a "Pin"
The Pin that the sketch is referring to is the "Pin" on the Microcontroller.
The microcontroller on the LaunchPad is the MSP430G2553, which has 20 Pins.
It's useful to learn what your microcontroller's "Pin out" is, which you can usually find in the microcontroller's datasheet (page 3 for the top figure).

Screen Shot 2012-10-19 at 5.21.55 PM
The one thing you need to pay attention to is where the numbers are on pin-out. These numbers correspond to each of the device's Pin's. This is what Energia is calling out when it says, Pin 14.


Luckily the LaunchPad also has 20 pins and it aligns nicely with the pin-out on the MSP430G2553 device.
This means that when we refer to Pin 3 on the Microcontroller, you can easily count the header pins to find Pin 3. 

Screen Shot 2012-10-19 at 5.13.22 PM

The LaunchPad's header pins have a "silkscreen" that labels each of the pins to help make it easier for users.
You can use the silk screen to help you remember what each pin does.

**You should already have VCC and GND connected to jumper wires to the + bus (red) of the breadboard and the -  (blue) bus. This means you have Pins 1 and Pins 20 tied to + and -.

Here's a reference:

Screen Shot 2012-10-19 at 5.13.34 PM

This will be particularly important when:
1. want to take the microcontroller off of the LaunchPad and place it on the breadboard
2. have a microcontroller that may have less pins (such as the MSP430G2231 with 14 pins) than what the LaunchPad has.

**For now it's important for you to know that the software (Energia) is talking to the microcontroller, but the physical part (the wires and the breadboard) are talking to the LaunchPad. If you want to move a pin, you will need to make sure to make a change in both, the microcontroller and the wires. **

Here's an amazing reference for the hardware: Energia Hardware

Exercise2.a: Continue to change Pin... Connect LED A to Pin3
Let's get back to our breadboard.
So, we have loaded already a program is making the LED blink for 1 second on and 1 second off for Pin 3.
On the MSP430G2553 Pin 3 is the third pin on the left hand side of the launchpad labeled with a silk screen P1.1.
(You can either count the pins starting from Pin 1 to get to Pin 3, or you can use the silk screen to find P1.1. )
Take the jumper from LED A that isn't connected to anything (the other 6 should still be disconnected since only the + and - buses are connected) and connect it to Pin 3.

2012-10-19 17.11.14



The LED A should begin to flash.

2012-10-20 10.12.20

Now, we are ready to make the rest of the LED's blink.
The hardware (in this case the breadboard) is already wired up. All we need to do is get the program modified and ready to work and then connect the hardware.
So, getting working on the program first...

------

To be able to modify the microcontroller to have more LED's we need to change the program or "sketch" to have the extra pins.
Let's first understand the parts of a sketch.

THEORY: Parts of a "Sketch"(Energia Program)
Go back to the Energia Window and look at how the program is put together.
Screen Shot 2012-10-20 at 10.15.12 AM copy
In general you will declare variables- although this program doesn't have any yet.
Then you need to initialize the pins that are getting used (tell the program what microcontroller pins it should be paying attention to).
Then it runs the program.

So, looking at the Blink LED Program  (above) we can read it to be:
There are no variables.
Set up pin 3 (connected to LED A) to be an output.
When the program runs,
it will do a digitalWrite of pin 3 to be HIGH or "on"
delay for 1000 mili seconds. (keep it on high for 1 second).
Then do a digitalWrite of the same pin (pin3) to be LOW or "off"
and delay for 1000 milliseconds.
then it repeats (the loop).

and modify the program to basically add another LED.
we'll need to Set Up that pin.
Then add it to the program.

Exercise: Add LED's to the Blink LED Program.

modify your program to look like this:
Screen Shot 2012-10-20 at 10.33.54 AM
hook up LED B cable to Pin4.
Now you should have LED A blink for one second, then turn off. Then LED B blinks for one second then turns off.

continue to add the rest of the pins. 

If you wanted to only to have each LED blink for 1 second in sequence you can use this method.
Make sure to connect your LED's to the board to the corresponding pins.


Example Code is attached to the end of this post.
You can either open that code and copy and paste it over your existing program or you can choose to type it out .


LED A
LED B
LED C
LED D
LED E
LED F
LED G
+ or Cathode (longer lead of LED)
e2 e6 e10 e14 e18 e22 e26
- or Anode (shorter LED Lead)
e3 e7 e11 e15 e19 e23 e27
560K Resistor side 1
c3 c7 c11 c16 c19 c23 c27
560 K Resistor side 2
-3 -7 -11 -16 -19 -23 -27
Jumper Wire breadboard side)
a2 a6 a10 a14 a18 a22 a26
Jumper Wire LaunchPad Side
3 4 5 6 7 8 9


Once you have the new program loaded and the LED's wired to the correct pins you should have all seven LED's blinking one at a time and repeating.

Congratulations, you've blinked all seven LED's...
You can stop here and play with the delay times, change the order of the blocks of code (if you put the code in different orders, you'll get different routines)

Now that you can blink them in order, what can you do with it?
Here's a quick one that spells out a team name for Football Sunday :-) (may be hard to see in the beginning).

otherwise, try modifying the code to make the LED's blink out of order!

Screen Shot 2012-10-20 at 12.34.16 PM


Preview: the next post will cover the coding that will cover making more complicated routines.

Multiple_LEDs1_oneafterother.ino