pwmWrite() — Maple v0.0.9 Documentation

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »
- Complete Language Index »

# pwmWrite()

Writes a PWM wave 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.

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 analogWrite() is an alias for `pwmWrite()`, but it is badly named, and its use is discouraged.

Contents

- Library Documentation
- Example
- See Also

## Library Documentation

void pwmWrite(uint8 pin, uint16 duty_cycle)

  Set the PWM duty on the given pin.

  User code is expected to determine and honor the maximum value (based on the configured period).

## Example

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

    int analogPin = 3;    // potentiometer connected to analog pin 3
    int val = 0;          // variable to store the read value

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

      pinMode(analogPin, PWM);   // sets the potentiometer pin as PWM
                                 // output
    }

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

      analogWrite(BOARD_LED_PIN, val / 16);  // analogRead values go from 0
                                             // to 4095, analogWrite values
                                             // from 0 to 65535
    }

## See Also

- Maple PWM tutorial

### Navigation

- next
- previous |
- LeafLabs |
- Docs Home |
- Index »
- Complete Language Index »