Difference between Analog and Digital Pins on my Arduino Board

In summary, analog pins are used for reading analog voltage while digital pins are mainly used for output. Digital pins can also be used for input and interrupt detection, and can check the status of devices connected to them. Some pins can be used as either analog or digital, and there is no analogWrite() command for analog pins. Instead, analogWrite() uses PWM modulation to simulate different voltage levels.
  • #1
ISamson
Gold Member
438
151
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
  • #2
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
  • #3
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
  • #4
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?
 
  • #5
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!
 
  • #6
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
  • #7
cnh1995 said:
Yes, and it generates a PWM signal on that pin.

Really!? The analogWrite() generates PMW. Wow!
 
  • #8
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
  • #9
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

1. What is the main difference between analog and digital pins on my Arduino board?

The main difference between analog and digital pins on an Arduino board is the type of signal they can read or write. Analog pins can read or write continuous analog signals, while digital pins can only read or write binary signals (0 or 1).

2. How many analog and digital pins does an Arduino board have?

The number of analog and digital pins on an Arduino board varies depending on the model. Most Arduino boards have 6 analog pins labeled A0-A5 and 14 digital pins labeled 0-13. However, some models may have more or fewer pins.

3. Can I use a digital pin as an analog pin or vice versa?

No, you cannot use a digital pin as an analog pin or vice versa. Each pin has a specific purpose and function, and they are not interchangeable. Attempting to use a pin for a different purpose may result in incorrect readings or damage to the board.

4. What are some examples of devices that use analog and digital signals?

Analog signals are commonly used in sensors such as temperature sensors, light sensors, and potentiometers. Digital signals are used in devices such as switches, buttons, and LEDs.

5. How do I know if a pin on my Arduino board is analog or digital?

The pins on an Arduino board are usually labeled to indicate whether they are analog or digital. Analog pins are labeled with the letter A (e.g. A0, A1, A2), while digital pins are labeled with a number (e.g. 0, 1, 2). You can also refer to the pinout diagram for your specific Arduino board.

Similar threads

Replies
7
Views
1K
Replies
5
Views
2K
Replies
10
Views
1K
  • Electrical Engineering
Replies
6
Views
945
  • Electrical Engineering
Replies
15
Views
2K
Replies
80
Views
3K
Replies
5
Views
940
  • Electrical Engineering
Replies
1
Views
1K
  • Electrical Engineering
2
Replies
38
Views
3K
  • Electrical Engineering
Replies
3
Views
1K
Back
Top