## Amplitude/PWM

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
meldiwin
Posts: 7
Joined: Sat Jul 28, 2018 10:46 pm

### Amplitude/PWM

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)

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

### Re: Amplitude/PWM

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.

pythoncoder
Posts: 3242
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

### Re: Amplitude/PWM

@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.
Peter Hinch

meldiwin
Posts: 7
Joined: Sat Jul 28, 2018 10:46 pm

### Re: Amplitude/PWM

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?.
Attachments
squaresignal.png (11.09 KiB) Viewed 572 times

meldiwin
Posts: 7
Joined: Sat Jul 28, 2018 10:46 pm

### Re: Amplitude/PWM

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?.

deshipu
Posts: 1342
Joined: Thu May 28, 2015 5:54 pm

### Re: Amplitude/PWM

You can do that with the DAC, but not with generic GPIO pins.

dhylands
Posts: 3067
Joined: Mon Jan 06, 2014 6:08 pm
Contact:

### Re: Amplitude/PWM

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.

chrismas9
Posts: 87
Joined: Wed Jun 25, 2014 10:07 am

### Re: Amplitude/PWM

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.