AnalogWrite() - PWM

From Microduino Wiki
Revision as of 06:16, 12 August 2016 by Fengfeng (talk) (Created page with "<pre style="color:green"> void analogWrite (uint8_t pin, int value) </pre> Write the analog pin. <br> *'''Parameter''':<br> pin Number of the pin. value From 0 to 255...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
void analogWrite (uint8_t pin, int value)    

Write the analog pin.

  • Parameter:

pin Number of the pin.

value From 0 to 255, 0 is corresponding to off,, and 255 is corresponding to on.

Write an analog value (PWM) to the pin. It can be sued to controlled the brightness of LED, or the rotational speed of motor. After executing this operation, you should wait for a moment before the next reading or writing to this pin. The frequency of the PWM is about 490Hz.

In some new Arduino controlling boards based on ATmega168 (such as Mini and BT), this function supports the following pins: 3, 5, 6, 9, 10, 11. In the based-on-ATmega8 one, it supports pin 9, 10 and 11.

  • Example:
int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
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
}

[Return to Arduino Syntax Manual]