H-Bridge galvanometer control

IMG_0500

A galvanometer is a very sensitive meter, which can be configured as an ammeter or a voltmeter, depending on how it is wired.  Here I’m using it as a voltmeter, which requires a high resistance in series with the voltage input, so it doesn’t just peg out.  An ideal voltmeter has an infinite resistance, an ideal ammeter has zero resistance … and as something that could be theoretically used for either, there’s a very low resistance across an ammeter.

The one I got requires a 54k-Ohm resistance in series to bring down the voltage from the Arduino 5V output to achieve a full-range reading.  To switch the direction I’m using an H-Bridge, which can be switched in direction based on which direction current is applied: +5V on (7) makes the meter swing to the left, +5V on (8) makes the needle swing to the right.  Pretty simple.

Because I’m using this as a tuning indicator, it’s set up to swing full left or right when it reaches the edge of a ‘window’, and center when the potentiometer value is at the target

IMG_0497

Notes:
(2), (7), (8), (3) refer to Arduino I/O pins

IMG_0494 

 

 

//Galvanometer testing program.  Use with potentiometer on analog pin 0 (zero)

// Pin Assignments
int enablePin = 2; // provides power to enable the H-Bridge
int galvNegPin = 7; // negative galvanometer power – H-Bridge leg 1 (pin 2, 1A)
int galvPosPin = 8; // positive galvanometer power – H-Bridge leg 2 (pin 7, 2A)
int galvFeedPin = 3; //galvanometer feed, PWM

//analog input pins for potentiometer
int tuningPin = 0;

//initial settings

int galvanometer = 0;  //NOTE: use 54k Ohm resistance string for full scale galvanometer reading (on Simpson alvanometer)

int target = 300;  //set tuning value (potentiometer value)
int tuning = 0;    //initial tuning value for the sake of initialization
int window = 50;   //
int track = 0;    //

void setup()
{
  Serial.begin(9600); // enable serial comm for debug panel
  //set other pin assignments
  pinMode(galvNegPin, OUTPUT);
  pinMode(galvPosPin, OUTPUT);
  pinMode(galvFeedPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  digitalWrite(enablePin, LOW); //set enablePin low to disable H-Bridge, zero galvanometer
}

void loop()
{
  tuning = analogRead(tuningPin); // read tuning potentiometer value
  if (tuning < 500-window/2 or tuning < 500 + window/2)
  {
  target = 500; //0..1023 equivalent for target
  tune(tuning, target);  // call tune sub, pass tuning pot value
  }
}

void tune(int tuning, int target) // tune sub, passes tuning and target values
{
  Serial.println(tuning); //print value to debugger
  if (tuning >= target-window/2 and tuning < target) // tuning below target, within window/2  needle to left
  {
    galvanometer = ((target+tuning)/ (window/2));
    galvanometer = map(tuning, target-window/2, target, 255, 0); // map (var, low in, high in, low out, high out) NOTE: reversed to move galvanometer needle the right way from below towards zero
    digitalWrite(enablePin, HIGH); // enable H-Bridge
    digitalWrite(galvNegPin, HIGH); //power H bridge negative circuit;
    digitalWrite(galvPosPin, LOW); // unpower H bridge positive circuit;
    analogWrite(galvFeedPin, galvanometer); //send voltage to the galvanometer feed pin (3)
  }

  else if (target < tuning and tuning <= target + window/2) // tuning above target, within window/2  needle to right
  {
    galvanometer = ((target+tuning)/ (window/2));
    galvanometer = map(tuning, target-window/2, target, 0, 255); // map (var, low in, high in, low out, high out)
    digitalWrite(enablePin, HIGH);
    digitalWrite(galvNegPin, LOW); //power H bridge negative circuit;
    digitalWrite(galvPosPin, HIGH); // unpower H bridge positive circuit;
    analogWrite(galvFeedPin, galvanometer);
  }
  else // out of tuning window
  {
    digitalWrite(enablePin, LOW);  // disable H-bridge
    galvanometer = 0; // set galvanometer read to zero
    analogWrite(galvFeedPin, galvanometer);
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s