PWM - Motor Control

A "new fashioned" televisor, using an Arduino to drive the motor and display.

Moderators: Dave Moll, Andrew Davie, Steve Anderson

Re: PWM - Motor Control

Postby Andrew Davie » Tue Mar 07, 2017 2:17 pm

A bit of reading on the 'net about the TIP31 transistor, and the following was very relevant

The datasheet for the TIP31 shows Ic(max) = 3 A, which is safely above the 1.25 A our motor will draw if it is stalled. So the TIP31 is a contender.

Next we have to verify that the transistor can safely handle the supply voltage we plan to use. The parameter we are looking for is the maximum collector emitter voltage, Vceo(max).

The TIP31 comes in 4 versions, with Vceo(max) ranging from 40 V to 100 V, all safely above the 12 V we plan to use. So the TIP31 is still a contender.

Now we need to calculate whether we can provide sufficient base current to keep the transistor in saturation. First we need to find what the base current will be when the transistor is carrying the worst-case current of 1.25 A. According to Figure 2, Ic/Ib = 10 or Ic = 10 * Ib. This means that for our collector current of 1.25 A, we would need to deliver a base current of .125 A, which is too much for our Arduino, which can deliver (safely) at most 40 mA 20 mA.

Finally let’s take a look at the datasheet for the TIP120. First, we see that Ic(max) = 5 A, and that Vceo(max) is 60, 80, or 100 V, so we are fine so far.

Next we check the base current. Again this is indicated in Figure 2, but this time Ic=250 * Ib or our collector current of 1.25 A requires a base current of 5 mA (5 * 250 = 1250), which is well below the maximum of 40 mA 20 mA the Arduino can put out.

Finally we need to select a base resistor which will be low enough to ensure that the TIP120 remains saturated, but high enough to prevent the Arduino from trying to deliver more current than it should. We want a current between 5 mA and 40 mA 20 mA, so let’s pick a midway point of 10 mA.

Back to Figure 2 where we see that when the collector current is 1 A, Vbe(sat) is about 1.5 V. Now if the Arduino is putting out 5 V, and Vbe is 1.5 V, that means that the resistor has a voltage drop of (5 – 1.5) or 3.5 V across it. Using Ohm’s law, R = V/I = 3.5/(10 mA)= 350 Ohms.


Source: How to use a transistor as a switch

This is all rather confused, and better to skip this and read Klass's explanations further down the thread. Note also that the maximum current on Arduino Micro pins is 20 mA, not the 40 mA used in these calculations.

Mmmh. Well those figures are very close to my motor so according to the above the TIP31 is not suitable. I can't saturate the TIP31 if the motor is drawing more than 400mA. I think I measured 350 mA max, but saw peaks around 800 mA, though now I'm not sure if that was when the motor was slowing and acting as a dynamo. But for the sake of argument, let's assume the motor takes 1 A when stalled.

This means that for our collector current of 1 A, we would need to deliver a base current of .1 A (100 mA), which is too much for our Arduino, which can deliver (safely) at most 40 mA (for the Arduino Micro). So that would rule out the TIP31 as a candidate. I'm pretty sure I have a TIP120 so probably time to switch over to that and do a bit of testing to see how things go. With the TIP120, same scenario, then we would need to deliver base current of (1000/250) = 4 mA. Easily < 40 mA of the Arduino. Further, let's go with the suggested 20 mA limit.

From the final paragraph in the quote, and looking at Fig. 2 in the TIP120 data sheet, Vbe(sat) = 1.5V so that's the voltage drop we use, and calculate ( 5V (arduino) - 1.5V (drop) ) / 20 mA = 3.5/0.02 = 175 ohms for the resistor. Note that the rather high voltage drop is due to the TIP120 being a darlington pair - effectively two transistors in the one package.

So, that's the next test - switch to a TIP120, change the resistor to ~175 ohms (or thereabouts) and also - I've bought some 3A Schottky diodes which unfortunately have thick pins but otherwise should really kill once and for all any backscatter from the motor. I've been advised to keep both the capacitor and the diode as close to the motor as possible to avoid EMI interference due to a long motor lead. So, I'll put them directly across the motor terminals.


If that all works OK, then I'll up the PWM frequency to 20KHz.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby Andrew Davie » Tue Mar 07, 2017 2:44 pm

I've learned heaps! I'm so glad I didn't just go with the online examples and say "yep, that's working!"
Thanks Klaas for sticking with me and guiding me in the right direction :)
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby Andrew Davie » Tue Mar 07, 2017 8:12 pm

First, I replaced the TIP31 with a TIP122. The picture shows the machine running with the disc very close to 750 rpm being driven by a 20kHz PWM. The "range" of duty is 0-99 inclusive and a value of 70/100 just about keeps it spinning at the correct speed. Current is sitting on close to 0.15 A. I saw a maximum of just over 0.3 A when starting up. Let's go with 0.4 A - and per the earlier calculations 400/250 = 1.6 mA required. Still, let's just halve the maximum of the Arduino pin and use 20 mA - again, (5V arduino-1.5V drop)/0.02 = 175 ohms. I used a 180 ohm resistor. Edit: but it turns out the Arduino Micro limit is 20 mA and the recommendation is 10 mA. So the resistor should have been 3.5 / 0.01 = 350 ohms. However, since the calculation was based on HALF of what I thought the maximum was, and then gave a bit of tolerance with a slightly higher resistor, I can leave this as-is. The maximum current will be 3.5/180 = 19.4 mA

I didn't see much wrong with the waveform - the only effect I saw was when the disc was spinning WAY overspeed, and slowing down, and then I saw some weird stuff. Particularly when I cut the duty to 0, and then I saw some strange patterns. I don't think it's the diode because the current reads 0 pretty much instantly when I set duty to 0. I tried measuring the current on both wires to the motor and got similar results. But I still saw the "badly affected" waveform at the collector of the transmitter when I cut duty to 0 and the disc was slowing down. That is; current = 0 measured on either motor wire, duty = 0, but disc spinning rapidly and strange signal on collector.

Not a big issue; I was just wondering what's causing it.

20khz.jpg
20khz.jpg (334.02 KiB) Viewed 10801 times


The spacing of the pulses is now 50 us. That's 20 kHz.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby Andrew Davie » Wed Mar 08, 2017 9:47 pm

Now it is all "tidy" and on a little breadboard - and it's ridiculously simple. But I know that it's correct - or close to, anyway. Happy that I went through that process, because I understand what's where, and why, and how it should work, and why, and how to fix it.



motorx2.jpg
motorx2.jpg (329.18 KiB) Viewed 10791 times


The diode and the capacitor are at the back of the motor, across the terminals. I have the red and black wires the wrong way around, and will fix that shortly just to be strictly correct. I need to move on. The next most important step is the IR sensors. Because once I have the IR sensors going I can start to play with frame lock and a single-hole on the disc. Pretty much the whole reason I started this was to see/show if that was possible.

You might have noticed i'm using new terminal/connectors. These just snap-fit for a connection, and there's simply no way to plug the connector upside-down. So once you've wired it up, you can remove and insert the connectors easy-as. A bit bulkier, sure, but for ease of use can't be beat.

SO, signing out from "PWM -Motor Control" for now and see you over at IR - Detecting Speed of the Disc!
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 4:54 pm

After the revisions/success of the LED board and realising I was doing the calculations all wrong, I revisited the motor board and removed the resistor from Arduino to the base of the TIP122, and replaced with a 7 ohm resistance from the emitter to ground. See this post for discussion on the correct calculations. I created the 7 ohm resistance from a parallel pair - 10 ohms and 24 ohms. Anyway, I not only get no motor any more, I also appear to have fried the arduino pin. Oh dear.

badmotor.jpg
badmotor.jpg (228.48 KiB) Viewed 10772 times


I wonder what I did wrong. Perhaps my soldering fried the transistor and when I plugged it in I got an excessive current to the Arduino??

Edit: There is no protective resistor on the 5V Arduino pin. If something goes wrong and there's a short back to the Arduino, then the excess current could fry the pin. See this new version with a protective resistor.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby gary » Mon Mar 13, 2017 5:46 pm

Andrew, where did this circuit come from? There should be a (say ~1k) resistor between the arduino pin and the base of the tip 122 to limit current out of the arduino in case of transistor failure/shorts etc.

There should also be a reversed diode across the terminals of the motor to suppress spikes that can damage the transistor.

Too late now by the looks of it, sorry :-(

I should have noticed that on the LED circuit but I just read the text and assumed the circuit was kocher.
gary
 

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 5:54 pm

gary wrote:Andrew, where did this circuit come from? There should be a (say ~1k) resistor between the arduino pin and the base of the tip 122 to limit current out of the arduino in case of transistor failure/shorts etc.

There should also be a reversed diode across the terminals of the motor to suppress spikes that can damage the transistor.

Too late now by the looks of it, sorry :-(

I should have noticed that on the LED circuit but I just read the text and assumed the circuit was kocher.



OK, me bad. There's a schottky diode across the motor terminals, and also a small capacitor.
However, no resistor between arduino and transistor. On the LED circuit Klaas advised it was not needed. I thought this would translate to the motor circuit too.
I guess not.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby gary » Mon Mar 13, 2017 6:06 pm

Generally speaking the resistor between the pin and ANY external device should be a minimum of Vpin/Imaxpin so that assuming your external device goes short circuit the worst that can happen is the current is the max the pin can handle - and less than that is obviously better.

IF that current x Beta is too low to run your device then you need another stage.
gary
 

Re: PWM - Motor Control

Postby gary » Mon Mar 13, 2017 6:26 pm

The thing here is that you are using PWM and so the transistor is only needed to act like a switch (not an amplifier) and so it should be turned hard on or off.

In fact that is the same for the LEDs except the LEDs must have a series resistor limiting the current. That limiting resistor can be in the collector or emitter leg.

Klaas may have been anticipating that the arduino pin had some sort of current overload protection.

In retrospect a ULN2003 darlington array may have been a better option (for both) as they can be connect directly to the arduino (internal protection). They only handle half an amp but you can always add another discrete stage for more current if necessary.
gary
 

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 6:32 pm

gary wrote:The thing here is that you are using PWM and so the transistor is only needed to act like a switch (not an amplifier) and so it should be turned hard on or off.

In fact that is the same for the LEDs except the LEDs must have a series resistor limiting the current. That limiting resistor can be in the collector or emitter leg.

Klaas may have been anticipating that the arduino pin had some sort of current overload protection.

In retrospect a ULN2003 darlington array may have been a better option (for both) as they can be connect directly to the arduino (internal protection). They only handle half an amp but you can always add another discrete stage for more current if necessary.


Fault, if any, is all mine. I'll but another Arduino tomorrow - meanwhile, it might be just that pin (#6) that is fried. I should be able to use another PWM pin - just a bit rattled at the moment because I had something working and I broke it. Stupid. I think I like the concept of always ensuring a pin is protected by an appropriate resistor. All part of learning. As a matter of policy I'll be using protective resistors where I can.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby gary » Mon Mar 13, 2017 6:58 pm

Andrew Davie wrote:
gary wrote:The thing here is that you are using PWM and so the transistor is only needed to act like a switch (not an amplifier) and so it should be turned hard on or off.

In fact that is the same for the LEDs except the LEDs must have a series resistor limiting the current. That limiting resistor can be in the collector or emitter leg.

Klaas may have been anticipating that the arduino pin had some sort of current overload protection.

In retrospect a ULN2003 darlington array may have been a better option (for both) as they can be connect directly to the arduino (internal protection). They only handle half an amp but you can always add another discrete stage for more current if necessary.


Fault, if any, is all mine. I'll but another Arduino tomorrow - meanwhile, it might be just that pin (#6) that is fried. I should be able to use another PWM pin - just a bit rattled at the moment because I had something working and I broke it. Stupid. I think I like the concept of always ensuring a pin is protected by an appropriate resistor. All part of learning. As a matter of policy I'll be using protective resistors where I can.


Oh Andrew letting loose the magic smoke is part and parcel of the game, we have all done it (more times than I care to remember). It's a great learning experience.
In this case, had everything gone to plan, it would have been fine, I think your calcs were correct given the assumptions - either the assumptions are wrong or there is a faulty device or wiring. The input resistance of a Darlington pair is high (compared to a single BJT) so something must have gone drastically wrong.

BTW are you sure the pin is kaput?
gary
 

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 7:20 pm

gary wrote:BTW are you sure the pin is kaput?


Jury is out. I put the oscilloscope on the pin, I see nothing. I could have changed the software somehow so I will re-evaluate. I did get the transistor rather hot when soldering - I could have fried it and that's the problem. In any case, I'll spend the next day or so learning how to get to a different PWM pin - truth be told I just set the registers and went hunting with the oscilloscope to find the pin which was changing according to the duty cycle I wrote. I need to learn how to select my own pinout anyway :P

I have just re-done the LED circuit diagram with my new understanding of protecting the arduino pin.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 7:50 pm

This is not a correct representation - left here for posterity. Please see this post for the latest and greatest.

protectivemotor.jpg
protectivemotor.jpg (125.78 KiB) Viewed 10758 times



Revised motor circuit/analysis with proposed protective resistor to limit Arduino current to 5 mA in case of unexpected events.
Edit: This works, but the 7 ohm resistor is pointless; it just slows down the motor's 'oomph' by restricting the maximum current. Better to just have the 1K resistor at the base to Arduino pin, and remove the 7 ohm resistor from emitter to ground.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: PWM - Motor Control

Postby gary » Mon Mar 13, 2017 8:13 pm

The only thing is I don't see the need for the 7 ohm resistor in the emitter leg. The transistor is just acting as a switch (that is it is in saturation), and, unlike the diodes, the motor doesn't need current limiting (unless your supply voltage exceeds the motors rating or it's current at that supply voltage exceeds the limit of your transistor).
gary
 

Re: PWM - Motor Control

Postby Andrew Davie » Mon Mar 13, 2017 8:22 pm

gary wrote:The only thing is I don't see the need for the 7 ohm resistor in the emitter leg. The transistor is just acting as a switch (that is it is in saturation), and, unlike the diodes, the motor doesn't need current limiting (unless your supply voltage exceeds the motors rating or it's current at that supply voltage exceeds the limit of your transistor).


I'm getting slightly conflicting advice from you and Klaas - and I know there are preferences in design. Klass advised the resistor from emitter to ground in the LED circuit, and no resistor between the base pin and the arduino. You are advising the latter but not the former in the motor circuit. I understand what you're saying that the motor doesn't need current limiting. Agreed. However the supply voltage (26V) probably dramatically exceeds the motor's (unknown) rating. I'm guessing it's a 6V or maybe 12V motor. I feel more comfortable with both resistors - the extra one is not doing harm, as I understand it, and provides some protection??
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

PreviousNext

Return to Andrew Davie's Arduino Televisor

Who is online

Users browsing this forum: No registered users and 2 guests