Shop OBEX P1 Docs P2 Docs Learn Events
Help regarding PWM for H-BRIDGES .... — Parallax Forums

Help regarding PWM for H-BRIDGES ....

RITESH KAKKARRITESH KAKKAR Posts: 254
edited 2012-01-17 09:10 in Propeller 1
Hello,
I have build H-BRIDGES for Brushed DC motor now i want to control its speed using PWM.So, I need help on building code and suggestion my circuit is working fine with 24V supply.
.
12012012262.jpg
1024 x 768 - 91K
«1

Comments

  • T ChapT Chap Posts: 4,223
    edited 2012-01-14 06:43
    First thing to determine, what is the voltage range required for the speed control input to the Hbridge? Then determine if the voltage swing from the Propeller is sufficient(0 - 3v3). If not, some buffer will be required to turn the Hbridge on and off. After you get the control set up, then determine what method the PWM will be controlled by. Will it be software only? Or will the software have some external control device(Potentiometer etc). If you can provide more info that would help with specific responses. In terms of PWM, the Prop can manage that well.


    It would be a good idea to study this app note for Propeller Counters to understand how the Propeller generates PWM.

    http://www.parallaxsemiconductor.com/an001

    There are PWM objects in the Object Exchange that makes generating PWM very easy. Search the Obj Exchange for "PWM", "motor control". There are several objects there to control speed for DC motors.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-14 07:29
    Hi,
    Then determine if the voltage swing from the Propeller is sufficient(0 - 3v3). If not, some buffer will be required to turn the Hbridge on and off.
    Yes it will work no need of buffer. you see the ckt of buffer using comparator at 1.4V.
    Or will the software have some external control device(Potentiometer etc). If you can provide more info that would help with specific responses. In terms of PWM, the Prop can manage that well.

    Yes using pot meters..
    .
    RiteshMotor.jpg
    800 x 448 - 39K
  • JonnyMacJonnyMac Posts: 9,198
    edited 2012-01-14 07:37
    Have you considered the using a TC4427 to drive your power MOSFETs? It will allow the level translation you need for gate drive and simplify your circuit. I've used it to drive a power MOSFET in a high-current LED lighting circuit.
  • T ChapT Chap Posts: 4,223
    edited 2012-01-14 07:39
    I would first get some practice in controlling the motor with software, just set the duty and period, see how it runs. You will find that if the period is too high, the mosfets may not switch fast enough. If the period is too low, the motor may not run smoothly. Find a period that allows the motor to run smoothly at all duty values (0-100).

    There may be objects better suited to DC motors, I do not use DC motors but someone else may have a suggestion. The object below is very easy to use, and has an example. You start the object and give it the pin you want to use, specify the period and duty cycle. One you get this part working, then you can tackle how to read the POT into the Prop, but it is not a good idea to tackle 2 things at once.

    http://obex.parallax.com/objects/216/
  • T ChapT Chap Posts: 4,223
    edited 2012-01-14 07:56
    I just noticed the drawing you added to your post. I would very concerned about the gates not switching at the exact time(especially on a breadboard with the potential for accidental connection issue), or having some overlap which will short 24V to GND. A mosfet driver would be much better. Second choice would be to have individual control of the mosfets so that the code can avoid having the PNP and NPN ever be on at the same time(even if very short overlap, a deadband in the software would be preferred)
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-14 08:08
    See my circuit working so don't like to do any change in it.
    please tell what will be method to control i think it will use ADC..?
  • T ChapT Chap Posts: 4,223
    edited 2012-01-14 08:17
    ADC is correct. The prop has a convenient method to do ADC that would work to get you started. Look in your Propeller Tool folder for ADC.spin. It shows the schematic. You can set the wiper to output the voltage to read by the prop, try to use 3v3 and GND on the pot to avoid over voltage to the pin. You will convert the ADC value to the duty factor. For example, if reading 0V, output 0 duty on the PWM. If reading 3V3, then output 100% duty. You will scale the PWM to the ADC value. If you are wanting forward and reverse on the pot, you will need to set the pot center ADC value and 0 duty on both. Above the center ADC value, turn on the PWM for forward, below the center turn on the reverse PWM output. Scale the center values to the duty for both forward and reverse. You will likely need to define a dead zone in the center to avoid oscillation and creeping.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-14 20:36
    Hi,

    Will running motor at low speed by switching freq give lots of spark how to remove it?

    Here is code no schematic...

    {{
    *****************************************
    * generic ADC driver v1.0               *
    * Author: Beau Schwabe                  *
    * Copyright (c) 2007 Parallax           *
    * See end of file for terms of use.     *
    *****************************************
    }}
    CON
        SDF = 6                     'sigma-delta feedback
        SDI = 7                     'sigma-delta input
    
    PUB SigmaDelta (sample)
        cognew(@asm_entry, sample)   'launch assembly program in a COG
    
    DAT
                  org
    
    asm_entry     mov       dira,#1<<SDF                    'make SDF pin an output
    
                  movs      ctra,#SDI                       'POS W/FEEDBACK mode for CTRA
                  movd      ctra,#SDF
                  movi      ctra,#%01001_000
                  mov       frqa,#1
    
                  mov       asm_c,cnt                       'prepare for WAITCNT loop
                  add       asm_c,asm_cycles
    
                  
    :loop         waitcnt   asm_c,asm_cycles                'wait for next CNT value
                                                            '(timing is determinant after WAITCNT)
    
                  mov       asm_new,phsa                    'capture PHSA
    
                  mov       asm_sample,asm_new              'compute sample from 'new' - 'old'
                  sub       asm_sample,asm_old
                  mov       asm_old,asm_new
                  
                  wrlong    asm_sample,par                  'write sample back to Spin variable "sample" 
                                                            '(WRLONG introduces timing indeterminancy here..)
                                                            '(..since it must sync to the HUB)
                                                            
                  jmp       #:loop                          'wait for next sample
    
                  
    
    asm_cycles    long      $FFFF                           '(use $FFFF for 16-bit, $FFF for 12-bit, or $FF for 8-bit)
    
    asm_c         res       1                               'uninitialized variables follow emitted data
    asm_cnt       res       1
    asm_new       res       1
    asm_old       res       1
    asm_sample    res       1
    asm_temp      res       1
    
    DAT
    {{
    +------------------------------------------------------------------------------------------------------------------------------+
    ¦                                                   TERMS OF USE: MIT License                                                  ¦
    +------------------------------------------------------------------------------------------------------------------------------¦
    ¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    ¦
    ¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    ¦
    ¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
    ¦is furnished to do so, subject to the following conditions:                                                                   ¦
    ¦                                                                                                                              ¦
    ¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
    ¦                                                                                                                              ¦
    ¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          ¦
    ¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         ¦
    ¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   ¦
    ¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         ¦
    +------------------------------------------------------------------------------------------------------------------------------+
    }}
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-15 07:43
    Hi Ritesh,

    I'm very ignorant about MOSFETs but I've been reading this thread.

    I'm not sure how the code you posted relates to your question about sparks. The code you posted could be used to read a pot. There is an apnote about using the code here. The pdf has this schematic.

    attachment.php?attachmentid=88584&d=1326641533

    There is a place on the QuickStart board to add these parts. There are the two Greek letters Delta and Sigma above the touchpad P5. This is where you'd add the needed resistors and capacitors.

    I've personally never added a delta-sigma curcuit to any of my boards. I usually just use an ADC chip.

    Now about the sparks. I don't think I understood your question. When you run the motor at slow speed you have a problem with sparks? What code are you using to control the motor? What parameters are you using?

    I hope you're having a good break from school (if you're still on break).
    799 x 307 - 25K
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 08:06
    Not sure either about the question. But, many DC motors have visible sparks. PWM is not likely to be able to remove sparks, as the sparks are a function of electrical arcing from the brushes. Although, it may be common sense that if there is less voltage( or less constant voltage ) to the motor, that the sparking could be less by default.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 08:13
    Hi again,

    I think first i should control motor by software then come to hardware (ADC with pot meter)..
    I was testing motor using Repeat and wait ( square wave) just testing giving lot of vibration and speed was not control..!!

    So, i want some basic help to start PWM in Quick start Board.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 08:14
    ot sure either about the question. But, many DC motors have visible sparks. PWM is not likely to be able to remove sparks, as the sparks are a function of electrical arcing from the brushes. Although, it may be common sense that if there is less voltage( or less constant voltage ) to the motor, that the sparking could be less by default.


    I think it require snubber ckt i don't know how to build it..
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 08:26
    It would help if you posted your code so we can see an error. Have you tested your hbridge by 3.3V to see if it runs? Have you tested also by simply turning on the PIN and leaving it high to see if the hbridge works?

    dira[pin] := 1
    outa[pin] :=1 ' see if this turns on hbridge, motor full speed.


    Try this:
    ''Demonstration of PWM version of NCO/PWM counter mode
    CON _clkmode = xtal1 + pll16x
        _xinfreq = 5_000_000  
        pwm1pin  = 12   'set your pin here
    
    VAR
     
      
    OBJ
      pwm1  :  "pwmasm"
    
    PUB go | x
      pwm1.start(pwm1pin)
      pwm1.setperiod(1000)    '  adjust this to find a smooth running value.  motors vary, speeds vary, loads vary. this is freq of the cycle
      pwm1.SetDuty(100)    ' adjust from 0 - 100,  full off to full on
      repeat    'keep alive
    
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 08:33
    Hi ,
    It would help if you posted your code so we can see an error. Have you tested your hbridge by 3.3V to see if it runs? Have you tested also by simply turning on the PIN and leaving it high to see if the hbridge works?

    dira[pin] := 1
    outa[pin] :=1 ' see if this turns on hbridge, motor full speed.

    Yes it is working fine..!!

    the code you have given is not working..
    It is asking for PWMASM..!!
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 08:44
    download pwmasm from the object exchange. search for it, download it and put it in your propeller folder, then the example above can find it. I already posted a link to it earlier in the thread.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 08:52
    I have paste that program in C/program file/parallax/library ...
    but then also showing error..!!

    122.jpg
    1024 x 576 - 54K
    122.jpg 54.3K
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 08:56
    When you download the obj, it is in .zip format. Find the file you downloaded, right click and choose EXTRACT ALL. This should make the file pwmasm.spin appear in your folder.

    It should be located in the folder that contains all your other spin files.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 08:59
    Yes i have extracted the file and open the folder then copy that two program and paste in lib. then close the window then open again.
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 09:03
    Is it in the same folder as the Propeller Tool? I do not have a folder called Library. Make sure the Prop tool is looking in the correct folder, where all the other spin files are. My Prop Tool loads it fine.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 09:07
    It is working by saving through software only.
    OK, when i change the freq and duty cycle at low speed the torque become small and the motor fail to restart..!!
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 09:12
    The code is working....

    When i change the of duty cycle the motor torque reduce and on increasing the freq the motor start running not in low freq..??
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 09:22
    Experiment with the period at 50, 100, 1000, 2500, 5000, 10000, 15000, 20000, 25000.

    Just because the program can go to 0 duty, that does not mean the motor will function properly at all duty cycles. Also, It is not clear that your hbridge is sufficient to operate at various frequencies/duty cycles. The circuit you have may not be switching nicely with PWM, since on each on/off pulse, you may have a point where both 24v and GND are on at the same time. A driver IC will prevent this. I think you are only going to have so much success with that circuit. It may work great when wired direct to a control voltage, but switching it is an entirely different matter. If it were me, I would test for now with ONLY the high side switching, connect the other side of the motor direct to GND. Then you can determine how the software is really behaving without concern for how nicely your circuit is behaving.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 09:25
    I would test for now with ONLY the high side switching, connect the other side of the motor direct to GND. Then you can determine how the software is really behaving without concern for how nicely your circuit is behaving.

    I am also doing the same..and the motor working fine..!!
    But the code seems to be very complex.
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 09:33
    Can you post the current schematic if it is different that in the earlier post? I do not think you should be driving the mosfets with a comparator using PWM.

    Not sure what you mean by complex, that is about as simple as it gets for PWM control.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 09:36
    The schematic of hardware is same.
    I am talking of that all code pwmasm ones...seems to be very hard for me.

    OK, now if i want to go with pot meter then how to control speed.?
  • T ChapT Chap Posts: 4,223
    edited 2012-01-15 09:59
    If the schematic you are using is the same as the one posted, there are problems with that schematic when using PWM. The issue is that you are switching one side of the motor from 24v to GND rapidly. Now, when you apply GND to both sides of the DC motor, you may be actually BREAKING the motor electronically. So, in your switching scheme with PWM, you are:

    apply 24v
    break
    apply 24v
    break

    This is not a good method to run the motor, as it will be fighting itself. It is best to disable the NPN mosfet so it is not taking the motor to GND. Just switch the PNP only.

    There is another thread already discussing how to use a pot. With a pot, you would put 3.3V on one side of the pot, GND on the other, then the wiper goes to the Prop based on the schematic shown in the ADC object. You read the ADC value, then create a ratio to PWM. I think you need to spend time first learning how to read a value fro, the Prop ADC, display the value somewhere(LCD,VGA, terminal etc). Once you can change the pot and see the result, you can learn to convert the value to PWM duty.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 20:27
    If the schematic you are using is the same as the one posted, there are problems with that schematic when using PWM. The issue is that you are switching one side of the motor from 24v to GND rapidly. Now, when you apply GND to both sides of the DC motor, you may be actually BREAKING the motor electronically. So, in your switching scheme with PWM, you are:

    apply 24v
    break
    apply 24v
    break

    This is not a good method to run the motor, as it will be fighting itself. It is best to disable the NPN mosfet so it is not taking the motor to GND. Just switch the PNP only.

    Hi again,

    OK you are thinking that i am using H-BRIDGES one side only as indicated in schematic its not true i am using both side 4 MOSFET a complete H-BRIDGES...The schematic is just for understanding the concept..!!
    OK, As QS also have a ADC converter but iu have to put C and R on it as suggested by Duage..I think i will like t to use ADC chip.!!
    please suggest which one to use?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-15 20:50
    I think i will like t to use ADC chip.!!
    please suggest which one to use?

    Ritesh,

    I suggested some ADC chips in post #3 of this thread.

    As I said before, I don't understand MOSFETs enough to help but it looks like T Chap knows about them. It's still unclear to me what schematic you are using. It might help T Chap if you post the exact schematic you are using (so he can better help you).

    Good luck.
  • RITESH KAKKARRITESH KAKKAR Posts: 254
    edited 2012-01-15 22:17
    As I said before, I don't understand MOSFETs enough to help but it looks like T Chap knows about them. It's still unclear to me what schematic you are using. It might help T Chap if you post the exact schematic you are using (so he can better help you).

    Hi again,
    My circuit is working fine..!!( using IRF540/9540 and LM339)
    I have tested it up to 6-7amps at 24V. OK, i will pot schematic soon.
    I t will be better to use serial or // ADC chip?? I don't want to use that sigma and delta. i will assemble extra PCB for ADC from pot meter.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-16 06:26
    Hi Ritesh,

    I think a serial ADC would be a good choice here.

    I've only used a parallel ADC when I needed lots of samples, really fast, for video capture on the Prop. (Surprisingly, the delta-sigma curcuit can also capture video.)

    As I mentioned in the thread I linked to earlier, my favorite ADC is the MCP3208 since it has 8-channels and doesn't cost much more the chips that only have two or four channels.

    One chip I didn't mention in the other thread is the MCP3002. I like this chip because it's so cheap. I usually buy 10 at a time so they cost even less ($2.07). The MCP3002 only has 10-bit resolution (0 to 1023) compared to the MCP3208's 12-bit (0 to 4095), but for most applications, 10-bits is plenty of resolution. I know there are already objects for these chips so they should not be very hard to add to your project.
Sign In or Register to comment.