Difference between Analog and Digital Pins on my Arduino Board

Click For Summary
Analog pins on an Arduino are designed for reading analog voltage values through an analog-to-digital converter, typically ranging from 0 to 5V. In contrast, digital pins serve primarily as output, allowing users to control devices like LEDs and buzzers by sending HIGH or LOW signals. Digital pins can also function as inputs, especially for detecting interrupts, with specific pins designated for this purpose. While analog pins are limited to reading values, digital pins can perform both reading and writing operations, including generating PWM signals with the analogWrite() function. Understanding these differences is crucial for effective Arduino programming and hardware interaction.
ISamson
Gold Member
Messages
435
Reaction score
150
I wanted to ask what the difference between analog and digital pins is on my Arduino (or any other board). I have been using both but don't really know what the difference is.
Thanks!
 
Engineering news on Phys.org
Analog pins are the ADC (analog to digital converter) input pins. They are used for reading analog voltage (between 0-5V on arduino, by default). Check out the sample program for analogRead() command.

Digital pins are used mainly as output pins. You can connect various devices (LEDs, buzzer, LCD) on the digital pins and turn them on/off by writing HIGH or LOW on the respective pins. Check out digitalWrite() command. Before using a digital pin, you have to set it in input/output mode using pinMode() command. You can use a digital pin in input mode as well, especially when using interrupts in your code. Digital pins 2 and 3 of UNO can be used for interrupt detection. In general, you can check the status (on or off) of a device (or a switch) connected to a digital pin by using that pin in input mode. The command used is digitalRead().
 
  • Like
Likes ISamson
Perhaps two things worth to add, as I have seen people confused about them:

1. Some pins can be used as either analog or digital, their use is not predefined in the hardware.

edit: this is wrong, see detailed explanation below:
2. There is no analogWrite() command (in other words: analog pin can be used only for reading, while digital pin can be used for both reading and writing).
 
Last edited:
  • Like
Likes ISamson
Borek said:
2. There is no analogWrite() command (in other words: analog pin can be used only for reading, while digital pin can be used for both reading and writing).

I have once used the analogWrite() and it seemed working fine to me...
Could this be true?
 
cnh1995 said:
Analog pins are the ADC (analog to digital converter) input pins. They are used for reading analog voltage (between 0-5V on arduino, by default). Check out the sample program for analogRead() command.

Digital pins are used mainly as output pins. You can connect various devices (LEDs, buzzer, LCD) on the digital pins and turn them on/off by writing HIGH or LOW on the respective pins. Check out digitalWrite() command. Before using a digital pin, you have to set it in input/output mode using pinMode() command. You can use a digital pin in input mode as well, especially when using interrupts in your code. Digital pins 2 and 3 of UNO can be used for interrupt detection. In general, you can check the status (on or off) of a device (or a switch) connected to a digital pin by using that pin in input mode. The command used is digitalRead().

Borek said:
Perhaps two things worth to add, as I have seen people confused about them:

1. Some pins can be used as either analog or digital, their use is not predefined in the hardware.

2. There is no analogWrite() command (in other words: analog pin can be used only for reading, while digital pin can be used for both reading and writing).

Very useful. Thank you!
 
ISamson said:
I have once used the analogWrite() and it seemed working fine to me...
Yes, and it generates a PWM signal on that pin.
 
  • Like
Likes ISamson
cnh1995 said:
Yes, and it generates a PWM signal on that pin.

Really!? The analogWrite() generates PMW. Wow!
 
ISamson said:
I have once used the analogWrite() and it seemed working fine to me...

OK, I was wrong with my wording, my mistake. What I meant was it works in a completely different way.

What you read with the analogRead() command is a value (from 0..1023 range) that tells you the value of the voltage (as @cnh1995 wrote, in reference to the voltage used to run the processor, so typically 5 V on the Arduino). When you attach a 3 V battery to the Arduino leg and you read it with analogRead() you will get something around 3/5*1023 = 614. It doesn't work the other way - when you use analogWrite() you will not get other voltage than 5 V on the output leg, rather it i will be using PWM modulation - switching the voltage on and off pretty quickly, so that it is "on" only part of the time. Net effect is that for example the LED seems to be working at half the brightness, because it really works only part of the time.
 
  • Like
Likes ISamson and cnh1995
Borek said:
When you attach a 3 V battery to the Arduino leg and you read it with analogRead() you will get something around 3/5*1023 = 614. It doesn't work the other way - when you use analogWrite() you will not get other voltage than 5 V on the output leg, rather it i will be using PWM modulation - switching the voltage on and off pretty quickly, so that it is "on" only part of the time. Net effect is that for example the LED seems to be working at half the brightness, because it really works only part of the time.
That's right. So if you want to use analogWrite() for D/A conversion, you'll have to use some filter circuitry to convert high frequency PWM signal into proportional dc voltage. So I believe it is easier to make one separate DAC (R-2R ladder) and use it with arduino's digital pins.

(I recently built a 6-bit R-2R DAC and generated sinewave signal (fixed magnitude- variable frequency) from it. It was fun!)
 
  • Like
Likes ISamson
  • #10
cnh1995 said:
(I recently built a 6-bit R-2R DAC and generated sinewave signal (fixed magnitude- variable frequency) from it. It was fun!)

Resister ladders are tricky (accuracy!) but yes, they are simple and as long as you are OK with just a few bits they can work quite good. No idea how old you are, does the name Covox ring a bell? I got one made for me by a friend of mine around 1990 :smile:
 
  • Like
Likes cnh1995
  • #11
Borek said:
Covox

Who/what is he/she/it?
 
  • #13
Borek said:
No idea how old you are, does the name Covox ring a bell?
22
Borek said:
I got one made for me by a friend of mine around 1990 :smile:
I'll look it up. Thanks!
 
  • Like
Likes ISamson

Similar threads

Replies
7
Views
2K
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
Replies
5
Views
2K
Replies
80
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K