top of page

Lab 7: Motors &

Serial

part 1

step 1: power, ground & button

I began by connecting vin and ground from my Arduino to both sides of the breadboard, and adding a button connected to digital pin 2 and to ground. 

20210317_225306.jpg
20210317_225147_edited.jpg

Button, Power & Ground Wiring

step 2: H-Bridge

I then added my H-bridge to the center of the breadboard and began making connections to its legs. I connected the top-left leg to D9 ("enablePin"), the second leg from the top-left to D4 ("motor2Pin"), and the second leg on the bottom-left to D3 ("motor1Pin"). Lastly, I connected the bottom left leg to Vin, the top right leg to vin, and the fourth leg from the top-left to ground. 

20210317_224527.jpg
20210317_224639.jpg

H-Bridge & Arduino Wiring

20210317_224554.jpg

step 3: adding the motor

My motor broke so I had to take it apart and rewire it with some extra cables. I then connected the positive wire of the motor to the third leg from the top-left on the h-bridge, and the negative wire to the third leg from the bottom left. 

20210317_152844.jpg
20210317_160418.jpg

motor connected to H-bridge

circuit wiring

step 4: programming the Arduino

In my code liked below, I gave my digital pins variables, and used the pinMode() function to set them as inputs and outputs. All of the pins were outputs except for the button, aka "switchPin," which was used to change the direction of the motor's rotation. I then used the digitalWrite() function to set the button to a high state, and an-if statement to distinguish if the button was being pressed. The button signals either motor 1 and motor 2 to either be in a HIGH state, deciding the direction of rotation. Next, I used the analogWrite() function on the "enablePin" variable to change the speed of the motor. I tested values from 50 to 200 to see their effect. For help making this code, I referenced my Lab 3 code which was quite similar. 

testing motor& switch button

testing code & motor speed 

step 5: adding a potentiometer

I added a potentiometer to control the speed of the motor through analog pin 3. I did this by connecting the 2-pronged side to vin and ground, and the 1-pringed side to A3 on the arduino. In my code, I created 2 new variables, one for the pin itself, "speedPin," and one for the motor speed, "mspeed." I then used analogRead() to determine the value of the potentiometer, mapped this value from 0-1023 to 0-255, and set the speed equal to this value. I then set the motor to this speed, using analogWrite() to change the value of the "enablePin" to the "mspeed" variable as the dial is turned. When needed, I referenced my code from lab 4 which also used a potentiometer. 

20210317_160144.jpg
20210317_155647_edited.jpg

Adding Potentiometer to A3

20210317_170215.jpg

potentiometer + motor wiring

testing potentiometer control

20210317_213616.jpg

part 1 / 2 schematic ( potentiometer removed for p2)

part 2

step 1: serial communication

For part 2, I began by removing the potentiometer from my breadboard, and building off of my original code. I then pasted the code from the "loop" section into the pre-made code snippet from the lab instructions. I also changed the value of the "enablePin" variable to the value of the received data from the serial monitor using the analogWrite() function (as done in the previous steps). 

20210317_160418.jpg

circuit wiring from part 1

using serial input to change motor speed

step 2: serial input via p5.js 

I opened the port connected to the Arduino and began communications from the Arduino to the p5.js editor through the p5 serial control app. In the serial template, I added my port name ('COM5'), and adjusted the code snippet provided in the lab instructions. With the two additional lines of code, I changed the speed of my motor by manually changing the value of the variable inside of the serial.write() function within draw().

testing serial input values in p5.js

step 3: mouse-controlled serial input via p5.js 

My last step was to set the value of the serial variable, 'variable1,' to the mapped value of 'mouseX' from 0-400 (the size of the canvas) to 0-255 (the range of the motor). By replacing this line of code, I made the motor speed equal to the x-position of the mouse on the canvas.

Serial Input with p5.js

bottom of page