analogWrite()

Description

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

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

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Energia reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Energia reference is based on Arduino reference. Code samples in the reference are released into the public domain.