# PwM using microcontroller

Discussion in 'Microcontrollers, Programming and IoT' started by pica, Aug 14, 2017.

1. ### pica

36
0
Jul 7, 2017
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.

2. ### (*steve*)¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥdModerator

25,501
2,841
Jan 21, 2010
On all microcontrollers I'm aware of, an analog output is PWM. To make it an actul analog voltage you need to integrate the output over time (essentially employ an RC time constant much greater than the period of the PWM)

3. ### pica

36
0
Jul 7, 2017
Hello Steve,

What I wanted to do is,

if the i/p is PWM with 1000Hz and 30% duty then I wanted to generate o/p with 100HZ with 30% duty.
if the i/p is PWM with 10Hz and 80% duty then I wanted to generate o/p with 100HZ with 80% duty.
if the i/p is PWM with 500Hz and 56% duty then I wanted to generate o/p with 100HZ with 56% duty.

4. ### (*steve*)¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥdModerator

25,501
2,841
Jan 21, 2010
Convert the pwm input to a voltage, read that voltage and use it to control the output pwm.

How fast you want the circuit t respond.

5. ### kellys_eye

4,300
1,158
Jun 25, 2010
You can't do the measurement and output on the same pin unless you have some form of 'switching' to differential between the two.

6. ### pica

36
0
Jul 7, 2017
Yes Steve,

I had did that using RC low pass filter but I am getting fluctuations.

Also by using RC low pass filter. The o/p is coming only 4.5V not 5V

7. ### pica

36
0
Jul 7, 2017
Yes Kellys,

For analog voltage 0-5V I will use internal ADC.

But what for i/p PWM? How I can measure the duty cycle.

8. ### kellys_eye

4,300
1,158
Jun 25, 2010
The output voltage will depend on the duty cycle and the RC time constant. You would only ever get the full 5V at 100% duty cycle (i.e. permanently 'on').

Use another timer - triggered by the rising/falling edge of the i/p PWM signal.

9. ### pica

36
0
Jul 7, 2017
Hello Kellys,

I was thinking in the same way but i am using ATtiny 10 controller of ATMEL.

I read the datasheet, It has one 16 bit timer. I have to capture i/p PWM also i have to generate o/p PWM.

Dose my operation is possible.

10. ### BobK

7,682
1,688
Jan 5, 2010
Depends on the frequencies involved and the accuracy required.

If slow enough, you don't need any hardware peripherals.

Bob

11. ### pica

36
0
Jul 7, 2017
Hello Kellys and BobK,

Then how to do it?

I have only one 16 bit timer.