Navigation

Saturday, 11 April 2015

Difference Amplifier


The difference amplifier amplifys the difference between two voltages.


Why low impedance is ensured during EEG experiments

Electroencephalography (EEG) is the recording of electrical activity along the scalp. It is often required to ensure a low impedance between the scalp and the electrodes. Here is why.


From the figure above,
Rin_amp is the input resistance of the amplifier the EEG electrode is connected to.
Vin is the true voltage of the EEG signal
Reeg is the impedance between the scalp and the electrode
The output voltage, Vout, is the voltage that appears at the input of the amplifier and is given by:


This output voltage is the one that is amplified by the op-amp.

As shown by the equation above, if Rin_amp is far greater than Reeg, Vout goes into the op-amp will be approximately equal to Vin and so there is much confidence that we are amplifying the the input voltage, Vin.

But if otherwise, Vout will be attenuated and will not reflect the input voltage, Vin.

Therefore the input impedance of the amplifier has to be far greater than the input impedance of the EEG signal. Also, it is necessary to keep the input impedance of the EEG signal as low as possible.

Op-Amps: Why a high input resistance and low output resistance is required

A very high input resistance is required so that the input becomes an open circuit. A very high input resistance will imply an open circuit. As shown in the diagram below, for an open circuit, the current through Vin will be zero.



From KVL,
-Vs+VRi +Vin=0
-Vs+iRs+Vin=0
Since i = 0,
-Vs+Vin = 0
Therefore, Vs=Vin

If it is an open circuit, the exact source voltage, Vs, gets transferred to the input as Vin. This means that the Ri resistor almost did nothing. Therefore we need a very hight input resistance, Ri, to achieve a zero current, i, so that it becomes an open circuit and all the source voltage will be the input voltage.

This is also true for a voltmeter. A voltmeter should have a very high resistance so that little or no current is able to flow meaning that all the source voltage of the instrument being measured will appear at the input.


A very low output resistance is required so that all the current will be consumed by the load. A very low output resistance is required for an op-amp so that if a load RL is placed in series with it, all the current will be consumed by the load. See the figure below.


From the figure above, this is a short circuit so Vin=0. This implies that all the source current will be eaten up by the load

Interrupts in Microcontrollers

Interrupts can be defined as a hardware triggered software event. They are used in microcontroller programming to ensure that events occur in real time.

To implement interrupts, the condition or event is placed inside the Interrupt Service Routine (ISR). Say, when we press a switch we want a response within a specified period, this if statement followed by the corresponding action is placed in the ISR. The ISR is always called at a frequency/period given by the value written into the RELOAD register.

Another use of interrupt is producing a sound at a particular frequency. This is achieved because interrupts are implemented at a given frequency, so the frequency of the interrupt will be the frequency of the sound produced. Other uses are in obtaining sensor readings at every specified time.

In short, interrupts are used whenever we want to produce an output at a particular time irrespective of the line of code or step currently being executed by the microcontroller in the infinite loop. When the hardware event is triggered, the software will round up its current line of execution, then jump to the ISR to execute the codes, after execution, program flow would return to where it left off in the infinite loop.

Design of a 4-bit Digital to Analog Circuit

A DAC is a digital to analogue circuit. The microcontroller produces a digital value while the DAC converts it to an analog value. A DAC can be implemented using either a binary-weighted circuit or an R-2R ladder circuit.

For the binary weighted circuit, assuming a 4-bit DAC, there are 16 levels which are from 0-15. Assuming the voltage is between 0 – 5v

Digital Value (N)
b3
b2
b1
b0
Analog Value
0
0
0
0
0
0
1
0
0
0
5.0V
5.0*(1/15)
2
0
0
5.0V
0
5.0*(2/15)
3
0
0
5.0V
5.0V
5.0*(3/15)
4
0
5.0V
0
0
5.0*(4/15)
5
0
5.0V
0
5.0V
5.0*(5/15)
6
0
5.0V
5.0V
0
5.0*(6/15)
7
0
5.0V
5.0V
5.0V
5.0*(7/15)
8
5.0V
0
0
0
5.0*(8/15)
9
5.0V
0
0
5.0V
5.0*(9/15)
10
5.0V
0
5.0V
0
5.0*(10/15)
11
5.0V
0
5.0V
5.0V
5.0*(11/15)
12
5.0V
5.0V
0
0
5.0*(12/15)
13
5.0V
5.0V
0
5.0V
5.0*(13/15)
14
5.0V
5.0V
5.0V
0
5.0*(14/15)
15
5.0V
5.0V
5.0V
5.0V
5.0

The resolution of this DAC which represents the smallest change it can capture is given by the difference between each consecutive elements is 5v/15=0.33V. 

The b0 is the LSB while b3 is the MSB. The resistor values are chosen in the ratio of 1/2/4/8 such that the MSB, b3, is attached to the least value of resistor while the LSB, bo, is attached to the highest value of resistance. Bit 0 is the LSB so it is subjected to a higher resistance, Bit 3 is the MSB so it is subjected to the least resistance. The higher the significance of the bit, the lower is the resistor it is connected to


Doing the analysing of the circuit when N=8 from the table, b3-b0 is given by 1000 where b3 is at 5.0V. This means at the circuit becomes:


The equivalent circuit becomes:



So Vout needs to be obtained. The equivalent resistance of 3KOhms. 6KOhm and 12KOhms in parallel is 12/7 KΩ. Therefore, the circuit can be further reduced to:


From voltage divider rule,

This results in the same analog value for the N= 8 as shown in the table

Taking another condition, say N= 12, the equivalent circuit becomes


The equivalent circuit becomes:


Which can be further reduced to:




This value is also the same as the analogue value in the N=12 row of the table.

Inorder to implement the DAC binary-weighted circuit, the value of resistors required has to be 1/2/4/8/16/32/64 ... which will continue to increase depending on the resolution of the DAC. Therefore a 10-bit DAC will require resistors which are in multiples of 2 up to 1024. Generally, it might be difficult to obtain these resistance values. However, when 2 resistors of the same value are connected in parallel, the resulting resistance is half of the value of each of the resistors. Likewise, connecting 2 same valued resistors in series will produce a resistance that is twice each of the resistor. This can be taken into consideration when the exact value of resistance is not available.

Another way of implementing the DAC is an R-2R ladder network. In this case, just two resistance values are required but the number of resistors needed is increased when compared with the binary weighted circuit



Saturday, 31 January 2015

Wheatstone Brigde

A Wheatstone bridge circuit as shown below is used as a sensor to measure very small voltage in the order of millivolts. It is often used when greater sensitivity is required. The Wheatstone bridge consists of 4 resistors. When all the resistances are equal, no voltage is seen across the bridge. When used as a sensor, resistors R1-R3 are of the same value while the resistance of resistor R4 which is the sensor will vary according according to the change in resistance. R4 can be a photocell whose resistance vary with light intensity, strain guage whose resistance varies with length or a flex sensor that changes resistance based on the degree of bend.

A Wheatstone bridge consist of two voltage divider circuit. As shown in the circuit above, the voltage across R3 is given by:

while the voltage across R4 is given by:

Therefore, the voltage across the Wheatstone bridge is given by:

Since R1 = R2 = R3 = R,

Therefore, if R equals R4, the output voltage is zero; if R is greater than R4, the output voltage is negative; if R is less than R4, the output voltage is positive. The changes in the output voltage is in the order of mV and therefore sensitivity is improved using a Wheatstone bridge configuration. A voltmeter is used to read this voltage. If this output voltage is to be read by a microcontroller, an operational amplifier will be required to amplify the voltage.

Friday, 9 January 2015

Tracking three colours with Processing

This tutorial explains the coding and algorithm to track three colours (red, green and blue) with Processing Language.



import processing.video.*;

Capture video;

void setup() {
  size(640, 480);
  // Uses the default video input, see the reference if this causes an error
  video = new Capture(this, width, height);
  video.start();  
  noStroke();
  smooth();

}

void draw() {
  if (video.available()) {
    video.read();
    image(video, 0, 0, width, height); // Draw the webcam video onto the screen
    
    int pixelRed = 0;   //value of red pixel
    int pixelRed_g = 0;
    int pixelRed_b = 0;
    int redX = 0;           // X-coordinate of the red video pixel
    int redY = 0;           // Y-coordinate of the red video pixel
    int reddest = 0;        // the brightest red pixel in the video
    int reddest_g = 64;
    int reddest_b = 64;
    
    int pixelGreen = 0;  //value of green pixel
    int pixelGreen_r = 0;
    int pixelGreen_b = 0;
    int greenX = 0;          // X-coordinate of the green video pixel
    int greenY = 0;          // Y-coordinate of the green video pixel
    int greenest = 0;        // the brightest green pixel in the video
    int greenest_r = 64;
    int greenest_b = 64;

    int pixelBlue = 0;  //value of blue pixel
    int pixelBlue_r = 0;
    int pixelBlue_g = 0;
    int blueX = 0;          // X-coordinate of the blue video pixel
    int blueY = 0;          // Y-coordinate of the blue video pixel
    int bluest = 0;         // the brightest blue pixel in the video
    int bluest_r = 64;
    int bluest_g = 64;
    
    float brightestValue = 0; // Brightness of the brightest video pixel
    // Search for the brightest pixel: For each row of pixels in the video image and
    // for each pixel in the yth row, compute each pixel's index in the video
    video.loadPixels();
    
    int index = 0;
    for (int y = 0; y < video.height; y++) {
      for (int x = 0; x < video.width; x++) {
        // Get the color stored in the pixel
        color pixelValue = video.pixels[index];
        
        // Extract the red, green, and blue components of the current pixel's color
        int currR = (pixelValue >> 16) & 0xFF;
        int currG = (pixelValue >> 8) & 0xFF;
        int currB = pixelValue & 0xFF;
      
        // If that value is red, find the brightest pixel and store its (x,y) location 
        
        if ( (currR > 128) && (currG < 64) && (currB < 64) ) {
          pixelRed = currR; pixelRed_g = currG; pixelRed_b = currB;
          if ((pixelRed > reddest) && (pixelRed_g < reddest_g) && (pixelRed_b < reddest_b)) {
            reddest = pixelRed; reddest_g = pixelRed_g; reddest_b = pixelRed_b;
            redY = y;
            redX = x;
          }
        }
        
        
        // If that value is green, find the brightest pixel and store its (x,y) location
        if ( (currR < 64) && (currG > 64) && (currB < 64) ) {
          pixelGreen = currG; pixelGreen_r = currR; pixelGreen_b = currB;
          if ((pixelGreen > greenest) && (pixelGreen_r < greenest_r) && (pixelGreen_b < greenest_b)) {
            greenest = pixelGreen; greenest_r = pixelGreen_r; greenest_b = pixelGreen_b;
            greenY = y;
            greenX = x;
          }
        }
        
        // If that value is blue, find the brightest pixel and store its (x,y) location
        
        if ( (currR < 64) && (currG < 64) && (currB > 128) ) {
          pixelBlue = currB; pixelBlue_r = currR; pixelBlue_g = currG;
          if ((pixelBlue > bluest) && (pixelBlue_r < bluest_r) && (pixelBlue_g < bluest_g)) {
            bluest = pixelBlue; bluest_r = pixelBlue_r; bluest_g = pixelBlue_g;
            blueY = y;
            blueX = x;
          }
        }
        
        index++;
      }
    }
    
    // Draw a red circle at the red pixel
    fill(255, 0, 0, 255);
    ellipse(redX, redY, 20, 20);
    
    // Draw a green circle at the green pixel
    fill(0, 255, 0, 255);
    ellipse(greenX, greenY, 20, 20);
    
    // Draw a blue circle at the blue pixel
    fill(0, 0, 255, 255);
    ellipse(blueX, blueY, 20, 20);
    
  }
}

Connecting a switch to a microcontroller: Pull-Up and Pull-Down Resistors

A switch can be connected to a microcontroller using either a pull-up or pull-down resistor. The pull-up and pull-down resistors ensure that the wire is at a defined logic level even if no active devices are connected to it.

Pull-Up Resistor



Pull-up resistor pulls the voltage of the wire it is connected to towards the voltage source level when all the other components on the line are inactive. When all other components on the line are inactive, they are high impedance and act like they are disconnected. The pull-up resistor brings the wire up to the HIGH logic level. When another component goes active, it will override the HIGH logic level set by the pull-up resistor. The microcontroller is HIGH when the switch is not connected and LOW otherwise.

Pull-Down Resistor



A pull-down resistor works in the same way but it is connected to GND. It holds the logic signal near zero volts when no other active device is connected. The microcontroller is LOW when the switch is not connected but HIGH otherwise. It is preferable to use a pull-down resistor.