# How to generate PWM with duty cycle varying in sinusoidal fashion?

I have to regulate a sinusoidal current in an inductor.

I have only a DC supply.

There are many ways to do it like using stepper motor driver to regulate step wise sinusoidal in 1/256th step mode.

For a specific application, I want to regulate the current by changing the duty cycle of the input PWM to a half-bridge.

If I can generate PWM pulses with a changing duty cycle in sinusoidal way then I'll be able to regulate the current.

I tried using a BeagleBone and an Arduino to generate PWM with a sinusoidal duty cycle, but wasn't able to do that. Mistakenly I had uploaded wrong drawing, now corrected.

I don't have waveform now to upload. But I can paste the beaglebone code I tried.

import Adafruit_BBIO.PWM as PWM
import time
import math

Ain1 = "P9_14"
duty=50 # initial value

sin_frequency=100
pwm_frequency=10000
pwm_period = 1.0/pwm_frequency
print("pwm period : ", pwm_period )

PWM.start(Ain1,0)
PWM.set_frequency(Ain1, pwm_frequency)
PWM.set_duty_cycle(Ain1, 0)

count=0
no_pulses=(0.5*pwm_frequency)/(sin_frequency)
print("number of pulses : ", no_pulses)
a=1

while True:
duty= abs(10.0*math.sin((3.14*count)/(no_pulses)))
PWM.set_duty_cycle(pin, duty)
time.sleep(0.1*pwm_period)
count=count+1
print(duty)

if count>=no_pulses:
count = 0
duty=0

PWM.stop(Ain1)
pwm.stop(Ain2)
PWM.cleanup()
print("Closed the pwm")

• Why weren't you able to that ? Can you post the existing circuit and code as well as any wave forms captured in scope ? Your question contains too few details of things that are already setup and things that have already been tried and their results. Did the beagle bone and arduino setups fail in the same way ? If they did, focus your question to any one of them.
– AJN
Apr 25, 2021 at 7:54
• Are you able generate a pre-determined steady current in the inductor with fixed PWM pulse width now ? Why are the inputs to the transistors labelled HIGH AND INPUT PWM ? Is the top transistor always ON ? Then you would be shorting the power supply when the lower transistor turns ON during the PWM pulse goes HIGH ?
– AJN
Apr 25, 2021 at 7:59
• High mosfet is PMOS so that is always off. When low side NMOS is ON current will build in coil, when OFF current will circulate through the body diode and decay gradually. Apr 25, 2021 at 15:52
• What's the point of the PMOS then? Apr 25, 2021 at 15:53
• You are right. It doesn't need a PMOS. Only a diode would be enough. I am just using body diode of the fet instead of using a separate diode. Because in design a diode is nothing but body diode of a fet. Since it is PMOS I can tie its gate to supply permanently. Apr 25, 2021 at 16:03