analogWrite()

[Analog I/O]
void analogWrite(unsigned short pin, short value);

Description

Writes an analog value to a pin.

The analogWrite() function writes an analog value to a pin. The value is specified as an integer between 0 and 1023, where 0 corresponds to 0V and 1023 corresponds to the output voltage defined by the family electronic specifications.

Parameters

pin: the Custom Element pin.

value: the value to write to the pin.

Returns

Nothing.

Example Code

The code reads the analog value from the pin 3 and sets the value obtained to the pin 2.

void setup() {
    pinLabel(3, "IN");   // sets the pin 3 label to "IN"
    pinMode(3, INPUT);   // sets the digital pin 3 as input
    pinLabel(2, "OUT");  // sets the pin 2 label to "OUT"
    pinMode(2, OUTPUT);  // sets the digital pin 2 as output
}

void loop() {s
    int value = analogRead(3);  // reads the analog value from the pin 3
    analogWrite(2, value);      // sets the value obtained to the pin 2 
}

Notes and Warnings

The analogWrite() function can be used only the pins defined as outputs.