Showing posts with label LED. Show all posts
Showing posts with label LED. Show all posts

Friday, June 26, 2015

RGB Strip LED

For my sound level project I am considering using inexpensive RGB Strip LEDs.  So to try some code out I did a trial from this Adafruit post:
https://learn.adafruit.com/rgb-led-strips/overview

The differences are:

* used Adafruit's circuit for the TIP120 except except that I used 5mm LEDs to stand in for the strip (also put a resistor in front of each LED so it wouldn't burn out)
* used Adafruit's sketch except I substituted pins 11, 12, and 13 for the Arduino PWM (used a MSP430G2553)
* used a 4.5V battery pack for external power to the LEDs

It is working:



For my project I am thinking I'll need at least 2 strips running 2 different colors so 6 TIP120s.  I only have 5.  Even better would be to have 3 strips and 3 colors - then I'll have the added complication of not enough PWM pins.  It looks like there is another order in my future.

Friday, April 3, 2015

Analog Potentiometer

Since I posted some code on a digital potentiometer the other day, I thought I'd go ahead and do the analog.  There is a lot more information on analog potentiometers in the SparkFun Inventor's Kit.  Here is my sketch for the MSP-EXP430F5529LP...

/*
  Control the brightness of a LED with the MSP-EXP430F5529LP LaunchPad
  Reads an analog input from a potentiometer, converts it to voltage, prints the result to
  the serial monitor, and varies the brightness of a LED accordingly

  HARDWARE REQUIRED:
  * MSP-EXP430F5529LP LaunchPad
  * Potentiometer
  * LED
  * 330 Ohm Resistor
 
  CONNECTIONS:
  Potentiometer
  Left pin        3.3 V             Note that potentiometer does not have polarity so flip
                                    the outside leads if you want to change rotation effect
  Center          Pin 6 (P6.6)      Make sure pin can do analog read
  Right pin       GND
 
  LED
  Positive pin    Pin 19 (P2_0)     LED output - make sure pin can do an analog write (PWM)
  Negative pin    330 Ohm    GND    Resistor protects LED

  2 April 2015
  Frank Milburn
  This example code is in the public domain.
*/

// Declarations
const int potPin = 6;                           // Input pin for pot
const int ledPin = 19;                          // Output pin for LED
const int analogRes = 4096;                     // resolution of analog input

int lastReading = -1;                           // Define lastReading for 1st pass
void setup()
{
  Serial.begin(9600);                           // Start serial
  Serial.println("Starting....");
  pinMode(ledPin, OUTPUT);                      // Make the LED pin an output
}

void loop()
{
  int potReading = analogRead(potPin);             // Read the potentiometer setting
   
  // Note that there is some jitter in a potentiometer.  In order to reduce the serial
  // print, a check is made on whether the change is signicant.  Note that a difference
  // in readings of 41 is about a 1% change since the resolution of an analog read
  // is 0 to 4095   
   
  if (abs(potReading - lastReading) > 41)          // if the readings change much
  {                                                // then update and inform of changes
    Serial.print("Raw reading: ");               
    Serial.print(potReading);
  
    float voltage = potReading * 3.3 / analogRes;  // Convert pot reading to voltage
    Serial.print("     Voltage: ");                // and inform the user
    Serial.println(voltage);
  
    int ledBrightness = map(potReading,0,analogRes,0,255);  // map to PWM duty cycle
    analogWrite(ledPin, ledBrightness);            // and adjust LED brigtness
 
    int brightness = (ledBrightness * 100) / 255;  // calculate brightness as percent
    Serial.print("Brightness: ");                  // and inform the user
    Serial.print(ledBrightness);
    Serial.print(" PWM   ");
    Serial.print(brightness);
    Serial.println(" %");
 
    lastReading = potReading;                      // update the last reading
  }
}

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
}