1

I am new to Raspberry pi. I want to make sine triangular PWM which will control the H-bridge of the inverter. I generate sine wave (50 Hz)and triangular wave (1500 Hz) using python language and comparing it. And the output of comparison is giving to GPIO pins 4,5,6,7. But when I see gpio pins output on Piscope, the output is not exactly which I want. Please help me to solve this problem.

import time
import RPi.GPIO as io

io.setmode(io.BCM)
io.setup(4,io.OUT)
io.setup(5,io.OUT)
io.setup(6,io.OUT)
io.setup(7,io.OUT)

from scipy import signal
import numpy as np

sample  = 0

f = 50
fsw = 1000
fc = 1500
sample = (fsw/f)-1
n = 0

while True:

    sine = 0.8 * np.sin(2 * np.pi * f * n * (1/fsw))
    triangle1 = 0.5 + 0.5 * signal.sawtooth(2*np.pi*fc*n*(1/fsw), 0.5)
    triangle2 = -0.5 + 0.5 * signal.sawtooth(2*np.pi*fc*n*(1/fsw), 0.5)


    x = 1 * np.greater(sine,triangle1)
    y = 1* np.greater(sine,triangle2)

    if x>0:
        x = 1
    else:
        x = 0
    if y>0:
        y = 0
    else:
        y = 1   

    io.output(4,x)
    io.output(5,x)
    io.output(6,y)
    io.output(7,y)

    #print(x,y)
    n = n+1

    if (n>=sample):
        n = 0

This I am getting on raspberry pi using above program.The frequency I am getting is 88.49 Hz instead of 50 Hz. enter image description here

Instead of this output which I should be getting enter image description here

enter image description here

Please help me rectify this problem.

5
  • Ah, I never heard of using triangular or sinusoidal PWM to play with a bridge. By definition, PWM means varying "width", never amplitude. Moreover, bridge input signal is digital, not analog. So I guess your design is either (1) Innovative R&D, or (2) Wrong timing calculation (H-Bridge is very slow, just to control electronical motors which has inertia, and cannot follow high freq signals) BTW you can easily find cheapy (< US$10) hardware trig/sin sign gen to testing, if your project goal in only on learning to control H-bridge.
    – tlfong01
    Dec 30, 2019 at 2:47
  • I just want to compare Sine wave and triangular wave and get the digital output. If sine magnitude is greater than triangular, GPIO pin should be HIGH. Dec 31, 2019 at 7:28
  • Ah. let me see. Rpi GPIO is a digital guy, in the sense that Rpi only tells you that it is below roughly 1V (Low) , or roughly above 3V (High). So it is sort of converting the triangular signal to PWM, with a smaller width than the PWM converted to sine.
    – tlfong01
    Dec 31, 2019 at 7:48
  • I am making myself clear here. With reference to the photos that I have uploaded, Lets say the sine wave is of 20 milisec and there are 30 cycles of triangular wave in that period. When we compare sine wave with triangular wave, the instance when sine magnitude is greater than triangular, GPIO pin should be HIGH, otherwise it should be LOW. When drawn on paper, it is clear that time width of GPIO ON OFF pulses are varying. Now am I clear saying this? Dec 31, 2019 at 14:36
  • Thank you for your clarification. I must confess that what you are saying is very new to me, so I need more time to look at the photos and follow your description, to make sure I understand what is going on, then I can comment something. Perhaps I can replay in a couple to days. Happy New Year!
    – tlfong01
    Dec 31, 2019 at 15:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.