Project 10: Zoetrope H-Bridge Debugging

I’m having trouble completing Project 10: Zoetrope. I followed the build instructions and used the provided code. The motor does not move.

I added print statements to the code to check that the input and output are as expected.

I also checked the voltage to the input pins of the H-Bridge and they are as expected. I checked that Pin 1 on H-Bridge receives 0V before I click the first switch and a variable voltage (changed from the potentiometer) after I click the first switch. I also checked that Pins 2 and 7 receive high (5V) and low voltages and they swap when I click the second switch. I also checked that 9V go into Pin 8.

I can only detect 0V on the output pins of the H-Bridge. I verified that the motor is functional by connecting it directly to power.

It looks like the only thing left to check is the H-Bridge. How can I confirm that it is working as expected? Do you see anything wrong with my setup? Is there anything else I can try?


Here is a photo of the circuit. The middle pin of the potentiometer is connected from the right side of the breadboard to the left side by a small jumper under the potentiometer.


Here is the code with my print statements.

const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9;   // connected to pin 1 on the H-bridge
const int directionSwitchPin = 4;  // connected to the switch for direction
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
const int potPin = A0;  // connected to the potentiometer's output

// create some variables to hold values from your inputs
int onOffSwitchState = 0;  // current state of the on/off switch
int previousOnOffSwitchState = 0; // previous position of the on/off switch
int directionSwitchState = 0;  // current state of the direction switch
int previousDirectionSwitchState = 0;  // previous state of the direction switch

int motorEnabled = 0; // Turns the motor on/off
int motorSpeed = 0; // speed of the motor
int motorDirection = 1; // current direction of the motor

void setup() {
  // initialize the inputs and outputs
  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // pull the enable pin LOW to start
  digitalWrite(enablePin, LOW);

  Serial.begin(9600);
}

void loop() {
  // read the value of the on/off switch
  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);

  // read the value of the direction switch
  directionSwitchState = digitalRead(directionSwitchPin);

  // read the value of the pot and divide by 4 to get a value that can be
  // used for PWM
  motorSpeed = analogRead(potPin) / 4;

  Serial.print("onOFF: (");
  Serial.print(previousOnOffSwitchState);
  Serial.print(") ");
  Serial.print(onOffSwitchState);
  Serial.print("\t direction: (");
  Serial.print(previousDirectionSwitchState);
  Serial.print(") ");
  Serial.print(directionSwitchState);
  Serial.print("\t speed: ");
  Serial.print(motorSpeed);
  Serial.print("\t enabled: ");
  Serial.print(motorEnabled);

  // if the on/off button changed state since the last loop()
  if (onOffSwitchState != previousOnOffSwitchState) {
    // change the value of motorEnabled if pressed
    if (onOffSwitchState == HIGH) {
      motorEnabled = !motorEnabled;
    }
  }

  // if the direction button changed state since the last loop()
  if (directionSwitchState != previousDirectionSwitchState) {
    // change the value of motorDirection if pressed
    if (directionSwitchState == HIGH) {
      motorDirection = !motorDirection;
    }
  }

  // change the direction the motor spins by talking to the control pins
  // on the H-Bridge
  if (motorDirection == 1) {
    Serial.print("\t1 HIGH\t2 LOW\tmotor: ");
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
  } else {
    Serial.print("\t1 LOW\t2 HIGH\tmotor: ");
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
  }

  // if the motor is supposed to be on
  if (motorEnabled == 1) {
    Serial.println(motorSpeed);
    // PWM the enable pin to vary the speed
    analogWrite(enablePin, motorSpeed);
  } else { // if the motor is not supposed to be on
    Serial.println("0");
    //turn the motor off
    analogWrite(enablePin, 0);
  }
  // save the current on/off switch state as the previous
  previousDirectionSwitchState = directionSwitchState;
  // save the current switch state as the previous
  previousOnOffSwitchState = onOffSwitchState;
}

Here is some the output produced when I click the switches, the number in the parenthesis is the previous value:

12:03:28.230 -> onOFF: (0) 0	 direction: (0) 0	 speed: 108	 enabled: 0	1 LOW	2 HIGH	motor: 0
12:03:28.329 -> onOFF: (0) 0	 direction: (0) 0	 speed: 108	 enabled: 0	1 LOW	2 HIGH	motor: 0
12:03:28.396 -> onOFF: (0) 1	 direction: (0) 0	 speed: 108	 enabled: 0	1 LOW	2 HIGH	motor: 108
12:03:28.495 -> onOFF: (1) 1	 direction: (0) 0	 speed: 108	 enabled: 1	1 LOW	2 HIGH	motor: 108
12:03:29.657 -> onOFF: (0) 0	 direction: (0) 1	 speed: 108	 enabled: 1	1 HIGH	2 LOW	motor: 108
12:03:29.724 -> onOFF: (0) 0	 direction: (1) 1	 speed: 108	 enabled: 1	1 HIGH	2 LOW	motor: 108
12:03:33.575 -> onOFF: (0) 1	 direction: (0) 0	 speed: 108	 enabled: 1	1 LOW	2 HIGH	motor: 0
12:03:33.641 -> onOFF: (1) 1	 direction: (0) 0	 speed: 108	 enabled: 0	1 LOW	2 HIGH	motor: 0

Looks like, from the image, you forgot to connect pin 16 of the L293D to 5V.

1 Like

That was it. Worked like a charm. Thanks so much!