Page 1 of 1

Amplitude/PWM

Posted: Fri Aug 03, 2018 5:13 pm
by meldiwin
Hello,

I don't know how I can control/tune the value of the amplitude in the applied PWM as follows:

from pyb import Pin, Timer

p = Pin('X1') # X1 has TIM2, CH1
tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)

Re: Amplitude/PWM

Posted: Fri Aug 03, 2018 8:08 pm
by SpotlightKid
Digital pins are either on (+3.3V) or off (0V). That's what "digital" means. PWM just modulates the duty cycle of the square wave that results from switching a digital pin on and off at a certain interval. The amplitude of the square wave, i.e the difference between the on and off level stays the same always.

But you can easily change the voltage level using resistors.

Re: Amplitude/PWM

Posted: Sat Aug 04, 2018 5:53 am
by pythoncoder
@meldiwin The point of PWM is to create a signal whose average value is proportional to a supplied number. It assumes that the signal is applied to a hardware device which responds to that average. This could be a low pass filter, or something like a DC motor. So if you set the pulse width to 50% the average amplitude is 50% and the motor runs at half speed. Assuming that the motor would run at full speed if it were supplied with 3.3V.

If your motor required 12V to run at full speed then you'd need some electronics to boost the 0-3.3V levels to 0-12V, and probably to supply higher currents.

So you need to clarify what you mean by the "amplitude" of a PWM waveform. Its digital levels or its mean analog percentage of full scale.

Re: Amplitude/PWM

Posted: Sun Aug 05, 2018 6:52 pm
by meldiwin
Thanks for replies:

Actually I am supposed to send a voltage square signal ranging from 0.5 to 2.5 V ( voltage amplitude) with low/high frequencies. In that case I dont know whether I have to send 60.6% for having two volts, or I have to go to the other solution using resistors?.

Re: Amplitude/PWM

Posted: Sun Aug 05, 2018 6:57 pm
by meldiwin
For this square signal, it supposed to be dynamic values of amplitude and frequencies which submitted to my systems at different time, so I dont think that using resistors alone could help. I am expecting that I can directly send 1V, 1.5,2V maximum 3V from the pyboard to my system, can I do that?.

Re: Amplitude/PWM

Posted: Sun Aug 05, 2018 9:48 pm
by deshipu
You can do that with the DAC, but not with generic GPIO pins.

Re: Amplitude/PWM

Posted: Mon Aug 06, 2018 1:56 am
by dhylands
You can also use am RC circuit like this article shows https://www.instructables.com/id/Arduin ... analog-DC/
Just don't expect to sink any serious current.

Re: Amplitude/PWM

Posted: Mon Aug 06, 2018 1:33 pm
by chrismas9
You can modulate the DAC output with the PWM.
1. Connect a resistor, say 10k to the DAC pin. The other end will be your output.
2. Connect a MOSFET, say 2N700p - S to GND, G to PWM and D to output end of resistor.

When the PWM is high the MOSFET will be on and drive output to 0V. When PWM is low the output will be equal to the DAC output (between 0 and 3.3V) as long as you have a high impedance load.

Use (100-D.C.)% due to the inversion by the MOSFET.