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…

1 Like

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

Hi @jtjojothomas123,

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

It switches the required pin on or off by setting it to the maximum voltage (3.3 V) or 0 volt.

Similar to digitalWrite, analogWrite can output any voltage between 0 and 3.3 volts as needed.

digital write(): is a command given from an user to the arduino board , for example when you connect an led to arduino , you can write(command or some sort of order) whether you really want the led to turn on or off , it’s actually like a pet, the arduino just receives the message from us which we put inside this command and it does what ever we just say
In simple it’s like our brain asking our hands take back when we touch fire.

analogRead(): This has nothing to do with the user, it’s all about the arduino itself, when we connect some analog devices such as sensors, the information which the sensor senses must be sent to the user, so this is done by the analogRead() command. It actually tells the user about the values which the sensors get . It’s used by us to read the information from an device connected to arduino. In a simpler way it’s like our brain receiving information from the sensory organs.

1 Like

The digitalWrite function takes two possible values, LOW and HIGH, in addition to a pin number. LOW sets the pin to 0v and HIGH to whatever the supply voltage is.

The analogWrite function takes a value of 0–255, in addition to a pin number. This number sets the mark/space ratio (time HIGH to time LOW) of a rectangular wave form, which is output on the specified pin. The waveform’s frequency is 490Hz default (and 980 Hz for pins 5 and 6 on the Uno). The default frequency can be changed by manipulating the onboard timers.

Varying mark/space ratio changes the average voltage. This can drive LEDS, and apparent brightness varies with the mark/space ratio. One can drive a resistor/capacitor network to smooth the output waveform, to get a varying voltage.

1 Like

DigitalWrite() gives the value only “1” OR “0” no intermediate values will be given or set to ‘High’ or ‘Low’ . In case of AnalaogWrite(), it gives the intermediate values that is 0 to 255 (the intensity of device ) and gives analog output at any of pwm (pulse width modulation) pin . ‘0’ indicates there is no intensity (off mode) ‘255’ indicates the maximum brightness of light

1 Like

digitalWrite () function controls Arduino’s pin to LOW or HIGH. Accordingly, We can use this function to turn on/off something such as: Turning on/off Solenoid Lock … analogWrite () function generates PWM signal to Arduino’s pin. Accordingly, We can use this function to control something such as: Controlling the angle of Servo moto
@jupally.poojitha @anmolbansal1512

1 Like