Fading an LED with PWM and a Potentiometer

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

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

Here is the schematic for the above project.

potentiometer_to_led

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

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