pwm pin in arduino

Thread Starter

denison

Joined Oct 13, 2018
328
If neither a high or low is sent to the pwm pin in the software is it in a high impedance state? Meaning it can not sink or source current. When I remove the low on the pin it must be in this high impedance state.
 

Papabravo

Joined Feb 24, 2006
21,158
If neither a high or low is sent to the pwm pin in the software is it in a high impedance state? Meaning it can not sink or source current. When I remove the low on the pin it must be in this high impedance state.
You have to be a little bit careful with your assumptions about pins. On most Atmel parts from the ATmega group (used extensively for Arduino family products) a given pin can have multiple functions associated with it. It also has a default condition that lasts from power on until one of the peripherals that is associated with that pin is initialized. Once that peripheral is initialized it takes over control of the pin and excludes all other peripherals from having anything to do with the pin. It's kinda like a giant custody fight and there can only be one winner. what happens to the pin at that point depends on which peripheral prevailed. Tell me the processor, and the pin, and I'll try to tell what I think will happen according to the datasheet.
 

Thread Starter

denison

Joined Oct 13, 2018
328
You have to be a little bit careful with your assumptions about pins. On most Atmel parts from the ATmega group (used extensively for Arduino family products) a given pin can have multiple functions associated with it. It also has a default condition that lasts from power on until one of the peripherals that is associated with that pin is initialized. Once that peripheral is initialized it takes over control of the pin and excludes all other peripherals from having anything to do with the pin. It's kinda like a giant custody fight and there can only be one winner. what happens to the pin at that point depends on which peripheral prevailed. Tell me the processor, and the pin, and I'll try to tell what I think will happen according to the datasheet.
The microcontroller is an atmega 328. in the set up loop I declare pin 12 an output. If a certain condition is fulfilled at a sensor input(a>46) I write a low to the pin. Want to know the state of the pin if the condition is not fulfilled and no low is written to the pin. I am hoping then that pin 12 will be high impedance. If it was a high for instance it would damage the peripheral it was attached to and perhaps draw to much current from pin 12.
 

KeithWalker

Joined Jul 10, 2017
3,063
You should initialize the output pin with a LOW as soon as you define the pin as an output, before the main loop of your program. If you don't, it will be floating and could cause problems.
Regards,
Keith
 

Papabravo

Joined Feb 24, 2006
21,158
The microcontroller is an atmega 328. in the set up loop I declare pin 12 an output. If a certain condition is fulfilled at a sensor input(a>46) I write a low to the pin. Want to know the state of the pin if the condition is not fulfilled and no low is written to the pin. I am hoping then that pin 12 will be high impedance. If it was a high for instance it would damage the peripheral it was attached to and perhaps draw to much current from pin 12.
The datasheet for the ATmega328 will tell you how much current pin 12 can source. It is unusual for an "output" pin to be Low or High Impedance. I think what you have to do is switch it back and forth between and Output Low and an Input (High Impedance). I think it is unlikely that current from a GPIO pin will damage an external device. Check the datasheet in any case.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
Not sure if it is an answer to your problem, but what I have done in the past when using Picmicro's is when I want to leave the PWM controller running but disable the output, I simply make the port pin an input instead of output.
And reverse to allow the PWM output.
I just saw that it what @Papabravo has suggested also.
Max.
 

Papabravo

Joined Feb 24, 2006
21,158
Not sure if it is an answer to your problem, but what I have done in the past when using Picmicro's is when I want to leave the PWM controller running but disable the output, I simply make the port pin an input instead of output.
And reverse to allow the PWM output.
I just saw that it what @Papabravo has suggested also.
Max.
In the Atmel architecture it is not clear to me if a pin is chosen as a PWM output, that the GPIO control registers have any effect at all over the pin. I think that if you want to make the pin an Input, you might have to find a way to trick the Output Compare/Input Capture peripheral into doing it in some fashion.

Turns out the DDR (Data Direction Register) controls the pin buffer output (1= output, 0=input) while the PORT data register is disconnected, and in the case of Pin 12 on the ATmega328P (28-pin package) the OC0A output is routed to the pin via PVOE and PVOV for PORTD pin 6.

There is also an additional proviso for pins on the ATmega family. If you want to switch between tri-state and Output High, or Input With Pullup and Output Low you must go through an intermediate state to get there. The intermediate state must have the Output Low or Input with pullup. Fortunately, the output bit can be indirectly set using the Force Output Compare function. I really don't want to write and debug this piece of code -- yeech!
 
Last edited:

John P

Joined Oct 14, 2008
2,025
It seems to me very wrong to switch an output pin into input (or hi-Z) mode while it's driving anything. If you do that, then the state that the external device sees would be undefined, which you have to assume causes the worst result--high, low, or oscillating--whatever you least want! Unless maybe you've got some sort of wire-or connection, where output A drives the load directly, and output B drives it via a resistor; then if A is active, it dominates, but if A is in hi-Z mode, B gets control. That's an unusual situation, though.

If the output is a PWM signal for instance, most likely the best way to "turn it off" would be to force it low, which on a PIC processor is easily done, just by setting a 0% duty cycle.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
It seems to me very wrong to switch an output pin into input (or hi-Z) mode while it's driving anything.
If the output is a PWM signal for instance, most likely the best way to "turn it off" would be to force it low, which on a PIC processor is easily done, just by setting a 0% duty cycle.
I originally did it on the advice of an App note, in my case the PWM output goes to the gate of a Mosfet which has a gate pull down resistor so it always turns off.
Works fine so far.
Max.
 

upand_at_them

Joined May 15, 2010
940
Typical way of turning off PWM is to either set the duty cycle to zero, or turn it off in its config register. In the case of PIC's the latter should reset the PWM at the end of the period. This is what I do.
 

Papabravo

Joined Feb 24, 2006
21,158
In the ATmega328P there are a pair of bits in the control register that return the output pin to the control of the PORT DATA register.
 

upand_at_them

Joined May 15, 2010
940
What ever works for you! ;)
So far I have had no problem and it is very simple and after immediately returns to the programed PWM rate.
Max.
This may depend on which PIC is being used, as peripherals on [perhaps newer?] PICs can take control of the TRIS. The UART, for example, once enabled is supposed to set TRIS without the programmer doing it.
 

Thread Starter

denison

Joined Oct 13, 2018
328
It seems to me very wrong to switch an output pin into input (or hi-Z) mode while it's driving anything. If you do that, then the state that the external device sees would be undefined, which you have to assume causes the worst result--high, low, or oscillating--whatever you least want! Unless maybe you've got some sort of wire-or connection, where output A drives the load directly, and output B drives it via a resistor; then if A is active, it dominates, but if A is in hi-Z mode, B gets control. That's an unusual situation, though.

If the output is a PWM signal for instance, most likely the best way to "turn it off" would be to force it low, which on a PIC processor is easily done, just by setting a 0% duty cycle.
I could have been more specific. I am using 2 pwm output pins as you suggest. I am driving an opto-coupler. One of the outputs is coupled to the opto via a resistor. The other output is connected directly to the led in the opto. Then if the condition is satisfied on a sensor input of say AO> 50 a low is applied directly to the led which stops the opto turning on even if there is a high at the output of the other output pin. But for this to work properly when A0<50 the output of the direct connection to the led must be hi-Z. Is this what would happen? I may have to design a program to test this if nobody knows.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
This may depend on which PIC is being used, as peripherals on [perhaps newer?] PICs can take control of the TRIS. The UART, for example, once enabled is supposed to set TRIS without the programmer doing it.
One was a small 12F683 and just "bsf TRISIO, GP2."

But for this to work properly when A0<50 the output of the direct connection to the led must be hi-Z. Is this what would happen? I may have to design a program to test this if nobody knows.
Not sure if you can do the same with Arduino, never used it.
Max.
 
Top