Test of PWM on Arduino

Tags:

One of the easiest way of testing PWM (Pulse Width Modulation) for regulating power on electric motors, such as fans, is to attach a standard LED to the Arduino.

Put the short pin of the LED into the socket marked PWN 13 on the digital side of the board, and the other pin into the socket marked GND.

Run the following little program (called a Sketch) on the Arduino.

// PWM test using a LED // connected between one of the PWM pins // - and ground - use the short pin to ground int ledvalue = 0; // variable to keep the pwn duty cycle value int ledpin = 9; // light connected to pwm pin int stepsize = 1; // how many steps to increase the duty cycle with per stepdelay int stepdelay = 20; // The delay (millisecs) between increases/decreases in duty cycle int ledmin = 10; // minimum value for duty cycle int ledmax = 255; // maximum value for duty cycle
void setup() { }
void loop() { for(ledvalue = ledmin ; ledvalue <= ledmax; ledvalue+=stepsize) // increase duty cycle (from min to max) { analogWrite(ledpin, ledvalue); // sets the value from ledmin to ledmax delay(stepdelay); // waits for stepdelay milliseconds to make sure we can observe this } for(ledvalue = ledmax; ledvalue >=ledmin; ledvalue-=stepsize) // reduce duty cycle (from max to min) { analogWrite(ledpin, ledvalue); delay(stepdelay); } }
And you can see the brightness of the LED change.
But these pins on the Arduino can only supply a little bit of current, so to support a real motor we will need to use a few more components to drive these - more about this later.

Technorati Tags:Technorati Tags:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.