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
  }
}

No comments:

Post a Comment