Learning Arduino for beginners EP#26 controlling DC motors with a H-Bridge

In this tutorial we will take a look at how you can use a h bridge to control the speed and direction of DC motors…





Arduino Code:

#define pin1 11
#define pin2 12
#define speedPin 10
#define f 2
#define r 5
#define s 9
int Speed;
int fv;
int rv;
int sv;

void setup() {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(f, INPUT_PULLUP);
pinMode(r, INPUT_PULLUP);
pinMode(s, INPUT_PULLUP);

void loop() {
Speed = analogRead(A0);
Speed = /= 4;
analogWrite(speedPin, Speed);
fv = digitalRead(f);
rv = digitalRead(r);
sv = digitalRead(s);

if (fv == LOW)
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
if (rv == LOW)
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
if (sv == LOW)
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);