Newbie question

Hi, this question may be answered over 9000 times, but I cannot find any answer.

What's the main difference between digital and analogic outputs?
If I connect a LED to digital 12 pin and I say

digitalWrite(12, HIGH);

Should the LED turn on?

And if I connect a LED to analogic 4 pin (for example) and I write

analogicWrite(4, HIGH);

Should the LED turn on or shouldn't it?

I hope you can help me! :slight_smile:

From one noob to an other:
As far as i understand it - or learned it - Digital ports are only having to states: ON or OFF (High, Low) - and Analog Ports should have a range of Values (e.g. 0 to 255). Might be possible that High and Low have corresponding values for Analog Ports, but i dont know that (and cant test it since i havent received my Arduino yet)

If i am wrong, please slap me with an Arduino manual ;0)

Okay, so with analog ports we can regulate intensity?

Also, slap me with other Arduino manual xD

i would dare to say so?

The analogue ports on the Arduino are only input ports not output ports.
There is no analogue output port on an Arduino.
However, some digital pins can work in the PWM mode. This switches the LED on and off rapidly and so allows it to be dimmed.
Look in the playground for this. Start here:-

Ok, so you can use analogWrite() with digital pins?

I'm absolutely newbie, since I haven't received my Arduino yet

ok, thank you :0)

digitalWrite will set a pin which has been enabled for output to a 1 or a 0. In analog terms this would be 0=0 or 1=255. But for the digital function the pin is either set to 0V or 5V for 0 or 1 respectively.

The analogWrite function works on PWM output pins. It actualy sets the PWM output percentage with 0=0% high and 255=100% high. This can be used as an analog output depending on what you are connecting it to. Most things will work just fine. LEDs for example do not need a hardware filter. If you select a PWM output enabled pin and connect the LED there, use analogWrite to control the brightness of that LED.

I have some more information about using PWM pins for analog output in an article at uCHobby. https://web.archive.org/web/20190610044417/http://www.uchobby.com/index.php/2008/01/08/arduino-audio-dac-options/

Does this help?

It does.
So all digital pins are PWM enabled?
If so: digitalWrite(12, 1); will have the same effect as analogWrite(12,255); ?

Thanks!

No not all the Digital lines are PWM capable. Digital lines 3, 5, 6, 9, 10, and 11 are PWM. You should see the PWM label on the Arduino board for these pins.

Ok heres another newbie question since this thread is live...

Can you increase the frequency of the PWM rather than the duty %? The reference claims 490hz which isnt fast enough for what I require - 4khz would be the desired number :slight_smile:

G.

Hmm the data sheet for the chip claims the PWM output has 250khz to maintain the 255 step resolution - so I presume its possible...

G.

Grumpy_Mike:

What you are saying is not completely true. Analog pins can be used for output. They can be used as digital outputs. They are mapped as pins 14-19 for output.

So using the ops example, if I wanted to set analog pin 4 high.

pinMode(17, OUTPUT); //13+4
digitalWrite(17, HIGH);

remember to change the pin to an output ping using pinmode. If you don't do that, the pin still acts like an input but with its internal pull up resistor enabled

What you are saying is not completely true.

Can you point to the bit in my reply you believe not to be true?

Yes you can use analogue input pins as digital inputs or outputs, I do that all the time if you have ever seen any of my projects:-
http://www.thebox.myzen.co.uk/Hardware/Projects.html

But that was not the question, the original question stated:-

What's the main difference between digital and analogic outputs

I pointed out that there is no analogue output on this controller but there was a PWM digital one.

That is not to be confused with saying there is no output possible from an analogue input pin. Which is an altogether different statement. Being dyslexic I believe words are important and they should say what they mean not hint ambitiously. So please try and read what the words say.

wow this is heavy. I didn't mean in any way to offend you.

In your original post you stated:

The analogue ports on the Arduino are only input ports not output ports.

I was just pointing out that the analog ports can be used for digital output.

I have used your website on many occasions and find it very helpful. I've refered many people to your tutorial section.

Maybe saying "not completely true" was a little harsh. However, the quoted statement above is "not completely true". I'm sorry if I offended you, it was not the goal of my post. My goal was just to inform the op that he can use the analog port for output if he runs out of pins.