NPN BJT Common emitter inverting amplifier

The Phase-Shift Oscillator I previously described used an NPN transistor as an inverting amplifier.

Transistors are current controller, current amplifiers. This means they take a control current in, and this is used to produce an output current. You’ll typically see curves like this used to describe the operation of a transistor:

Bipolar_Transistor_CharacteristicCurve

I find them pretty confusing as a first introduction to transistors. Most datasheets don’t seem to include these curves either. Here’s a simpler graph:

transistor_graph

Not a very complicated graph is it. X any Y are on the same scale, so what the graph is saying is that a small change in the base->emitter current, creates a large change is the collector to emitter current. Let’s try and understand that in terms of resistance:

NPNTransistor

One way of thinking about a Bipolar transistor is as a electrically controlled variable resistor. By controlling the base current, you can control the resistance between the collector and emitter. But you generally aren’t using a current to control things in a circuit. This poses some practical problems. Let’s look at a 2N2222. You can download the datasheet here.

We can connected this up as follows:

trans_bb1

The collector is connected to the positive supply, the emitter is connected to ground. No current will flow. I set the supply to 3 volts, and no current was being drawn.

So, let’s try connecting the base to the voltage source:

trans_bb2

BANG! This wont work, there’s a current limiting resistor on the collector. So the reason isn’t that all the current flows from the collector to the emitter. No, the connection between the base and the emitter is the problem. Bipolar transistors are current controlled, and there’s effectively no resistance from the base and the emitter (as an analogy). This means that if we connect the base directly to a voltage source it will draw as much current as it can and the transistor will pop under the load.

trans_bb3

The easiest way to limit the current supplied with a voltage source is with a resistor. I added a 220Ohm resistor to the base, over 3Volts (V=IR) this gives a base->emitter current of 0.01A. Note in the above circuit there’s no resistor on the collector. But the supply showed 460mA being drawn. From this we can calculate the “current gain”. 0.01A on the base = 0.46A on the collector. Which is a gain of 46. If we look at the datasheet:

2N2222gain

We can see that’s about right, at a Collector current (Ic) of 440mA and around 1 to 10V we’re looking at a gain of 40 to 50. These results will hold as you vary the supply voltage, however you can’t go below 0.6V. The datasheet tells you this here:

2N2222saturation

So now we’ve seen how a transistor works. We’ve also seen how a voltage can be used to control the current flowing through the device. We can think of this as a current controlled variable resistor.

Well, a voltage divider looks like this:

voltage_div

Voltage dividers let us create a smaller voltage from a larger one. The smaller R2, the smaller the output voltage will be. We can therefore replace R2 with a transistor and the control a large voltage with a smaller one:

trans_volt

There’s one caveat. As we increase Vin, more current will pass though the transistor from the collector to the emitter. We’re effectively decreasing the resistance, meaning Vout will also get smaller. This means we have an inverting amplifier. The larger the input voltage, the smaller the output voltage.

There are reasons we might often prefer this over a non-inverting configuration (replacing R1 with a transistor). The most significant being that as we saw above the transistor can’t operate below 0.6V. By working in an inverting configuration we can avoid that issue.

Simple example of SDL in Emscripten (generating graphics from C)

It’s pretty easy to get emscripten installed. And straight forward to compile existing SDL code a simple:

emcc mycode.c -o mycode.html

Will work. However there are a few things to watch out for when working with SDL. Firstly, Emscripten currently supports SDl1.2 I don’t believe there are any plans for SDL2 support. Secondly you may need to change your code slightly. I tried my previous SDL1.2 example and it compiled and sort of worked, but pretty much hung the browser. The issue is that Emscripten doesn’t like infinite loops. In fact the correct way to work in emscripten is register a callback for screen rendering with emscripten_set_main_loop. You can set the required number of frames per second or allow the browser to decide. To use this function you also need to include emscripten.h. Here’s a version of my previous code modified to use this callback:

#include <string.h>
#include <SDL/SDL.h>
#include <iostream>
#include <emscripten.h>

using namespace std;
 
SDL_Surface *screen;

void renderloop() {
  SDL_Flip(screen);
  SDL_LockSurface(screen);
  for(int n=0;n<1000;n++) {
    int x=rand()%800;
    int y=rand()%600;
    int pixel=rand()*100000;
    int bpp = screen->format->BytesPerPixel;
    Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
    if((x>screen->w)||(y>screen->h)||(x<0)||(y<0)) return;
    *(Uint32 *)p = pixel;
  }
 
  SDL_Event event;
  while(SDL_PollEvent(&event)) {
    if(event.key.keysym.sym == SDLK_c     ) { SDL_FillRect(screen,NULL,0); }
  }
 
  SDL_UnlockSurface(screen);
  //SDL_Quit();
}

int main(int argc, char *argv[]) {

 
  if(SDL_Init(SDL_INIT_VIDEO)<0) {
    cout << "Failed SDL_Init " << SDL_GetError() << endl;
    return 1;
  }
 
  screen=SDL_SetVideoMode(800,600,32,SDL_ANYFORMAT);
  if(screen==NULL) {
    cout << "Failed SDL_SetVideoMode: " << SDL_GetError() << endl;
    SDL_Quit();
    return 1;
  }
 
  emscripten_set_main_loop(renderloop,0,0);
 
  return 0;
}

You can compile it using the command above (assuming you save it as mycode.cpp). And you should see the following output in firefox:

emscripten_graphics

Phase Shift Oscillator – High Pass configuration

Yesterday I took a look at a phase shift oscillator. The design I showed used a capacitor in a low pass configuration. I explained why this caused a phase shift allowing us to construct an oscillator.

Today, I tried using the capacitors in a high pass configuration. To understand how this circuit works, we again need to understand how a capacitor in series will cause a voltage phase shift.

phaseshift_high

The diagram above shows the configuration used, and it’s surprisingly similar to that shown yesterday. However this time when a voltage build up on the top of our capacitor the capacitor will need to draw current though the lower resistor to equalize the voltage. This will take time, causing a delay, and therefore a phase-shift as before. As before the phase-shift is limited to 90degrees (and a larger shift causes more attenuation). The circuit below uses 3 such circuits to generate a 180degree phase-shift:

phaseshift_sch2

I built the above circuit using a 2N2222 (a 2N3904 works as well). I used 22nF capacitors. Unlike the previous example these can’t be electrolytic. The resistors on the caps were 2.7K. The resistor going from the supply to the emitter was 12k and from the base to the supply 600k.

phaseshift_highpass

And here’s the output:

phaseshift_h_trace

Phase Shift Oscillator

Today I’ve been trying to understand the phase-shift oscillator. In order to understand the phase-shift oscillator, you first need to understand the phase-shift caused by a capacitor!

The figure below shows a single capacitor causing a phase shift:

phaseshift

It took me a little while to understand why a capacitor in this configuration causes a voltage phase shift. However, fundamentally the charging of the capacitor causes a lag, it take the capacitor a while to “catch up” with the current voltage, so it appears to lag behind the input. This means the amount shifted will depend on the capacity of the capacitor (how long it takes the capacitor to get up to full voltage). The capacitor can only lag behind the current voltage. This means that you can shift the phase by at most 90 degrees. Any more that this and the capacitor would be fighting the input voltage, rather than approaching it’s current value. The phase-shift will also always cause some attenuation, that the capacitor is trying to “catch up” with the current voltage, but never quit getting there. It’s worth note that if you overlay the original sine wave and the shifted version, the shifted wave will always cross the input at the shifted versions maximum voltage. You can therefore see the relationship between attenuation and the degree of phase-shift.

Hopefully that helps explain phase-shift a little. For a phase-shift oscillator we use this characteristic to generate an oscillation.

Here’s the phase-shift oscillator circuit, there are different configurations, but this is the one we’re going to talk about:

phaseshift_sch

I’ve drawn the schematic so the 3 phase-shift resistor/capacitors line up. Each set will shift the phase of the input ~60 degrees. This results in a 180 degree shift. Remember because we can only shift by less than 90 at a time we need 3 sets of capacitors. We’ll also be significantly attenuating the input with each shift.

phaseshift2

The output of the phase-shift network in this oscillators drives the input to an amplifier (in this case a transistor) in a feedback loop. But why do we want to shift by 180 degrees? Well, if we shift a sine wave by 180 degrees we’ll end up with the exact opposite of our signal. The NPN transistor we’re using is in an inverting configuration (you might want to read my other post about that), this effectively gives another 180degrees of shift. The result is a resonance at a frequency determined by the phase-shift network resulting in an oscillation.

Here’s a simulation of the phase-shift circuit shown above:
phaseshift_sim
You can also download the file for iCircuit here.

I built the circuit up using an 2N2222 and the approximate values in the simulation above:

phaseshift_r2

It worked pretty well, output a 20Hz sine wave which is approximately as expected from the simulation:
phaseshift_r

Here’s a clearer picture of the trace:
phaseshift_trace