Week7- pcomp lab
To control the DC motor, I used the H-bridge which have six transistors-two for switches, four for inner circuit. If pin 1 and 3 are open, pin 3 and 4 are closed. In other words, one is high, the other is low.
Four pins(4,5,12,13) in the middle of the H-bridge are for the ground. 1 and 9 pins are enable pins. 3 and 6 are for one motor. 11 and 14 are the other motor(H-bridge can control two motors. 2 and 7 is for the Arduino pin to control each H-bridge leg. Depending on the switch’s mode, the motor’s direction can be changed.
When I supplied the 12V power, I could smell something burning. One of the classmate said this was because of turning the direction quickly. Later I found that the motor’s power is for 6V based on the data instruction. According to the instruction, I used too much power.
<Transistor>
Transistos => electronic devices that control a large current from a smaller current. There are three connections referred as the base, the collector, and the emitter. By putting a small voltage and current on the base of a transistor, you allow a larger current to flow from the collector to the emitter. They function as amplifiers. There are two type of bipolar transistors : One is NPN, the other is PNP type. The NPN is equivalent a normally open switch and PNP is equivalent to a normally closed switch. <from “Physical Computing”>
Program the microcontroller to run the motor through the H-bridge:
int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int speedPin = 9; // H-bridge enable pin
int ledPin = 13; //LED
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set speedPin high so that motor can turn on:
digitalWrite(speedPin, 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, 100);
}
void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// 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
}
}
/*
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);
}
}


