This projects showcase how to use Digilent Pmod HB5Pmod HB5 H-bridge driver and Arduino Uno to drive the DC motor. The Pmod HB5 offers a 2A H-bridge circuit to drive small- to medium-sized DC motors. Two sensor feedback pins are incorporated into the motor connection header and are specifically designed to work with the Digilent motor/gearbox, which incorporates quadrature encoder feedback. An Arduino Uno can be used to control the motor direction and speed. Digilent Pmod BTN pushbutton let users input the speed and direction.
First of all, you need to connect the Pmod HB5 and Pmod BTN to Arduino Uno. To drive the DC motor, you need to connect a 9V battery to Pmod HB5.
Wiring instructions:
Pmod HB5 <----------> Arduino Uno <----------> Pmod BTN
VCC to 5V to VCC
GND to GND to GND
DIR to 2 to -
EN to 3 to -
- to 4 to BTN0
- to 5 to BTN1
- to 6 to BTN2
Then, you can enter the below Arduino code in the Arduino IDE and upload the sketch.
/************************************************************************ Test of the Pmods HB5 and BTN ************************************************************************* Project description: The pushbutton BTN0 changes the direction of the motor The pushbutton BTN1 increases the motor speed. The pushbutton BTN2 decreases the speed of the motor. The motor speed is displayed on the serial monitor. ***********************************************************************/ //defining connections #define DIR 2 #define EN 3 #define BTN_0 4 #define BTN_1 5 #define BTN_2 6 void setup() { Serial.begin(9600); // Initialization of the serial monitor pinMode(DIR, OUTPUT); // Configure DIR to an output pin digitalWrite(DIR, LOW); // Set DIR Low pinMode(EN, OUTPUT); // Configure EN to an output pin digitalWrite(EN, LOW); // Set EN Low pinMode(BTN_0, INPUT); // Configure BTN_0 to input pin pinMode(BTN_1, INPUT); // Configure BTN_1 to input pin pinMode(BTN_2, INPUT); // Configure BTN_2 to input pin } unsigned int spd = 0; //define motor speed as unsigned integer and set initial motor speed to 0 bool dir = LOW; //define dir (motor direction) as a Boolean variable and set initial direction to low void loop() { //read buttons bool b0 = digitalRead(BTN_0); // Reading BTN_0 bool b1 = digitalRead(BTN_1); // Reading BTN_1 bool b2 = digitalRead(BTN_2); // Reading BTN_2 //increment motor speed by 5 if button 1 is pressed and spd is less than the maximum duty cycle (255) if (spd < 255 && b1) { spd += 5; } //decrement motor speed if button 2 is pressed and spd is greater than 0 if (spd > 0 && b2) { spd -= 5; } //change direction if button 0 is pressed if (b0) { dir = !dir; } //control motor digitalWrite(DIR, dir); //set direction analogWrite(EN, spd); //Use PWM to control the motor speed //display data Serial.print("Motor speed: "); Serial.print(map(spd, 0, 255, 0, 100)); //convert pwm to percentage Serial.println("%"); Serial.print("Direction: "); if (dir) { Serial.println("counter-clockwise"); } else { Serial.println("clockwise"); } delay(100); }
The direction and the speed of the motor are displayed in the serial monitor.