Arduino Programming Language: AnalogWrite




The analogWrite()method sets the value of a PWM output pin. 
The frequency of the PWM signal on most pins is approximately 490Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980Hz. Pins 3 and 11 on the Leonardo also run at 980Hz. 
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 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. 
The possible values are between 0 to 255.

In the M0/M0 Pro boards on the A0 pin there is the DAC and it can be used via analogWrite, choosing a value between 0 and 1023.


analogWrite(pin, val)


  • pin: the PWM output pin number.
  • val: int value of duty cycle between from 0(always off) to 255(always on)






int outLed = 10; //LED connected to digital pin 10
int value = 0; //variable to store the read value
int analogIN = 3; //input pin
void setup() {
pinMode(outLed, OUTPUT); // set the PWM pin as OUTPUT

void loop() {
value = analogRead(analogIN); // read the value of analogIN (values between from 0 to 1023)
analogWrite(outLed, value/4); // sets the read value on outLed (values between from 0 to 255)

Based on the reference originally written by Arduino Community, and