/* Arduino Operating TA8050 H-Bridge Motor Controller https://www.bristolwatch.com/arduino/arduino_pwm_hb.htm by Lewis Loflin lewis@bvu.net */ #define port1 9 #define port2 10 #define SW1 5 #define SW2 6 int val; // variable // A '1' or HIGH are both understood to both be a binary 1. // A HIGH on a port pin is 5 volts. // A '0' or LOW are both understood to be a binary 0. // A LOW on a port pin is 0 volts. void setup() { pinMode(port1, OUTPUT); // DL1 TA8050 H bridge pinMode(port2, OUTPUT); // DL2 TA8050 H bridge pinMode(SW1, INPUT); pinMode(SW2, INPUT); digitalWrite(SW1, 1); // pull up on digitalWrite(SW2, 1); // pull up on // turn off 'H' bridge: analogWrite(port1, 0); // 0 volts out or LOW analogWrite(port2, 0); // 0 volts out or LOW } // end setup void loop() { // main loop // If neither SW1 or SW2 are pushed a digitalRead(pin) // will return a HIGH because of the pull ups. // A return of LOW means the switch was pressed. // A zero is written to both PWMs on port1 and port2 // will ouput a LOW. // We need to get the value from a speed control // (variable resistor) connected // analog input 0. This ten bit value from 0-1023 // must be divided by four before // being written to a PWM pin which is 8 bits. val = analogRead(0) / 4 ; //If we press SW1: if (digitalRead(SW1) == 0) { analogWrite(port1, val); // write val to PWM port1. analogWrite(port2, 0); } // If we press SW2: if (digitalRead(SW2) == 0) { analogWrite(port1, 0); // write val to PWM port2. analogWrite(port2, val); } // while either switch is held down one can adjust the speed. // if both switches are pushed at the same time the motor will stop if ((digitalRead(SW1) == 0) && (digitalRead(SW2) == 0)) { analogWrite(port1, 0); analogWrite(port2, 0); delay(500); // delay half second for key release } } // end loop