7

As the title states. What's the difference between the two?

analogWrite(pin,0-255) vs digitalWrite(pin,LOW-HIGH)

  • I don't mean to be rude but what was confusing about this in the documentation at arduino.cc or included with the IDE? – linhartr22 Dec 14 '18 at 3:01
15
+100

digitalWrite will set the specified pin to one of two states - HIGH/LOW, which equate to 5v (3.3v on some boards) and ground respectively.

analogWrite can vary by the type of output used.

If applied to a PWM pin - it will set the pin to a periodic high/low signal, where the percentage of the signal spent high is proportional to the value written. for example -

    analogWrite(PWMpin,255)

Will be HIGH 100% of the time, while

    analogWrite(PWMpin,127)

Will be HIGH 50% of the time, and LOW 50% of the time

When applying analogWrite to a DAC pin (available on some boards, like the DUE or MEGA) analogWrite will actually cause the specified pin to output a voltage level proportinal to the specified analog value

For example, on the Due, with maximal voltage of 3.3v and a default analog resolution of 8 bits -[0:255]

    analogWrite(DACpin,255)

Will cause the specified pin to output 3.3v, and-

    analogWrite(DACpin,127)

Will cause the specified pin to output 1.35v

  • 2
    No, there is no DAC on the Mega. – Edgar Bonet Mar 18 '17 at 21:59
  • you are correct, answer edited to reflect this. – TanyaV Mar 18 '17 at 22:52
  • Great. I took the liberty to make the edit more concise. – Edgar Bonet Mar 19 '17 at 9:21
  • I believe there is a "maxanalog" define or constant, to provide better compatibility with other platforms. The esp8266 has an analogwrite that can range from 0 to 1024. *I've scratched some hairs out trying to find why my RGB led controller wouldn't be as bright with the same code, thought it was due to the difference in power or voltage, but I used an MOSFET with suitable (gain?) factor. It was simply on for (1024/256) of the time. – Paul Mar 23 '17 at 17:07
6

analogWrite(): The analogWrite() method sets the value of a PWM output pin. The analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time).

PWM Duty Cycle

Syntax: analogWrite(pin, val)

Where,

pin: the PWM output pin number.

val: int value of duty cycle between from 0(always off) to 255(always on)

Example Code:

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)
}

digitalWrite: The digitalWrite() method sets the value of a digital pin as HIGH or LOW. Here, 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

Syntax: digitalWrite(pin, val)

Where,

pin: the pin number

val: HIGH or LOW

Example Code:

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

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
5

digitalWrite sets the output pin to either LOW or HIGH (where those voltages depend on the Vcc of the processor. For a Uno or Mega that would be 0V or 5V (or close to it).

Here's a screenshot of digitalWrite (LOW):

digitalWrite (LOW)

That is, the output pin is at 0V.


Now for digitalWrite (HIGH):

The output voltage is 5V.

digitalWrite (HIGH)


analogWrite really should have been named PWMwrite since it configures the processor timers to output PWM (pulse-width modulation).

Let's try analogWrite(1):

analogWrite (1)

You can see that the voltage level is 0V most of the time, and going to 5V for short periods. You also see that the frequency is 490 Hz which is what the reference page for analogWrite says it will be.


Zooming in:

analogWrite (1) - zoomed

The output is high for 8 µs, which is exactly 1/256 of 2048 µs which is the period of the timer. So, we have a duty cycle of 1/256 (0.39%).


Let's try analogWrite(127) - half-way from 0 to 255:

analogWrite (127)

Now you can see that the output is HIGH exactly half the time, and LOW the rest of the time.


Let's try analogWrite(254):

analogWrite (254)

This is the opposite of analogWrite (1). The output is HIGH all the time except for a brief period. Zooming in:

analogWrite (254) - zoomed

Now the output is off for 8 µs - compared to the earlier image where it was on for 8 µs.


analogWrite (0) is the same as digitalWrite (LOW).

analogWrite (255) is the same as digitalWrite (HIGH).

This is proven by the relevant code in wiring_analog.c:

if (val == 0)
{
    digitalWrite(pin, LOW);
}
else if (val == 255)
{
    digitalWrite(pin, HIGH);
}

Summary

analogWrite basically configures the hardware timers to output PWM. Once you do that the timer hardware outputs the requested duty cycle (from 0 to 255) where 0 is always off, 255 is always on, and some value inbetween gives you PWM (pulsed output).


For more information about the timers see my page about timers.

3

digitalWrite sets the pin to an high or low value that remains at exactly that value until digitalWrite is called for that pin again.

analogWrite sets the pin to have an oscillating value which has a pulse length based of the duty cycle specified as the second parameter.

So:

digitalWrite (5, HIGH);    // Pin 5 goes high
analogWrite (6, 127);      // Pin 6 oscillates regularly between 0v and 5v (or 3.3v) at about 250Hz.
2

analogWrite(): 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 on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.

For details visit: https://www.arduino.cc/en/Reference/analogWrite

analogRead(): Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference().

For details visit: https://www.arduino.cc/en/Reference/analogRead

1

digitalWrite set the specified pin to one of two states - HIGH/LOW

Where, HIGH = 5 V and LOW = 0 V

analogWrite Set the PWM value of PWM pin

(In Arduino UNO, PWM pins are 3, 5, 6, 9, 10, 11)

It will set the pin to a periodic high/low signal.

analogWrite(PWMpin,255)

Will be HIGH 100% of the time, while

analogWrite(PWMpin,127)

Will be HIGH 50% of the time, and LOW 50% of the time

0

So what's the difference between analogWrite(X, 255) and digitalWrite(X, HIGH)? Probably nothing, except perhaps the processor has to do some extra stuff to work out that it doesn't need to use PWM, and also style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.