Introduction: PWM
PWM-Pulse with modulation, arduino uno has 6 pins with pwm (pins: 3, 5, 6, 9, 10, 11), they can be used to light a LED at varying brightnesses or drive a motor at various speeds.
"Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off."-from https://www.arduino.cc/en/Tutorial/PWM
When the we select the pwm pin and call analogWrite () the pin will generate a square wave with the specified duty cycle (from 0-255) in volts that is from 0V-5V.
I made a program to see how pwm pin can change a speed of a motor or brightnesses of an LED,
the program is simple and you can see it on the arduino web site.
CODE:
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4);
}
-----------------------------------------------------------------------------------------------------------
Step 1: PWM Without Pot
It is possible to change brightness without a potentiometer, it is just change in code, led is connected to GND and to pin 3.
int ledPin=3;
int var=0; //initial value of variable
void setup()
{
pinMode (ledPin, OUTPUT); //setting pin to be output
}
void loop()
{
analogWrite(ledPin, var++);
delay (500);
}