Arduino的模拟输出-analogWrite()

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.

模拟值PWM波)输出到管脚可用于不同的光线亮度调节发光二极管亮度或以不同的速度驱动马达。调用analogWrite()后该引脚将产生一个指定占空比的稳定方波,直到下一次调用analogWrite()(或在同一引脚调用digitalRead()digitalWrite()) PWM的信号频率约为490赫兹。

 

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. You do not need to call pinMode() to set the pin as an output before calling analogWrite().

在大多数Arduino(带有ATmega168ATmega328),这个函数工作在引脚3,5,6,9,10和11。ArduinoMega,它适用于2-13号引脚。老的带有ATmega8的Arduino板只支持9,10,11引脚上使用你并不需要在调用analogWrite()之前为设置输入引脚而调用pinMode()

 

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

这个analogWrite方法与模拟引脚或者analogRead方法毫不相干

 

Syntax 语法

analogWrite(pin, value)

 

Parameters 参数

pin: the pin to write to.

pin:输出的引脚号

 

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

value:占用空:从0(常关)到255(常开)

 

Returns 返回值

nothing

 

Notes and Known Issues 备注和已知问题

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

引脚5和6的PWM输出产生高于预期的占空比。这是因为millis()和delay()函数,它们共享同一个内部定时器用于产生PWM输出所产生的相互作用这提醒我们引脚5和6在多数占空比的设置(如0- 10)的情况下0数值的结果并没有完全关闭

 

Example 例子

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

 

 
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
}
  • 11
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值