Pulse Width Modulation Using a Potentiometer

/**/

Pulse Width Modulation Using a Potentiometer

In this project, I am going to demonstrate how to use a potentiometer to dim LEDs.

Things used in this project

Hardware components

  • Arduino UNO x 1
  • Idiotware Shield x 1
  • USB-A to B Cable x 1
  • Generic Jumper (0.1″) x 2

Software apps and online services

  •  Arduino IDE

Story

Mode of Operation

A potentiometer, or “pot” for short, is a variable resistor. It’s the same type of control you’d use to change volume or dim a lamp. A potentiometer changes resistance as it is turned. By using it as a “voltage divider”, the Arduino can sense the position of the knob, and use that value to control anything you wish.

In this example we are going to fade 3 LEDs depending upon the position of the knob of a potentiometer.

Configuring idIoTware Shield

Once we upload the code to Arduino, we can rotate the potentiometer to fade the LEDs accordingly. No more connecting LEDs to breadboards and messy wiring. With the idIoTware shield the sky’s the limit.

Schematics

Code

int ledPin1 = 3;    // LED connected to digital pin 3
int ledPin2 = 6;    // LED connected to digital pin 6
int ledPin3 = 9;    // LED connected to digital pin 9
int potentiometerPin = A2; 


void setup()  
    { 
     // nothing happens in setup 
    } 
 
void loop() 
    { 
      int potValue = potentiometerValue();
      int fadeValue = map(potValue, 0, 1023, 0, 255);   // sets the value (range from 0 to 255):
      
      analogWrite(ledPin1, fadeValue);
      analogWrite(ledPin2, fadeValue);
      analogWrite(ledPin3, fadeValue);        
      // wait for 30 milliseconds to see the dimming effect    
      delay(30);                            
    }

    
//function to calculate potentiometer value
int potentiometerValue()
   {
    int val = analogRead(potentiometerPin);
    return val;
   }    

Video