aboutsummaryrefslogtreecommitdiffstats
path: root/source/lang/api/pwmwrite.rst
blob: cea602bf20feb8ce72b481c7d3b7dd9ef45926e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
.. highlight:: cpp

.. _lang-pwmwrite:

pwmWrite()
==========

Writes a :ref:`PWM wave <pwm>` to a pin.  You can use this to make an
LED get brighter or dimmer, control a servomotor, etc. After a call to
pwmWrite(), the pin will output a steady square wave with the given
duty cycle.  You can change the duty cycle later by calling pwmWrite()
again with the same pin and a different duty.

.. FIXME board-specific information

On the Maple, the pins which support PWM are: 0, 1, 2, 3, 5, 6, 7, 8,
9, 11, 12, 14, 24, 27, and 28.

The Arduino function :ref:`analogWrite() <lang-analogwrite>` is an
alias for ``pwmWrite()``, but it is badly named, and its use is
discouraged.

.. contents:: Contents
   :local:

Library Documentation
---------------------

.. doxygenfunction:: pwmWrite

Example
-------

Sets the output to the LED proportional to the value read from the
potentiometer::

    int analogPin = 3;    // potentiometer connected to analog pin 3

    void setup() {
      pinMode(BOARD_LED_PIN, OUTPUT);   // sets the LED pin as output

      pinMode(analogPin, INPUT_ANALOG); // sets the potentiometer pin as
                                        // analog input
    }

    void loop() {
      int val = analogRead(analogPin);        // read the input pin

      pwmWrite(BOARD_LED_PIN, val * 16);  // analogRead values go from 0
                                          // to 4095, pwmWrite values
                                          // from 0 to 65535, so scale roughly
    }

See Also
--------

-  :ref:`Maple PWM tutorial <pwm>`