Wednesday, 26 June 2013

About PWM in Arduino board

PWM is Pulse Width Modulation, it is a technique which will represents  analog values with using digital means. If you want to get different results, you can change the pulse width. Therefore, we can output results with using function analogWrite().

Syntax:
analogWrite(pin, value); 
where "pin" is the pin number will be written to, and "value" is the duty cycle which is between 0(off) and 255(on). Furthermore, duty cycle is shown in Figure 1:

                                  Figure 1: Duty cycle of PWM, taken from Arduino

If you want to adjust the speed of motor, you can only change the value in function analogWrite(). We use Lego motor to test this function, so we need to connect the white wire to pin 3 and black wire to pin GND. Therefore, pin=3. For example, analogWrite(3, 0) means the Motor cannot rotate, analogWrite(3, 255) represents the motor will rotate at full speed, thus, analogWrite(3, 127) means that motor will rotate at half speed. 

Note: Not all pins in Arduino can be used to output PWM, the pins which is labeled "~" means that it can be used for outputting PWM as shown in Figure2.

                                       Figure 2: Arduino board, taken from Arduino

In above picture, pin 3, 5, 6, 9, 10, 11 can be used for PWM, thus, the function analogWrite() is only effective for this pins. If this function is used for other pins, it works abnormally. So there are 6 pins used for PWM output. 


Reference: 

1. Arduino, http://arduino.cc/en/


No comments:

Post a Comment