Fading an LED with PWM and a Potentiometer

Using a potentiometer and PWM on an Arduino to fade an LED.

  1.  
  2. /* POT to LED test -> by Owen Mundy March 11, 2010
  3.    from: http://itp.nyu.edu/physcomp/Labs/AnalogIn
  4. —————————————————————*/
  5.  
  6. int potPin = 0;    // Analog input pin that the potentiometer is attached to
  7. int potValue = 0;  // value read from the pot
  8. int led = 9;      // PWM pin that the LED is on.  n.b. PWM 0 is on digital pin 9
  9.  
  10. void setup() {
  11.   // initialize serial communications at 9600 bps:
  12.   Serial.begin(9600);
  13.   // declare the led pin as an output:
  14.   pinMode(led, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18.   potValue = analogRead(potPin); // read the pot value
  19.   analogWrite(led, potValue/4);  // PWM the LED with the pot value (divided by 4 to fit in a byte)
  20.   Serial.println("hello");      // print the pot value back to the debugger pane
  21.   delay(10);                     // wait 10 milliseconds before the next loop
  22. }
  23.  

Here is the schematic for the above project.

Using PWM and a potentiometer to fade an LED and drive a stepper motor, powered by a Boarduino RBBB.

  1.  
  2. /*
  3.   Owen Mundy
  4.  July 29, 2009
  5.  
  6.  p. 262 of Physical Computing
  7.  Using BBB to run stepper motor by manually moving steppers
  8.  
  9.  */
  10.  
  11. int pin1 = 3;                 // PWM
  12. int pin2 = 5;                 // PWM
  13. int pin3 = 6;                 // PWM
  14. int pin4 = 9;                 // PWM
  15. int ledpin = 13;              // LED
  16. int led = false;              // LED monitor
  17. int motor_time_lapse = 80;
  18.  
  19. int potPin = 0;      // Analog input pin that the potentiometer is attached to
  20. int potValue = 0;    // value read from the pot
  21. int ledPotPin = 11;  // PWM pin that the LED is on.  n.b. PWM 0 is on digital pin 9
  22.  
  23.  
  24. void setup()
  25. {
  26.   pinMode(pin1, OUTPUT);      // sets the pin as output
  27.   pinMode(pin2, OUTPUT);      // sets the pin as output
  28.   pinMode(pin3, OUTPUT);      // sets the pin as output
  29.   pinMode(pin4, OUTPUT);      // sets the pin as output
  30.   pinMode(ledpin, OUTPUT);    // sets the pin as output
  31.  
  32.   // initialize serial communications at 9600 bps:
  33.   Serial.begin(9600);
  34.   // declare the led pin as an output:
  35.   pinMode(ledPotPin, OUTPUT);
  36. }
  37.  
  38. void loop()
  39. {
  40.   potValue = analogRead(potPin); // read the pot value
  41.   analogWrite(ledPotPin, potValue/4);  // PWM the LED with the pot value (divided by 4 to fit in a byte)
  42.   Serial.println(potValue);
  43.  
  44.   digitalWrite(pin1, HIGH);   // on
  45.   digitalWrite(pin2, LOW);    // off
  46.   digitalWrite(pin3, HIGH);   // on
  47.   digitalWrite(pin4, LOW);    // off
  48.   delay(motor_time_lapse);    // wait
  49.  
  50.  
  51.   digitalWrite(pin1, LOW);    // off
  52.   digitalWrite(pin2, HIGH);   // on
  53.   digitalWrite(pin3, HIGH);   // on
  54.   digitalWrite(pin4, LOW);    // off
  55.   delay(motor_time_lapse);    // wait
  56.  
  57.   digitalWrite(pin1, LOW);    // off
  58.   digitalWrite(pin2, HIGH);   // on
  59.   digitalWrite(pin3, LOW);    // off
  60.   digitalWrite(pin4, HIGH);   // on
  61.   delay(motor_time_lapse);    // wait
  62.  
  63.  
  64.   digitalWrite(pin1, HIGH);   // on
  65.   digitalWrite(pin2, LOW);    // off
  66.   digitalWrite(pin3, LOW);    // off
  67.   digitalWrite(pin4, HIGH);   // on
  68.   delay(motor_time_lapse);    // wait
  69.  
  70.   blink();
  71. }
  72.  
  73. void blink()
  74. {
  75.   if (led == false)
  76.   {
  77.     led = true;
  78.     digitalWrite(ledpin, HIGH); // on  
  79.   }
  80.   else
  81.   {
  82.     led = false;
  83.     digitalWrite(ledpin, LOW); // on  
  84.   }
  85. }

Tags: , , , ,



3 Responses to “Fading an LED with PWM and a Potentiometer”

  1. Joey Says:

    Does the reed the value of the analog pot or do u have to change the code. EX. if I have a 5k pot than I switch it out for a 10K pot will the program reed it and than change the code to operate with 10K pot?

  2. Jelle Says:

    Yep, will word only, no problems there only POT scale will react different, hard to controll
    preferrable to take a 1K pot.

    Made a sketch for fan controll with LCD (Not tested)

    #include
    ShiftLCD lcd(2, 4, 3);
    int fanPulse = 1;
    unsigned long pulseDuration;

    int potPin = 0; // Analog input pin that the potentiometer is attached to
    int potValue = 0; // value read from the pot
    int fan = 3; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9

    void setup()
    {
    Serial.begin(9600);
    lcd.begin(20, 4);
    pinMode(fan, OUTPUT);
    pinMode(fanPulse, INPUT);
    digitalWrite(fanPulse,HIGH);
    }

    void readPulse() {
    pulseDuration = pulseIn(fanPulse, LOW);
    double frequency = 1000000/pulseDuration;
    Serial.print(“pulse duration:”);
    Serial.println(pulseDuration);
    Serial.print(“time for full rev. (microsec.):”);
    Serial.println(pulseDuration*2);
    Serial.print(“freq. (Hz):”);
    lcd.setCursor(0,1);
    lcd.print(“FREQUENTIE: “);
    Serial.println(frequency/2);
    lcd.setCursor(12,1);
    lcd.println(frequency/2);
    Serial.print(“RPM:”);
    lcd.setCursor(0,0);
    lcd.print(“TOERENTAL: “);
    Serial.println(frequency/2*60);
    lcd.setCursor(12,0);
    lcd.println(frequency/2*60);

    }

    void loop()
    {
    potValue = analogRead(potPin); // read the pot value
    analogWrite(fan, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
    readPulse();
    delay(25);
    }

  3. Jay Says:

    Your wiring diagram is incorrect but your photos do show a correct connection. Center pin goes to Analog Input. The way you have it shown will short the 5V to ground when the pot is turned all the way to one side.

Leave a Reply