Saturday, February 24, 2007

H-Bridge PCBoard + Arduino Code

PCB layout for connecting the H-bridge to Arduino w/four input slots (for photocells, resistors, switches, etc.) The L293E (SN754410) is placed in center with traces for connecting to Arduino, the two motors, 4 photocells, resistors, capacitor, etc. The four inputs allow for a variety of sensors and/or switches to be on one board which can be used as inputs for Arduino.

The following is a revised version of Arduino code, which allows two photocells to control the direction each motor is rotating. The motors will rotate in one direction if exposed to light + another if the light level is reduced. Changes to the threshold amount determines how sensitive the sensors react.

/* Controlling Two DC Motors Using H-Bridge + PhotoCell
* ------------
*
* Uses an H-Bridge (L293E or SN754410) to control the direction of two DC motors.
* Additional DC motor added. Photocells used to control direction of each motor.
* Modification to Physical Computing tutorial:
* http://itp.nyu.edu/physcomp/Labs/DCMotorControl
*
* Modified 23 February 2007
* By Kyle Janzen
* http://kylejanzen.blogspot.com/
*
* based on an original by Physical Computing @ ITP
*/

int photo1Pin = 1; // select analog input pin for photocell1
int photo2Pin = 2; // select analog input pin for photocell2
int val1 = 0; // variable to store the value coming from photocell1
int val2 = 0; // variable to store the value coming from photocell2
int threshold1 = 410; // threshold1: adjust to light conditions
int threshold2 = 250; // threshold2: adjust to light conditions
int motor1Pin = 3; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int motor3Pin = 5; // H-bridge leg 3
int motor4Pin = 6; // H-bridge leg 4
int speed1Pin = 9; // H-bridge enable pin 1-2
int speed2Pin = 10; // H-bridge enable pin 3-4

void setup()
{

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(speed1Pin, OUTPUT);
pinMode(speed2Pin, OUTPUT);

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

}

void loop()
{
val1 = analogRead(photo1Pin); // set val1 to equal reading of photo1Pin
if (val1 >= threshold1) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}else{
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
val2 = analogRead(photo2Pin); // set val2 to equal reading of photo2Pin
if (val2 >= threshold2) {
digitalWrite(motor3Pin, LOW); // set leg 3 of the H-birdge low
digitalWrite(motor4Pin, HIGH); // set leg 4 of the H-bridge high
}else{
digitalWrite(motor3Pin, HIGH); // set leg 3 of the H-bridge high
digitalWrite(motor4Pin, LOW); // set leg 4 of the H-bridge low
}
}

No comments: