Tuesday, November 10, 2009

Switching High-Load Circuits Lab, Playing with Motors

Earlier this week I was finally able to get caught up on my lab work for Introduction to Physical Computing. The lab exercises for last week involved setting up two circuits using DC motors: (1) Transistor Lab - simple circuit that rotates the motor in one direction only; (2) H-Bridge Lab - more complex circuit that enables bi-directional rotation of the motor.

Below is a short video that features the final circuits that I set-up. This is followed by an overview of the issues that I encountered and the code for the Arduino that I developed.



Issues and Solutions
The first issue that I encountered on this lab was finding the right motor to test. Initially I wanted to test a motor with at least a 12v load. After spending some time at local Radio Shack stores, I decided against buying a new and larger motor (e.g. 12V). So I used the 3v-6v motor that came with my parts kit.

Once the motor had been selected I worked on setting up the uni-directional circuit. At first I attempted to run the circuit without soldering the wire to the motor; as expected this did not work. After soldering the wires to the motor the circuit was working fine.

Next up I moved to work on the bi-directional circuit. As outlined in my notes from class last week, in order for a motor to work in both directions we need to reverse the flow of electricity going through the motor. This is where the H-bridge comes in handy. It couples four darlington transistors in a small package specifically for this purpose. Setting up this circuit took no time.

That said, I did encounter an issue when I was playing around with the Arduino code after the circuit had been tested. The changes I made to the Arduino code included setting up a potentiometer to control the speed and direction of movement (code included below). When running this sketch my Arduino seems to disconnect and reconnect to the multimedia computer continuously.

My guess is that this may be caused by the Arduino is experiencing a brownout when the motor is pulls a lot of electricity from the circuit (e.g. on start-up and when faced with resistance). I plan to try adding some capacitors to the circuit to resolve this issue. If you have any suggestions please leave them in the comments section.


Arduino Code - Bi-Directional Motor
Here is the code that I created for the H-bridge lab. I used the sketch provided by Tom Igoe for this lab as the foundation for this code.

/* 
 * LAB - BI-DIRECTIONAL MOTOR 
 * code based on sketch from Tom Igoe
 * modified by Julio Terra
 */

  const int potPin = 1;       // Analog in 0 connected to the potentiometer
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    // H-bridge enable pin
  const int ledPin = 13;      // LED 

 int potValue = 0;            // value returned from the potentiometer
 int motorSpeed = 0;          // speed of the motor
 
 void setup() {

   // open serial port for debugging
   Serial.begin(9600);

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);
    pinMode(ledPin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH); 

    // blink the LED 3 times. This should happen only once.
    // if you see the LED blink three times, it means that the module
    // reset itself,. probably because the motor caused a brownout
    // or a short.
    blink(ledPin, 3, 300);

 }

 void loop() {

   // read the potentiometer, convert it to 0 - 255:
   potValue = analogRead(potPin);
   
    // if the switch is high, motor will turn on one direction:
    if (potValue < 512) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      motorSpeed = map(potValue, 0, 511, 255, 100);  // set motor speed based on pot position
    } 
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      motorSpeed = map(potValue, 512, 1023, 100, 255);  // set motor speed based on pot position
    }

   // set new motor speed using analog write on the enablePin
   analogWrite(enablePin, motorSpeed); 

   // de-bug by sending potValue through Serial
   Serial.print("potValue: ");
   Serial.print(potValue);
   Serial.println();  
 }

  /*
    blinks an LED
   */
  void blink(int whatPin, int howManyTimes, int milliSecs) {
    int i = 0;
    for ( i = 0; i < howManyTimes; i++) {
      digitalWrite(whatPin, HIGH);
      delay(milliSecs/2);
      digitalWrite(whatPin, LOW);
      delay(milliSecs/2);
    }
  }


No comments: