PWM using controller

Thread Starter

RenesasT1

Joined Jul 6, 2017
107
Hello All,

I am working on one project which requires following condition.

Analog voltage and PWM on same pin of microcontroller.

Does it possible??

Currently I am using one pin of microcontroller. If analog voltage at that pin is 0-5V . I am converting 0-5V from internal ADC and getting values between 0-1023 as my adc is of 10 bit. Using those values, I am generating PWM and varying duty according to analog value.

Now I wanted to use the same pin which should accept pwm and according to the duty cycle of i/p pwm it should vary the duty cycle of o/p Pwm.

In case I am using analog then it should work as analog control and if I am using PWM then it should work as PWM control.

Does it possible using one pin.

Please let me know How??
 

LesJones

Joined Jan 8, 2017
4,511
You could just put a low pass filter (Resistor and capacitor.) in series with the input. This will average the PWM duty cycle to a DC voltage. This method is used in the PICkit2 for controlling the target voltage. You will have to choose tha values that are suitable for the PWM frequency and the maximum rate of change of the input value.

Les.
 

Thread Starter

RenesasT1

Joined Jul 6, 2017
107
Hi Les,

There are two methods of controlling smaller LED.

1) Using ADC
2) Using PWM

In first method, suppose on P1.0 the i/p is 0-5V and I will convert that into analog value from 0-1023 as my internal ADC is of 10 bit. According to the ADC value, I am generating PWM of 100Hz on P1.1 which is connected to LED. So the ADC value on P1.0 will decide the dutycycle of PWM of P1.1.

In second method, the i/p is square wave PWM at P1.0. The frequency of i/p PWM is user dependable. I wanted to capture the duty cycle of i/p PWM at P1.0 and according to generate PWM of 100Hz on P1.1 which is connected to LED. So the duty cycle of PWM on P1.0 will decide the dutycycle of PWM of P1.1.

I wanted to use only one pin as i/p and other pin as o/p.

The o/p operation is same to generate PWM of 100Hz whose duty is depends upon the i/p.

But for i/p , How I can do?
 

jayanthd

Joined Jul 4, 2015
945
Hi Les,

There are two methods of controlling smaller LED.

1) Using ADC
2) Using PWM

In first method, suppose on P1.0 the i/p is 0-5V and I will convert that into analog value from 0-1023 as my internal ADC is of 10 bit. According to the ADC value, I am generating PWM of 100Hz on P1.1 which is connected to LED. So the ADC value on P1.0 will decide the dutycycle of PWM of P1.1.

In second method, the i/p is square wave PWM at P1.0. The frequency of i/p PWM is user dependable. I wanted to capture the duty cycle of i/p PWM at P1.0 and according to generate PWM of 100Hz on P1.1 which is connected to LED. So the duty cycle of PWM on P1.0 will decide the dutycycle of PWM of P1.1.

I wanted to use only one pin as i/p and other pin as o/p.

The o/p operation is same to generate PWM of 100Hz whose duty is depends upon the i/p.

But for i/p , How I can do?
You mean to say both Analog input and PWM input are connected to same pin and you need both these inputs without a switch ?
Which MCU are you using ? 8051 variant ?
 

LesJones

Joined Jan 8, 2017
4,511
So are you saying that the frequency can be anything. If I guess at the lowest PWM input frequency being 0.1 hz then I would suggest that the low pass filter should have a time constant of about 100 seconds to smooth out the PWM input signal. This will mean it will have a very slow responce to changes on PWM duty cycle or changes in analog input voltage. This idea will only work if the input PWM waveform goes from zero volts when it is low and exactly +5 volts when it is high. If you give some background to the purpose of this device then members will probably be better able to make suggestions.

Les.
 

Thread Starter

RenesasT1

Joined Jul 6, 2017
107
Hello LesJones,

The i/p frequency range is from 50Hz to 1kHz.

i wanted to run motor at o/p. The o/p frequency is fixed i.e. 100Hz and I am able to generate it.

Now I wanted to change duty according to the i/p PWM duty cycle.

I am able to generate the operation o/p PWM of 100Hz.

Suppose I am taking analog 0-5V as a i/p then I am able to generate 100Hz frequency on other port of controller i.e o/p with duty according to i/p voltage.

Now the o/p is 100Hz frequency(which I can generate). The problem is that now, i/p is PWM square wave. According to duty at i/p , I wanted to set the duty at o/p.
 

LesJones

Joined Jan 8, 2017
4,511
The average voltage of a PWM signal is the voltage of the high state multiplied by the duty cycle. So if you have a PWN signal with 50% duty cycle with a low level of zero volts and a high level of +5 volts then the average level is 2,5 volts. Passing it through a low pass filter converts it into a DC voltage. The DC voltage will have some ripple on it. The amount will depend on the low pass filter. As your lowest input frequency is 50 Hz I would suggest a low pas filter with a cut off frequency of about 5 Hz. This will slow down the response to changes in PWM duty cycle and changes in analogue input voltage when feeding it with an analogue signal.

Les.
 

jayanthd

Joined Jul 4, 2015
945
The average voltage of a PWM signal is the voltage of the high state multiplied by the duty cycle. So if you have a PWN signal with 50% duty cycle with a low level of zero volts and a high level of +5 volts then the average level is 2,5 volts. Passing it through a low pass filter converts it into a DC voltage. The DC voltage will have some ripple on it. The amount will depend on the low pass filter. As your lowest input frequency is 50 Hz I would suggest a low pas filter with a cut off frequency of about 5 Hz. This will slow down the response to changes in PWM duty cycle and changes in analogue input voltage when feeding it with an analogue signal.

Les.
RC low pass filter is a solution as mentioned. You can read the converted voltage using ADC and set the duty for output pwm.

Cut of frequency of 5 Hz or 5 KHz ?
 

jayanthd

Joined Jul 4, 2015
945
As lesjones told me to use 5hz fc. Suppose i am using 50Hz as Fc. What it will affect on my circuit?
Why considering low frequency. You max frequency is 1 KHz. You have to consider that for your cut-off frequency calculation because your low pass filter should be able to pass 1 KHz single through its output. Anything above 1 KHz or 3db cut-off frequency should be passed to ground.

Do you have proteus to plot graph of PWM frequency vs RC low pass filter output voltage ?
 

Thread Starter

RenesasT1

Joined Jul 6, 2017
107
Yes Jayanthd,

That's what I wanted to say. I have made RC low pass filter.

I have chosen Fc as 1khz.
R=16 ohm
C=10 uF

Also

R=3.3ohm
C=47uF

But I am not getting proper O/p. I am getting fluctuations in DC voltage
 

jayanthd

Joined Jul 4, 2015
945
Yes Jayanthd,

That's what I wanted to say. I have made RC low pass filter.

I have chosen Fc as 1khz.
R=16 ohm
C=10 uF

Also

R=3.3ohm
C=47uF

But I am not getting proper O/p. I am getting fluctuations in DC voltage
Don't use a value for capacitor which comes in the range of electrolytic capacitors like 10uF or 1 uF.

Use Multilayer Ceramic capacitor. Choose a value of Capacitor like 100 nF and then work out resistor value.


Did you try 1.5k and 100 nF 16V ?
 
Last edited:
Top