top of page

3.17.21

LAB 7 - MOTORS

Assignment: In this lab, you will explore motors and building circuits using a DC motor driven with an H-Bridge. This lab does not require the transistor we used in the lecture - the H-Bridge is a more advanced component that contains four transistors and protection diodes already.

Part 1: DC Motor Control

Objectives:

  • Use an H-bridge to drive a DC motor

  • Send Serial Commands from P5.JS to Arduino

​

For this first part, we are using an H bridge and push button to control the direction of the motor using HIGH and LOW digital inputs.

​

​

Step 1: Gather Materials

  1. Arduino

  2. USB cable

  3. Breadboard

  4. Push Button

  5. Jumper Wires

  6. H-Bridge 

  7. DC Hobby Motor

  8. Fan

File_006 (1).jpeg

1

2

3

4

5

6

7

8

Step 2: Connect Power and Ground Rails

I like to do this before adding anything else to my breadboard. I use the black wires to go to GND on the Arduino and between the negative rails and red wires to connect the positive rails and go to 5V. 

File_003 (2).jpeg

Step 3: Add the H-Bridge and Push Button

Next, I added the h-bridge and push button to the breadboard. Based on the schematic diagram given to us, the push button is attached to pin 2, so I put it on the left side of my breadboard and my h-bridge in the middle of my breadboard. I use yellow wires to indicate connections from the push button to pin 2 and to the negative rail on my breadboard.

File_005 (1).jpeg

Step 4: Wire the H-Bridge

To connect the h-bridge, we are connecting wires from the left side of it to the Arduino and other parts of the breadboard.

 

Here is how the wires are connected:

  • Leg 1 connects to pin 9, motor speed (green wire)

  • Leg 2 connects to pin 4, (orange wire)

  • Leg 3 connects to the negative end of DC motor (black wire), not pictured so it's easier to see

  • Leg 4 connects to ground (black wire)

  • Leg 5 doesn't connect to anything

  • Leg 6 connects to the positive end of DC motor (red wire), not pictured so it's easier to see

  • Leg 7 connects to pin 3 (orange wire)

  • Leg 8 connects to the positive terminal (red wire)

File_002 (5).jpeg

Step 5: Arduino Code and Completed Circuit

Now I need code that will allow the push button to switch the direction of the motor. Normally, it reads HIGH. When the button is pushed, the pin reads LOW and will cause a change in direction.

File_001 (8).jpeg

Step 6: Video of Push Button and Schematic Diagram

Here is a video of the push button changing the direction of the motor. 

File_000 (11).jpeg

Step 7: Add an Analog Input Sensor

I decided to add a joystick that controls the speed of the motor with the x-direction.

 

In order to do this, I gathered additional materials:

  • Joystick

  • Female to male jumper wires (x3)

​

Then, I connected a black wire from GND on my joystick to the negative rail, a red wire from 5V to the positive rail, and a yellow wire from VRx to A0.

File_000 (9).jpeg

Step 8: Arduino Code

Now I need to add code that reads the input from the joystick and maps the values. Here is my new code: 

Step 6: Video of Joystick and Schematic Diagram

Here is a video of the joystick changing the speed of the motor.

File_001 (9).jpeg

Part 2: Serial Communication: Control Motor over Serial

In this part, we will control the motor using serial communication on P5.JS

​

Step 1: To begin, I removed the joystick, so it was just the push button again.

Step 2:  Write Arduino Code

Now, we need to use the code with the switch, but add in a section in our loop that reads and parses serial data. Here's my new code:

Step 3: Connect to p5 Serial Control

After uploading my code, I opened the p5 serial control and connected to COM4.

Step 4: Write p5 Code

I edited the file provided in class to use the Y value of the mouse to change the speed.

Step 5: Here is a video showing the mouse controlling motor speed

bottom of page