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

i am getting bit conused on the voltage and what is use and meaning behind both DigitalWrite() and AnalogWrite()

digitalWrite gives only output voltage of 0V OR 5V .

1, digitalWrite(1,HIGH);

2, digitalWrite(1,LOW);

analogWrite can give output voltage from 0V - 5V ( OUTPUT CAN BE 1V , 1.5V,3V, 3.3V 4.3V, 4.9V, 5V)

1, analogWrite(pin, range);

range: 0- 255

eg : analogWrite(3, 0); ———-> output voltage= 0V

analogWrite(3, 127); —→ output voltage = 2.5 v

analogWrite(3, 255); ———→ output voltage= 5v

Hope this helps…

oh okay, I understood a bit now, Thank you for your answer

DigitalWrite() gives the value only “1” OR “0” no intermediate values will be given
AnalaogWrite() gives the intermediate values

digitalWrite is a simple function. Simply put, it sets the desired pin to maximum voltage(3.3 V) or 0 volt, in other words, turns it on or off.

analogWrite is similar to digitalWrite, but it can provide desired any voltage between 0V to 3.3V. It provides 256 different voltage levels including 0V. See the following example to better understand.

Let’s say I want approximately 1.2V on a pin.
Smallest voltage step possible=(3.3-0)V/255(excluding 0V)= 0.0129V
So if I want 1.2V, let no of steps required be x
x * smallest step = 1.2V
x * 0.0129V = 1.2V
x= 93(Approx, remember it should be a whole number between 0 and 255)
so I would write analogWrite(‘pin’,‘93’)

Now internally the way analog write works is by using Pulse Width Modulation(PWM). Check this video out to learn more.
But if you are a beginner you don’t have to worry about it too much. But if you are using Motors/anything that uses coils then having knowledge about PWM would be necessary and you shall also know about the effect of sudden current changes in inductors, but don’t worry about this too much if you are controlling small LEDs and buzzers and all that.

1 Like

@raghav.srivastava Thank you so much for your reply

@201601014 Thank you so much for the response

digitalWrite() function turns any I/O pin high or low and analogWrite() function gives analog output at any of PWM (pulse width modulation) pin

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.

digitalwrite function sets digitalpin 0 or 1 i.e. low or high respectively. but analogwrite function has value range of 0-255.
eg) if you just want to on or off led pin then you will use digitalwrite function , but if you want different shades of led then voltage provided to led needs to be controlled that is done by analogwrite function

digitialWrite() and analogWrite() are functions to OUT signals on the selected ports.

digital write is used for on/off jobs like turning on a LED or putting it off. Dimming a LED could not be done using digital write but rather an analog write.

Analog signal varies in its magnitude which make it suitable for controlling variable speed motors or dimming a LED.

digitalWrite() function is used to set the state of a device (ON/OFF). It accepts only boolean values i.e “TRUE” or “FALSE”
analogWrite() function is used to alter the Magnitude , Eg: Dimming a light , Changing the frequency of Buzzer. It accepts any value in the range of 0-255

digitalWrite() function turns any I/O pin high or low and analogWrite() function gives analog output at any of PWM (pulse width modulation) pin

DigitalWrite() will sets your output voltage to one of two states , LOW or HIGH, like a boolean or binary. It provides either 0V (low) or 3.3V (high) to the output device connected to the Bolt Wifi module.
Functions- ON- OFF cases output devices like LED.

AnalogWrite(), on the other hand writes an analog value 0–255 to a pin. It can be used to modulate the voltage anywhere between 0 to 5V, anywhere in between.
Functions- Modulated voltage for different outputs, like buzzers

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

digitalWrite() and analogWrite() are functions in Javascript and are used with Bolt Iot for :

digitalWrite() :- This function is used to send output to our connected components such as Buzzer and Led (which is provided to us in the kit). So to output these components we use this function.
digitalWrite(pin,‘value’) , pin and value are the 2 parameters passed to the function.

analogWrite():- This function used to give analog values (0-255) to the components.
eg:- we had used this function when we wanted to control the brightness of our LED.
and the above example is know as Pulse Width Modulation (PWM) technique where the width
of digital pulses is adjusted to generate different average dc voltages.

You can find the function here https://cloud.boltiot.com/static/js/boltCommands.js