Thursday, March 1, 2012

Class B Push-Pull Amplifier Design for DC Motor Analog Drive

I am currently involved in a project that requires analog speed control of a DC motor. One very popular method to achieve control of DC motors requires the use of a PWM signal along with an Hbridge circuit. Modular circuit website  provides a great resource for the Hbridge circuit and explains thoroughly different PWM driving techniques like Sign-Magnitude and Lock Anti-Phase Drive methods. However, and after doing a basic online search, I barely found any good resources on analog DC motor drives. There are many reasons for this mainly because switching drive methods are generally much more efficient than their analog counterparts and PWM signals are easily generated using microcontrollers as compared to the use of DAC for analog control. This does not mean that analog drive is obsolete as it is still the preferred method in some applications such as motor frequency response, continuous time control, and applications that are sensitive to the electromagnetic interference (EMI) that can be generated by a PWM switching circuit.

A famous method for transforming an analog signal into switching PWM signal is done using a class-D amplifier (switching amplifier). However such an amplifier design is complicated and requires lots of components. On the other hand, there are some commercially available power amplifiers such as the LM675, L165. Such ICs are a bit expensive and not widely available for the hobbyist to use.
In this post I will be reviewing DIY analog circuit designs to drive DC motors using BJT power transistors. This requires good knowledge of BJT transistors. If the terms base, collector, emitter, npn, pnp, cut-off, active and saturation regions do not mean anything to you, then maybe you should consider doing a BJT transistor research before continuing to read here.

I will be using a TIP31 npn transistor which has the following characteristics:
- Current gain: hfe=β= 50
- Collector-Emitter saturation voltage : Vce(sat)=1.2V
- Cut-off voltage Vbe(on) = 1.8V [according to the context-this will be called Vbe from now on]
I will be using also TIP32 which is the complimentary pnp transistor of the TIP31.

Common Emitter Configuration 

In this configuration we can achieve current control for the motor. Since for a DC motor the torque is proportional to current (T=K*ic where K is a constant that depends on the motor's winding configuration), torque control can be achieve using this circuit. While in active mode, the equation relating ic to Vin can be found (note Vbe = Vbe(on))


Let us understand this model intuitively:
  • For Vin<Vbe  transistor is in cut-off region and motor is off.
  • For Vin>Vbe and Vce>Vce(sat) transistor is in active region, motor torque T is proportional to Vin.
  • As we increase Vin, ib increases which means ic increases and Vce decreases. 
  • For Vin>>Vbe and Vce=Vce(sat):saturation region, motor reaches its maximum torque/speed capacity.
If we follow the DC motor's mechanical model developed in the following Carnegie Melon tutorial and assuming the only torque applied on the motor shaft is that of shaft inertia (Jm) and damping (B) we can compute the transfer function of the speed (w) versus ΔV=Vin-Vbe

According to the transfer function the steady state relationship between speed and voltage in the case of no load condition is
According to this result, decreasing Rb would cause the speed to go higher. Of course this method has its pros and cons:
  • pro: Assuming β is constant we can achieve linear current (torque) control of DC motor
  • con: The model is highly dependent on the current gain β (hfe) which varies alot specially for high power BJT (in the case of TIP31 it varies between 10 and 100). Hence the model is non linear!
Common Collector Configuration (also called Emitter-Follower)

In order to cancel the effect of β on the system we connect the motor in a common collector configuration. In this configuration, the motor is connected to the emitter of the BJT as shown in the following figure. This configuration is called emitter follower because the voltage on the emitter will always be following that of the input with a voltage drop of Vbe. So the gain of this configuration is a bit less than 1.


Again, and to evaluate the circuit, we need to find the transfer function ω/ΔV where ΔV=Vin-Vbe.
In this configuration when the transistor is turned on, Ve=Vb-Vbe. Knowing that ΔV=Vin-Vbe, the relation between the motor current, motor voltage and Vin can be found:

According to the mechanical model of the motor we know that the motor torque is equal to


Since T=K*ie and performing the Laplace transform we obtain


Equating the two equations we get the following relation:


The electrical model of the motor gives

Where R and L are the internal resistance and inductance of the motor coils. We are assuming in here that the motor is ideal and the generated EMF constant (K) is equal to that of the torque/current proportionality constant. Combining the electrical model with the previous equations of Ve and ie we obtain the following transfer function

From this transfer function we notice that if we choose Rb = 0 the effect of β on the motor is canceled. The transfer function then becomes


This is exactly the transfer function of a DC motor. However we haven't considered yet the rotation of the motor in both directions (Clockwise CW and counterclockwise CCW)

Class B Push-Pull Amplifier
In order to be able to rotate the motor in both clockwise and counterclockwise directions we should be capable of providing a negative current to the motor. This can be achieved using a push-pull amplifier. A push pull amplifier is a Class B type of amplifier that either drives a positive or a negative current into a load. It consist of a pair of complimentary transistors, in our case NPN and PNP BJT transistors, connected in common collector configuration.


When +Vcc>Vin>Vbe the upper transistor (NPN) is in active region and the lower transistor (PNP) would be in cutoff region. When -Vcc<Vin<-Vbe the lower transisor (PNP) will be in active region and the upper one will be in cutoff. The same theory developed in the previous section can be applied in here.
In order to be able to drive the transistors from a high output impedance source (potentiometer - DAC etc.) we need an impedance matching device. This can be achieved using a UA741 op-amp. The feedback for the op-amp needs to be implemented depending on the gain needed. In our case unity gain is required.

In order to test the class B push-pull amplifier I devised a setup consisting of a DC motor of maximum speed 30RPM connected to a quadrature encoder to measure speed. The data acquisition is accomplished using a dspic30F4012 microcontroller. Running at 120Mhz and 30MIPS this dspic computes the instantaneous velocity and communicates serially with a Matlab code in order to display in real time this velocity. I connected a potentiometer to the input of the amplifier. I would set the pot at a certain voltage and then wait for steady state condition to measure the velocity of the motor.

The amplifier/DC motor/encoder/acquisition setup used for the experimentation
The following plot shows the RPM vs voltage (Vin) curve for the Class B push pull amplifier design shown above.



We notice from this plot that there is a dead zone around 0V. This dead zone is called "crossover distortion" in the literature. This is obviously due to base-emitter voltage necessary for the NPN transistor to be in the active region (in the case of positive Vin) and for the PNP transistor to be in the active region (in the case of negative Vin). In the case of TIP31 and TIP32 this crossover distortion is of about 2Vbe=2*1.8=3.6volts. This  is not desirable at all since it provides non-linearity that will cause the frequency response analysis or the closed loop control to fail.

In the following allaboutcircuits.com article a solution for this crossover distortion was suggested which consists of connecting the op-amp with the negative feedback in the following manner


Without any loss of generality and in order to understand better this circuit we are going to analyze the NPN half of it. A is the gain of the amplifier.

Assuming Vin=ε is the voltage  responsible for switching the transistor from the cut-off region to the active region. Considering the case when the transistor is about to switch to the active region, then Vbe=1.8V while Ve is still 0V, hence according to the previous formula

Since the opamp has a very large gain A (about 200000 for ua741), this shows that ε is infinitesimal and very close to zero.
The following plot shows the RPM vs voltage (Vin) curve for the improved Class B push pull amplifier design with opamp feedback as compared to  the previous case


We notice from the plot that the crossover distortion has been reduced from a value of 3.6 volts to a value of 0.5v with the improved feedback design. We know from the theory that the new crossover distortion is supposed to be infinitesimally small, however in practice, this turned out not to be true. The reason for this is unknown to me but most probably it has to do with the imperfections in the opamp. If you have an idea about the reason please don't hesitate to comment.

I do realize that this design might need improvements and maybe the implementation of a class AB amplifier would have provide a better response, however for my current analog control of a DC motor application the Class B push-pull amplifier turned out to be very useful as it provided an almost linear relation between RPM and voltage. If you have any suggestions or improvements please comment on this post.

References and suggested readings:

Monday, January 16, 2012

DIY smartphone headphone adapter (with MIC & control button)



  • Do you happen to own a very expensive and personal set of headphones?
  • Do you have a special connection with your personal headphones such that you cannot listen to music without them?
  • Do you wish your personal headphones have the feature of the remote control (buttons) and MIC that smartphone earphones usually have? 
  • Do you have an AUX line in your car's radio and you like to listen to your smartphone's music in your car?
  • Are you bothered when you receive a phone call while driving because you'll have to unhook your phone from the AUX input to answer, which can get you into an accident?
  • Are you like me and you prefer not paying tens of dollars for something that you can build yourself and will only cost you time (and a few dollars). There is nothing more rewarding then DIY!  
Ja3far had the same problems mentioned above and in the video below he will show you the solution he came up with. (This is my first time shooting a DIY video, and I think I am a bad illustrator, so excuse my sloppy technique)



In the video, Ja3far decided to make an adapter which he can hook up to his expensive headphones (or any other earphone/headphone). This connector has a microphone and a button. Ja3far can use the button to play/pause/move to the next track/answer phone calls (the features of the button depends really on the operating system of your smartphone). And the MIC is obviously used to communicate during a phone call without having to bother (very useful while driving).

This project requires little knowledge of electronics and soldering. You just need the following items:
1- Solder and Soldering Iron
2- Wires*
3- Electret MIC*
4- Push Button*
5- TRRS (Tip-Ring-Ring-Sleeve) audio Jack*
6- Multimeter (not necessary, but it is good to be able to perform continuity testing to follow the connections of the Jack)

*total cost is about 2$
All these items can be found at your local electronics shop.
The TRRS jack goes into the smartphone audio input, while it is important to note that regular earphones and headphones have a TRS type of jack as shown below:
A TRRS audio jack

Headphones TRS jack
For a headphone jack: TIP is connected to Left channel, RING is connected to the Right channel, and Sleeve is connected to the Ground.
Oh and this is a final image of the adapter.

I know it doesn't look great but hey it does the job, and trust me wearing your headphones with this geeky looking gadget will definitely make you stand out.
So I leave the aesthetics for the aesthetic minded.

Tuesday, August 23, 2011

Chaos Theory: Simulating a double pendulum to understand how I owe my existence to Gaddafi's apple

"Does God Play Dice? The New Mathematics of Chaos" is a beautiful book by Ian Stewart. It provided me with a valuable introduction into the concept of chaos and chaos theory. Motivated by the book I decided to experiment with Chaos and write a dynamics simulator on MATLAB to simulate a 2 degrees of freedom pendulum (double pendulum).

In chaos theory 'Chaos' does not necessarily mean random (or a state of disorder). "Chaos occurs when a deterministic system behaves in an apparently random manner"Chaos is actually all around us. Chaos is the rule rather than the exception.

It is only when you think that everything is under 'control' that you wake up one morning to see that yesterday's weather forecast was all wrong and there is a huge storm outside, you check the stock market only to find out that you have lost thousands of dollars. HELL! you turn on the news only to realize that  suddenly the Libyan people rose against a crazy tyrant who ruled them for 42 years. Chaos.

An important characteristic of Chaotic systems is their high sensitivity to initial conditions. Even for a deterministic system (with no random elements), this high sensitivity to initial conditions is what causes the impossibility of long term prediction of the system's behavior. A slight change in the initial conditions would yield completely different and diverging end results.

To experiment with Chaos, I wrote a dynamic simulation of a double pendulum (2DOF pendulum) on MATLAB. The double pendulum is a classical example of Chaos theory.   I used the "Simple Forward Kinematics library for Robotic Chains" that I developed back in 2010 (click here to download), and I developed a new library to compute the joint-space dynamic equations and to perform the simulation for any kinematic chain (click here to download). So back to our chaos theory. To experiment, I ran two simulations:
- The first (on the left in the video below) is a simulation with a base joint initial position of 130 degrees
- The second (on the right in the video below) is a simulation with a base joint initial position of 129 degrees



Notice that although the initial conditions are almost the same, a difference in the trajectory begins to be noticeable only after 7 seconds. This difference increases as time passes. And after 40 seconds we notice that each pendulum is located on a totally different position in space.
We've all heard of the vivid and beautiful term "the butterfly effect". Here it is. The impossibility of predicting the long term behavior of the pendulum.

In 1972, Muammar Al-Gaddafi, while having diner in his tent and surrounded by his beautiful amazonian guards, decided to eat an apple rather than a banana. Imagine he ate the banana instead and it turned out the banana was contaminated with a deadly bacteria. He would have died after a month of suffering. Libya would have been 'inherited' by a crazier person (as is always the case). This person would have changed the entire political scene in the region, maybe causing the Lebanese civil war to be more intense. My father would have decided to flee the war and immigrate to Canada and he wouldn't have met my mother.  I wouldn't have existed. I owe my existence to Gaddafi's apple.

MATLAB Files:
- Simple Forward Kinematics library for Robotic Chains
- Dynamics Simulator for Kinematic Chains

Thursday, August 4, 2011

Sound Triggered Flash: High speed photography




capturing scene invisible to the naked eyes: water balloon popping



I was always amazed with Discovery Channel's Time wrap video clips. So I decided to do some experimenting on my own. A high frame rate video camera costs thousands of dollars... that was out of the question. A compromise needed to be made... instead of shooting videos, let's go for high speed photography. Sound Triggered Flash.  As the name indicates, it is a flash triggered by a sound impulse. Using my SLR camera with the shutter open in a dark room, I would be able to capture photos of sound generating events.

Simple but inspiring. Enough to get me started on the project.

After doing some online search, I was able to find some links for DIY sound triggered flash. They were pretty helpful to get me started, however I decided to drop the research and just go with my own circuit design (more fun).



1. Component list:

- Disposable camera  with built-in flash x1
- Electret Microphone x1
- 9V battery x1
- LM386- Low voltage audio power amplifier x1
- LM339- Comparator x1
- NE555- 555 timer x1
- TIP120- BJT darlington transistor x1
- 10Kohm potentiometer x2
- Resistances: 10K x7 - 5K x 1 - 510 x 2
- Capacitors:   10nF x3 - 10uF x2 -  220 uF x1
- LED x2
- On/Off dip switch x 2




2.   Hacking the disposable camera:


You must have realized by now that we only need the flash circuit from the disposable camera (of course any professional flash would do the job, but for $10 the disposable camera flash is good enough). After cracking open the Kodak disposable camera that I got, and analyzing the amazing ingenuity behind the trigger/shutter/flash mechanisms, i focused on the flash circuit.


Now notice how I carefully hold the flash circuit PCB. Notice how there is a 185uF - 330V capacitor in the circuit. I was actually electrocuted by the 330V capacitor -twice. Not fun. Not deadly, but not fun. (What doesn't kill you makes you stronger doesn't really apply in this case..)
So I have to say this..
Courtesy of "How camera flash work" article
HAZARD: DON'T TOUCH THE CAPACITOR LEADS WITH UR BARE HANDS!! 
Be careful in handling the flash PCB. It is preferable to discharge the capacitor by triggering the flash before holding the pcb.


As you can see, the flash circuit is powered with a 1.5V AA battery. It consists of impulsing a high voltage into a xenon tube. So the circuit boosts up the battery's low voltage into a high voltage to charge the 330V capacitor. More theoretical info can be found in this nice "How camera flash work" article.

After understanding the circuit, I realized that connecting the two metallic leads M1 and M2 in the above picture causes the flash to trigger. PERFECT! (The circuit differs between different brands, however you will always find the similar two metallic leads). Again DON'T CONNECT THE LEADS WITH YOUR BARE HANDS! I soldered two wires to M1 and M2 as can be seen in the picture below.


3. The electronics:

First step in designing my circuit was to decide on the user interface. I would like the user to:
-Tweak the sound sensitivity using a potentiometer
-Tweak the time delay in the order of milliseconds between the sound detection and flash triggering using a potentiometer. (I realized during experimentation the need for this feature, else the pictures were captured a bit too early)
-Test the sound detection using a LED.
-Press a push button to switch the circuit from triggering a LED into triggering the flash.

I prefer not using a microcontroller for such a simple circuit, it would be an overkill. Let's do it the old challenging way: build my own A/D conversion and triggering.

The idea behind the circuit is simple: 
  1. Pick up the signal using an Electret microphone.
  2. Amplify the signal using LM386.
  3. Compare the amplified signal to a constant threshold (which can be modified using a pot to change sensitivity) using the LM339 comparator.
  4. The comparator's falling edge would trigger a pulse using the 555 timer in Monostable mode.
  5. The pulse would be delayed by charging an RC circuit connected to the LM339 comparator. (The resistance of the RC is actually a pot so that that we could vary the value of the time delay)
  6. The delayed pulse coming out of the comparator would go into the base of the TIP120 transistor switch that would trigger the flash.

M1 and M2 wires are connected to the H1-camera header in the schematic.
Audio: C1 - removes the DC component in the signal
C2 - removes the high frequency noise
LM386- amplifies the signal. C3 is connected between terminals 1 and 8 so that the gain is 200 (46db)
Comparator: Pot1-varies the voltage between 4.5V and 9V at the positive terminal of the LM339 comparator (because the output of the amplifier is biased at 4.5V)
When the signal voltage goes above the voltage specified by Pot1, a falling edge occurs that would trigger the 555 timer to generate a pulse.
555 timer is in Monostable mode: the pulse width is t=R4*C4*ln(3)=2.4 sec.
Time delay: The pulse generated by the 555 timer will charge the RC circuit composed of pot2 and C6.
The RC is a first order system:

which is compared using LM339 to a voltage divider circuit providing 10k/15k*Vin. As soon as this voltage is reached, the flash would trigger. The time delay equation is:


So the maximum time delay achieved with the 10K pot is t =110ms.


Trigger: Finally, the signal switches the TIP120 transistor. If SW2 is pressed, LED2 would light up (for testing purposes), otherwise the flash would trigger.

4. Prototype and PCB

One of the earliest models I made was based on the NE5532 audio amplifier. Here are a few images of the early prototype built with a perforated board.


The reason I switched to LM386 amplifier is for its simplicity, however NE5532 provided the option of having a pot in order to modify the gain.
I designed a single sided Printed Circuit Board of the new circuit


Click here to download Gerber files of the PCB.

5. Experimentation:


Here are a some of early pictures I took:
More experimentation pictures will be uploaded soon.. stay tuned.


6. Purchase

Contact us at DepotBeirut if you'd like to purchase a PCB or even a fully assembled sound triggered flash.

Cheers,