# analogWrite

## 函数原型

```cpp
void analogWrite(uint8_t pin, int val);
```

## 作用

设置指定引脚的电压，而且**只有支持模拟输出的引脚才能使用此函数。**

判断是否支持模拟输出的标志是引脚编号旁有一个小波浪线，但 Arduino Uno 中 A 开头的引脚除外，它们既支持模拟输出，又支持模拟输入。

{% hint style="info" %}
在调用此函数之前要保证该引脚已经 `pinMode` 过，为输出模式，且只有支持模拟输出的引脚才能使用此函数。
{% endhint %}

## 参数

* `pin`

  指定引脚的编号，如： `10` , `A0` 等。
* `val`

  引脚的电平。

其中，`val` 的值的范围是 **0\~255**，也就是电压位 0 对应 0， 电压为 5V 对应 255，成比例对应

## 示例

```cpp
digitalWrite(3, 0);   // 设置 3 号引脚为 0V
digitalWrite(3, 255); // 设置 3 号引脚为 5V
digitalWrite(3, 233); // 设置 3 号引脚为 (5 * 233 / 255)V
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://arduino.doc.skyone.host/core/io/analogwrite.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
