Go Down

Topic: Digital output on Analogue pins. Quick question. (Read 174 times) previous topic - next topic

jonsmith9847

Hi,

Just a quick question. When I digital write to an analogue pin on the arduino Mega 2650, with the output be 5V digital or 5V PWM?

Thanks!

Wawa



MarkT

To my knowledge no analog input capable pins are PWM capable too on any of the Arduino
AVR microcontrollers, but there is no deep reason for this.  A pin can be attached to the ADC
or not, or to the standard PORT/DDR digital output circuitry, or to a timer's OCR outputs, you
just look up the datasheet to find out what's what for a new chip.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Delta_G

There is no 5V PWM.  At 5V it would 100% duty cycle, or in other words just be on.

Look at the source for analogWrite, if you give it 255 for the argument it calls digitalWrite.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

OldSteve

Look at the source for analogWrite, if you give it 255 for the argument it calls digitalWrite.
That's interesting. I guess that would apply to '0' too. Where would I find the source for analogWrite()? (And other 'built-in' functions like that.)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

jcallen

#6
Oct 28, 2015, 05:13 am Last Edit: Oct 28, 2015, 05:17 am by jcallen
On Linux its in:
usr/share/arduino/hardware/arduino/cores/arduino/wiring_analog.c

Delta_G

#7
Oct 28, 2015, 05:43 am Last Edit: Oct 28, 2015, 05:44 am by Delta_G
Here's the first part of analogWrite with the comments removed.

Code: [Select]


void analogWrite(uint8_t pin, int val)
{
     pinMode(pin, OUTPUT);
     
     if (val == 0)
     {
      digitalWrite(pin, LOW);
     }
     else if (val == 255)
     {
digitalWrite(pin, HIGH);
     }
     else
    {
                   // Here is where it actually sets up timers and creates PWM



You can see that it also calls pinMode for you.  So no pinMode needed for analogWrite-ing.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

MarkT

There is no 5V PWM.  At 5V it would 100% duty cycle, or in other words just be on.

Look at the source for analogWrite, if you give it 255 for the argument it calls digitalWrite.
What are you talking about?  5V PWM is a PWM signal which has a 5V HIGH level.  I've
never heard of calling a 50% 5V PWM signal as a "2.5V PWM signal", that would be
extremely confusing!

By your logic mains electricity would be a 0V AC waveform, since the average voltage
of the live conductor is 0V.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Delta_G

What are you talking about?  5V PWM is a PWM signal which has a 5V HIGH level.  I've
never heard of calling a 50% 5V PWM signal as a "2.5V PWM signal", that would be
extremely confusing!

By your logic mains electricity would be a 0V AC waveform, since the average voltage
of the live conductor is 0V.
Maybe I worded it poorly.  My point was that when you want 5V on an Arduino pin, it's not using PWM.  I wasn't making any larger theoretical inferences.  I only meant that output of 5V on an Arduino doesn't involve PWM. 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

Paul__B

Here's the first part of analogWrite with the comments removed.

Code: [Select]

void analogWrite(uint8_t pin, int val)
{
     pinMode(pin, OUTPUT);
    
     if (val == 0)
     {
     digitalWrite(pin, LOW);
     }
     else if (val == 255)
     {
 digitalWrite(pin, HIGH);
     }
     else
    {
                   // Here is where it actually sets up timers and creates PWM

You can see that it also calls pinMode for you.  So no pinMode needed for analogWrite-ing.
Which is interesting on a couple of counts.

One is that it is very involved and slow which may be of concern if you expected it to - act quickly!

The other is the reason for this - it makes sense that you simply cannot set timer modes to the null cases - always on or always off.

DrAzzy

PWM is only ever used when you use analogWrite(), and even then, PWM is only used on the 6 pins that support it - which are NOT the same as the "analog pins". (on other pins, 127 or less is the same as digitalWrite(pin,LOW) 128 or more digitalWrite(pin,HIGH) ).

The so-called "analog pins" are ones that work with analogRead(). Really, analogRead() and analogWrite() have little in common with eachother - one works with analog voltages, the other with PWM output, and they don't work on any of the same pins).

The "analog pins" can be used just like a digital pin, either with the number, or the A... number (ie, A0~A5 or 14~19). The only exception to this is on ATmega328p-based boards that have an A6 and A7 - those two pins are weird (i think unique among Atmel's product line in this regard) and can only be used with analogRead()
 
ATtiny core for 841+1634+828 and x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny 841/1634/828/88/861/167 breakouts, mosfets, touch sensors and prototyping board in my store http://tindie.com/stores/DrAzzy

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy