Reference.AnalogWrite History

Hide minor edits - Show changes to markup

June 01, 2010, at 12:41 AM by Equipo Traduccion -
Changed lines 3-28 from:

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.

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().

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

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.

Example

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

to:

Descripción

Escribe un valor analógico (PWM) en un pin. Puede ser usado para controlar la luminosidad de un LED o la velocidad de un motor. Después de llamar a la función analogWrite(), el pin generará una onda cuadrada estable con el ciclo de trabajo especificado hasta que se vuelva a llamar a la función analogWrite() (o una llamada a las funciones digitalRead() o digitalWrite() en el mismo pin). La frecuencia de la señal PWM sera de aproximadamente 490 Hz.

En la mayoría de las placas Arduino (aquellas con el ATmega168 o ATmega328), se podrá generar señales PWM en los pines 3, 5, 6, 9, 10, y 11. En la placa Arduino Mega, se puede llevar a cabo con los pines desde el 2 hasta el pin 13. Las placas Arduino más antiguas que posean el chip ATmega8 solo podrán usar la función analogWrite() con los pines 9, 10 y 11. No hace faltar configurar el pin como salida para poder usar la función analogWrite().

La función analogWrite no tienen ninguna relación con los pines de entrada analógicos ni con la función analogRead.

Sintaxis

analogWrite(pin, valor)

Parámetros

pin: Es el pin en el cual se quiere generar la señal PWM.

valor: El ciclo de trabajo deseado comprendido entre 0 (siempre apagado) y 255 (siempre encendido).

Devuelve

Nada

Notas y problemas conocidos.

Las señales PWM generadas en los pines 5 y 6 poseerán ciclos de trabajo superiores a lo esperado. Esto es así por que para esos dos pines se utiliza el mismo temporizador que se utiliza en las funciones millis() y delay(). Este efecto se notará mucho más en ciclos de trabajo bajos (por ejemplo de 0 a 10) y puede ser que aunque configuremos esos pines con una señal de ciclo de trabajo cero no llegue a ser verdaderamente 0.

Ejemplo

Produce una señal donde conectamos el LED, cuyo ciclo de trabajo es proporcional a la tensión leída en el potenciómetro.

Changed lines 30-33 from:

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

to:

int ledPin = 9; // LED conectado al pin digital 9 int analogPin = 3; // potenciómetro conectado al pin 3 int val = 0; // variable en el que se almacena el dato leído

Changed lines 41-42 from:
  val = analogRead(analogPin);   // read the input pin
  analogWrite(ledPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
to:
  val = analogRead(analogPin);   // lee la tensión en el pin
  analogWrite(ledPin, val / 4);  // los valores de analogRead van desde 0 a 1023 y los valores de analogWrite values van desde 0 a 255, por eso ajustamos el ciclo de trabajo a el valor leído dividido por 4.
Changed line 48 from:

See also

to:

Véase también

April 27, 2010, at 10:52 PM by Paul Badger -
Changed lines 8-9 from:

analogWrite has nothing whatsoever to do with the analog pins or analogRead().

to:

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

April 27, 2010, at 10:52 PM by Paul Badger -
Added lines 8-9:

analogWrite has nothing whatsoever to do with the analog pins or analogRead().

February 01, 2010, at 07:39 PM by David A. Mellis -
Changed lines 6-7 from:

On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

to:

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().

Changed lines 21-26 from:

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

You do not need to call pinMode() to set the pin as an output before calling analogWrite().

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 analogWrite(x, 0) not fully turning off the output on pins 5 & 6, as one would expect.

to:

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.

Changed lines 49-51 from:
  • Adjusting Arduino PWM Frequencies

to:
November 18, 2009, at 06:58 PM by Paul Badger -
Changed lines 21-22 from:

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

to:

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

November 18, 2009, at 06:57 PM by Paul Badger -
Added lines 20-22:

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

Changed lines 25-26 from:

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 analogWrite(x, 0) not fully turning off the output on pins 5 & 6, as one would expect.

to:

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 analogWrite(x, 0) not fully turning off the output on pins 5 & 6, as one would expect.

November 18, 2009, at 05:54 PM by Paul Badger -
Changed lines 22-23 from:

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.

to:

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 analogWrite(x, 0) not fully turning off the output on pins 5 & 6, as one would expect.

June 06, 2009, at 05:01 AM by Paul Badger -
Changed lines 50-52 from:
to:
  • Adjusting Arduino PWM Frequencies

June 06, 2009, at 04:59 AM by Paul Badger -
Changed lines 50-52 from:
to:
February 21, 2009, at 10:20 PM by David A. Mellis -
Changed lines 49-50 from:
to:
February 21, 2009, at 10:20 PM by David A. Mellis -
Deleted line 47:
Added lines 49-50:
February 21, 2009, at 10:09 PM by David A. Mellis -
Changed line 4 from:

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.

to:

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.

Changed lines 49-51 from:
to:
November 02, 2008, at 11:00 AM by David A. Mellis -
Changed lines 1-2 from:

analogWrite(pin, value)

to:

analogWrite()

Added lines 8-10:

Syntax

analogWrite(pin, value)

November 02, 2008, at 10:59 AM by David A. Mellis -
Changed line 4 from:

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.

to:

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.

October 01, 2008, at 12:58 PM by Paul Badger -
Changed line 4 from:

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 wave 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.

to:

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.

June 08, 2008, at 06:18 PM by David A. Mellis -
Changed lines 4-7 from:

Writes an analog value (PWM wave) to a pin. On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

to:

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 wave 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.

On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

Changed lines 11-12 from:

value: the duty cycle: between 0 and 255. A value of 0 generates a constant 0 volts output at the specified pin; a value of 255 generates a constant 5 volts output at the specified pin. For values in between 0 and 255, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts). For example, a value of 64 will be 0 volts three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one quarter of the time and 5 volts three-quarters of the time.

to:

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

Deleted lines 18-19:

The frequency of the PWM signal is approximately 490 Hz.

April 10, 2008, at 04:58 PM by David A. Mellis -
Changed lines 21-22 from:

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because the internal timer used to generate the PWM signals on pins 5 and 6 is also used for the millis() and delay() functions.

to:

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.

April 10, 2008, at 04:56 PM by David A. Mellis -
Changed lines 21-22 from:

The millis() and delay() functions apparently interfere with the PWM signal on pins 5 and 6 causing them to report a higher-than-expected average voltage.

to:

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because the internal timer used to generate the PWM signals on pins 5 and 6 is also used for the millis() and delay() functions.

April 10, 2008, at 04:54 PM by David A. Mellis -
Added line 47:
February 18, 2008, at 10:51 PM by Paul Badger -
Changed line 16 from:

Note

to:

Notes and Known Issues

Added lines 21-22:

The millis() and delay() functions apparently interfere with the PWM signal on pins 5 and 6 causing them to report a higher-than-expected average voltage.

July 17, 2007, at 08:09 PM by David A. Mellis - clarifying a couple of things
Changed lines 17-18 from:

Pins taking analogWrite (9-11), unlike standard digital ones (1-8, 12, 13), do not need to be declared as INPUT nor OUTPUT

to:

You do not need to call pinMode() to set the pin as an output before calling analogWrite().

Deleted lines 20-21:

analogWrite only works on pins 9, 10, and 11; on all other pins it will write a digital value of 0 or 5 volts.

July 17, 2007, at 05:32 AM by Paul Badger -
Deleted lines 49-51:
July 17, 2007, at 05:31 AM by Paul Badger -
Added lines 24-26:

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

Changed line 28 from:

int ledPin = 9; // LED connected to digital pin 9

to:

int ledPin = 9; // LED connected to digital pin 9

Changed lines 30-31 from:

int val = 0; // variable to store the read value

to:

int val = 0; // variable to store the read value

Changed lines 44-45 from:

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

to:
June 15, 2007, at 11:45 PM by David A. Mellis - updating to reflect the fact that the atmega168 (and thus 6 pwm pins) is now standard.
Changed lines 4-5 from:

Writes an analog value (PWM wave) to a pin. On normal Arduino boards (e.g. Arduino NG), this works on pins 9, 10, or 11. On the Arduino Mini, this also works on pins 3, 5, and 6.

to:

Writes an analog value (PWM wave) to a pin. On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

February 26, 2007, at 11:32 PM by David A. Mellis -
Changed lines 19-20 from:

The frequency of the PWM signal is approximately 30769 Hz

to:

The frequency of the PWM signal is approximately 490 Hz.

November 11, 2006, at 09:33 AM by David A. Mellis -
Changed lines 4-5 from:

Writes an analog value (PWM wave) to pins 9, 10, or 11. 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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

to:

Writes an analog value (PWM wave) to a pin. On normal Arduino boards (e.g. Arduino NG), this works on pins 9, 10, or 11. On the Arduino Mini, this also works on pins 3, 5, and 6.

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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

April 13, 2006, at 06:03 PM by Clay Shirky - Updated PWM description to include pin 11
Changed lines 4-5 from:

Writes an analog value (PWM wave) to pin 9 or 10. 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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

to:

Writes an analog value (PWM wave) to pins 9, 10, or 11. 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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

Changed lines 9-10 from:

value: the duty cycle: between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

to:

value: the duty cycle: between 0 and 255. A value of 0 generates a constant 0 volts output at the specified pin; a value of 255 generates a constant 5 volts output at the specified pin. For values in between 0 and 255, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts). For example, a value of 64 will be 0 volts three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one quarter of the time and 5 volts three-quarters of the time.

Changed lines 15-16 from:

Analog pins unlike digital ones, do not need to be declared as INPUT nor OUTPUT

to:

Pins taking analogWrite (9-11), unlike standard digital ones (1-8, 12, 13), do not need to be declared as INPUT nor OUTPUT

Changed lines 19-20 from:

analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

to:

analogWrite only works on pins 9, 10, and 11; on all other pins it will write a digital value of 0 or 5 volts.

March 31, 2006, at 11:43 AM by David A. Mellis -
Changed lines 4-5 from:

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. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

to:

Writes an analog value (PWM wave) to pin 9 or 10. 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 wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

Changed lines 9-10 from:

value: the value between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

to:

value: the duty cycle: between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

Added lines 19-20:

analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

January 19, 2006, at 12:01 PM by 85.18.81.162 -
Added lines 17-18:

The frequency of the PWM signal is approximately 30769 Hz

January 13, 2006, at 12:39 AM by 82.186.237.10 -
Changed lines 4-5 from:

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. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

to:

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. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

Changed line 11 from:

This function returns

to:

Returns

Added lines 42-43:
January 03, 2006, at 10:36 AM by 82.186.237.10 -
Changed lines 1-10 from:

analogWrite

What it does

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.

What parameters does it take

The pin to write to. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

The value between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

to:

analogWrite(pin, value)

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. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

Parameters

pin: the pin to write to.

value: the value between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

December 16, 2005, at 10:14 PM by 85.18.81.162 -
Added lines 11-13:

This function returns

nothing

December 16, 2005, at 10:09 PM by 85.18.81.162 -
Added lines 1-38:

analogWrite

What it does

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.

What parameters does it take

The pin to write to. analogWrite only works on pins 9 and 10; on all other pins it will write a digital value of 0 or 5 volts.

The value between 0 and 255. 0 corresponds to constant low output (no voltage); 255 is a constantly on output (5 volts). For values in-between, the pin rapidly alternates between 0 and 5 volts - the higher the value, the more often the pin is high (5 volts).

Note

Analog pins unlike digital ones, do not need to be declared as INPUT nor OUTPUT

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
}

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

See also

Share