Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Difference between Analog and Digital Pins on my Arduino Board

  1. Oct 28, 2017 #1

    ISamson

    User Avatar
    Gold Member

    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!
     
  2. jcsd
  3. Oct 28, 2017 #2

    cnh1995

    User Avatar
    Homework Helper
    Gold Member

    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().
     
  4. Oct 28, 2017 #3

    Borek

    User Avatar

    Staff: Mentor

    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: Oct 28, 2017
  5. Oct 28, 2017 #4

    ISamson

    User Avatar
    Gold Member

    I have once used the analogWrite() and it seemed working fine to me...
    Could this be true?
     
  6. Oct 28, 2017 #5

    ISamson

    User Avatar
    Gold Member

    Very useful. Thank you!
     
  7. Oct 28, 2017 #6

    cnh1995

    User Avatar
    Homework Helper
    Gold Member

    Yes, and it generates a PWM signal on that pin.
     
  8. Oct 28, 2017 #7

    ISamson

    User Avatar
    Gold Member

    Really!? The analogWrite() generates PMW. Wow!
     
  9. Oct 28, 2017 #8

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  10. Oct 28, 2017 #9

    cnh1995

    User Avatar
    Homework Helper
    Gold Member

    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!)
     
  11. Oct 28, 2017 #10

    Borek

    User Avatar

    Staff: Mentor

    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:
     
  12. Oct 28, 2017 #11

    ISamson

    User Avatar
    Gold Member

    Who/what is he/she/it?
     
  13. Oct 28, 2017 #12

    Borek

    User Avatar

    Staff: Mentor

  14. Oct 28, 2017 #13

    cnh1995

    User Avatar
    Homework Helper
    Gold Member

    22
    I'll look it up. Thanks!
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?