You definitely noticed that, for the same sensor, in some project we use Analog pin, but in some other project we use Digital pin. The analog pins on the Arduino can be used as digital outputs. There is another word or phrase – PWM. Pulse Width Modulation (PWM) is a method for getting analog results by digital means, like fading LED. Let us Discuss the Difference Between Analog and Digital Pins in Arduino UNO. You will get documentation of Arduino pin outs on official site :
Difference Between Analog and Digital Pins in Arduino UNO
digitalRead() works on all pins on Arduino UNO.
digitalRead() will simply round the analog value received and present it to you.
digitalWrite() has allowed parameter 0 or 1.
analogRead() works only with analog pins. It can accept any value between 0 and 1023. The analog pins let you read/write analog values – basically, instead of giving out a voltage of 0 or 5 (that is for digital pins), analog pins can give a range of voltages between 0 and 5, we can measure that output with a multimeter. Analog pins actually send pulses of 0V and 5V signals to get an output that feels analog (that is PWM). PWM is like pseudo-analog signal.
So when we need 0 or 1 as response, using
digitalRead() is practical. Whereas, to read sensor values,
analogRead() is practical.
analogWrite() works on all analog pins and all digital PWM pins. You can supply it any value between 0 and 255. Taking pin A1 as example, if
analogRead(A1) is greater than or equal to 512, then
digitalRead(A1) will be 1, otherwise it will be 0.
digitalWrite(A0,0) is the same as
digitalWrite(A0,1) is the same as
digitalRead() will return HIGH or LOW.
analogRead() will returns any number from 0-1023.
Analogue reads are usually very slow, but fairly precise. Pin A0-A5 are labelled Analog IN. This is primarily because these pins are used as input pins. They take inputs in the form of Analog signals, and return values between 0 and 1023. Arduino UNO has a 10 bit Analog to Digital converter to do that job.
An Analog to Digital converter works roughly in three stages – sampling, quantization and digitization. Arduino operates on a 0–5 volts range, it is 5/1023=0.00488volts or 4.88mV. In this way, a 4.88 mV input is 1, a 9.77 mV input is 2 and at end 5 V input is 1023. Below 4.88 mV is 0. Above 5 V can be risky for the board.