Navigation

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.