analogWrite()

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz.

Refer to the hardware page on github for correct pin mapping https://github.com/energia/Energia/wiki/Hardware

For some boards, certain pins conflict with each other. They use the same timer and thus you can only analogWrite() to one of the conflicting pins. As an example, the MSP-EXP430G2553 uses the same timer for pin 4, 14 and 19. analogWrite() can only be used with one of these pins at the same time. If analogWrite() is used with both of these pins at the same time, the value written to one pin will be copied to the other pin and both pins will show the same PWM signal even though the values written were different. See example below:


setup() {
analogWrite(4, 128);
analogWrite(14, 128);
// After this third analogWrite() pin 4 and 14 will now also show
// a PWM signal with value 64 as the duty cycle
analogWrite(19, 64);
}

Pins that use the same timer for the various LaunchPad’s. Only LaunchPad’s that have conflicting pins are listed

  • MSP-EXP430G2553:
    • Pins: 4, 14 and 19
    • Pins: 9 and 10
    • Pins: 12 and 13
  • MSP-EXP430FR5969:
    • Pins: 3 and 12
    • Pins: 7 and 28
    • Pins: 8 and 15
    • Pins: 9 and 14
  • MSP-EXP430FR6989:
    • Pins: 8 and 40
    • Pins: 11 and 34
    • Pins: 12 and 36
    • Pins: 13 and 35
    • Pins: 14 and 45
    • Pins: 15 and 43
    • Pins: 19 and 39
    • Pins: 38 and 46
  • MSP-EXP430FR5739:
    • Pins: 2 and 23
    • Pins: 5 and 9
    • Pins: 6 and 10
    • Pins: 7 and 11
    • Pins: 8 and 12
  • CC3200-LAUNCHXL:
    • Pins: 9 and 39 (both routed to GPIO_10 of the chip)
    • Pins: 10 and 40 (both routed to GPIO_11 of the chip)
    • Pins: 13 and 36 (both routed to GPIO_25 of the chip)
    • Pins: 19 and 37 (both routed to GPIO_09 of the chip)

CAUTION — Analog input is limited to 1.46V on the CC3200. Higher voltages may damage the MCU. — Reference: §3.2 Drive Strength and Reset States for Analog-Digital Multiplexed Pins, CC3200 SimpleLink™ Wi-Fi® and Internet-of-Things Solution, a Single-Chip Wireless MCU (swas032f).

The analogWrite function has nothing whatsoever to do with the analog pins or the analogRead function.

 

Syntax

analogWrite(pin, value)

 

Parameters

pin: the pin to write to.

value: the duty cycle: between 0 (always off) and 255 (always on).

 

Returns

nothing

 

Example

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

int ledPin = 14;      // LED connected to digital pin 14
int analogPin = A3;   // potentiometer on education boosterpack connected to analog pin 5
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop()
{
  val = analogRead(analogPin);   // read the input pin
  analogWrite(ledPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

 

See also

 

Reference Home