What is the diffrence between DigitalWrite() and AnalogWrite()?

DigitalWrite(): It is a command that is given by the user to the arduino board.

AnalogWrite(): When we connect an analog device to arduino board, the information that this devices get is sent to the user by using the analogRead() command.

1 Like

digitalWrite() and analogWrite() are two functions in Arduino programming language that are used to control the output pins of the Arduino board.

The main difference between digitalWrite() and analogWrite() is that digitalWrite() is used to set the state of a digital output pin to either high (5V) or low (0V), while analogWrite() is used to generate a PWM (Pulse Width Modulation) signal on a digital output pin
@niranjanchinnu262200 @anmolbansal1512

1 Like

The functions digitalWrite() and analogWrite() in the Arduino programming language are used to control the output of a digital or analogue pin, respectively. The key distinction between them is that digitalWrite() only has two possible output states: HIGH or LOW, but analogWrite() can output values ranging from 0 to 255.

digitalWrite() is used for digital output and can be used to turn a pin on or off, set it to a high voltage level (often 5 volts) or a low voltage level (typically 0 volts), or transition it between these two states quickly. (known as “pulsing” or “PWM” for pulse width modulation).

AnalogWrite(), on the other hand, is used for analogue output and can be used to generate a voltage on a device.

1 Like

DigitalWrite() : This function is used to set the output of a digital pin to either HIGH or LOW. The digital pins on Arduino boards can only output binary states, meaning they can be either HIGH (5 volts) or LOW (0 volts).

AnalogWrite(): Unlike digital pins, which can only output binary states, certain pins on Arduino boards are capable of providing analog output.

The DigitalWrite()and AnalogWrite() functions are specific to the Arduino programming language and are used to control digital and analog pins, respectively, on Arduino boards.

1 Like

The functions digitalWrite() and analogWrite() are used in Arduino programming to control the output of digital and analog pins, respectively.

  1. digitalWrite():

    • digitalWrite() is used to set the output state of a digital pin.
    • It takes two arguments: the pin number and the desired state (HIGH or LOW).
    • It can only set the pin state to two discrete values: HIGH (5V) or LOW (0V).
    • Typically used for turning ON/OFF LEDs, controlling relays, or any other digital output devices.
  2. analogWrite():

    • analogWrite() is used to provide a variable output voltage on a PWM (Pulse Width Modulation) enabled pin.
    • It takes two arguments: the pin number and the desired output value (ranging from 0 to 255).
    • It generates a square wave with a fixed frequency but varying duty cycle to simulate an analog output.
    • The output value determines the duty cycle of the square wave, which in turn controls the average voltage output.
    • The PWM technique allows the creation of intermediate voltage levels between HIGH and LOW, enabling smooth control of devices like motors, dimming LEDs, or generating audio tones.

In summary, digitalWrite() is used for binary digital control (ON/OFF), while analogWrite() is used for simulating analog control by providing a variable voltage level using PWM.

1 Like

In Arduino programming, digitalWrite() and analogWrite() are two functions used to control the output pins of the microcontroller. The main difference between them lies in the type of output they provide.

  1. digitalWrite():

    • The digitalWrite() function is used to set the digital output pins of the Arduino to either HIGH (5V) or LOW (0V).
    • It is suitable for controlling digital devices that have binary states, such as LEDs, relays, or digital sensors.
    • The output provided by digitalWrite() is a square wave with a fixed voltage level (either 5V or 0V) and a fixed duty cycle of 50%. It does not allow for varying the intensity or level of the output signal.
  2. analogWrite():

    • The analogWrite() function is used to set the analog output pins of the Arduino to a specific voltage level between 0 and 5V (for a typical Arduino board).
    • It is commonly used for controlling analog devices, such as servo motors, LEDs with variable brightness, or analog actuators.
    • The output provided by analogWrite() is a Pulse Width Modulation (PWM) signal. PWM generates a square wave with varying duty cycle, allowing the average voltage level to be controlled. By adjusting the duty cycle, the perceived intensity or level of the output signal can be varied.

To summarize, digitalWrite() is used for digital control, providing a binary output (either HIGH or LOW), while analogWrite() is used for analog control, providing a variable voltage output (through Pulse Width Modulation) to control the intensity or level of analog devices.

1 Like

DigitalWrite() gives the value only “1” OR “0” as high or low .No intermediate values will be given. It can be used to turn on a LED, switch a relay etc.
AnalaogWrite() gives the intermediate values (writes an analog value 0–255 to a pin) .It can be used to light a LED at varying brightness or drive a motor at various speeds.

1 Like

In Arduino programming, the functions digitalWrite() and analogWrite() are used to control the output of pins on the Arduino board, but they have different purposes and behaviors.

digitalWrite(): This function is used to write a digital value (HIGH or LOW) to a digital pin on the Arduino board. It can be used with any pin that supports digital I/O. The digital pins are typically used for binary on/off signals. When you use digitalWrite(pin, HIGH), the pin is set to a logic HIGH voltage level (typically 5 volts), and when you use digitalWrite(pin, LOW), the pin is set to a logic LOW voltage level (typically 0 volts). This function is commonly used for controlling LEDs, relays, or other digital devices.

analogWrite(): This function is used to write an analog value (0 to 255) to a digital pin on the Arduino board. However, despite the name, analogWrite() does not produce a true analog output. It uses a technique called Pulse Width Modulation (PWM) to simulate analog voltages. PWM rapidly switches the pin between HIGH and LOW states in a specific pattern, creating an average voltage that appears like an analog value. It is commonly used for dimming LEDs, controlling the speed of motors, or generating audio tones.

1 Like

with addition to this I would like to add that digitalWrite requires you to set the pin to output using pinMode, analogWrite sets the pin to output on each call, which makes it slower and might ruin the code which uses the same pin alternatively as input and output, and relies on the fact that writing to the input pin using digitalWrite doesn’t switch it to output.

DigitalWrite() will set the output to one of two states , LOW or HIGH.It can be used to turn on a LED, switch a relay etc. AnalogWrite() writes an analog value 0–255 to a pin. It can be used to light a LED at varying brightness or drive a motor at various speeds.
Hoping that you have understood clearly.

Hi @jtjojothomas123
In the name itself, we can get an idea: Digital is always True or false, like 0 or 1.
but the analog will vary from 0 to 255 where you can alter the output with many values.

digitalWrite() is used to set a digital pin’s output state to HIGH or LOW, enabling basic on/off control for devices like LEDs . It works with binary signals.

analogWrite() generates a PWM (pulse-width modulation) signal on a pin, allowing control over the analog-like behavior of components such as LEDs’ brightness or motor speed. It creates varying levels of voltage by rapidly toggling between HIGH and LOW states.

In essence, digitalWrite() is for straightforward digital control, while analogWrite() simulates analog control using PWM. Both functions are fundamental in microcontroller programming.

Digital Write:

Digital Write allows you to set a digital pin to one of two states: HIGH or LOW.
When you set a digital pin high it gives a logic high voltage (usually around the supply voltage like 5V) and when you set it low it gives a logic low voltage (usually close to 0V).
This is useful for turning devices such as LEDs, relays, and transistors on and off.
Analog Record (Pulse Width Modulation - PWM):

Analog Record is used to simulate a change of variable through a process called Pulse Width Modulation to producean analog-like result.The
PWM interrupt quickly switches the output pin between the high and low states so that the average voltage appears between 0V and the supply voltage over a period of time.
By adjusting the duty cycle (the ratio of the time the pin is high to the total time of the PWM **cycle),the apparent voltage of the pin can be effectively controlled.
PWMs are often used for tasks such as dimming LEDs, controlling motor speed and making noise.

DigitalWrite() will change the output to either a LOW or HIGH state. It can be utilized to switch a relay, activate an LED, etc.
AnalogWrite() writes an analogue value between 0 and 255 to a pin. It can be used to run a motor at different speeds or light an LED at different intensities.

digitalWrite() sets a digital pin to either HIGH or LOW (1 or 0). analogWrite() creates a PWM (Pulse Width Modulation) signal on a capable pin, simulating an analog voltage between 0 and 255.

digitalWrite gives only 2 output voltages. Either 0V OR 5V. It basically has either High or Low voltage.

analogWrite can give Multiple output voltages from 0V - 5V depending to the pin used and the range. So this function has 2 variables (pin and range)

analogWrite(pin, range);
where the range is 0- 255
analogWrite(3, 0) gives 0 Voltage
analogWrite(3, 255) gives 5V
and if you want and intermediate voltage then
analogWrite(3, 127) gives 2.5V