Motors
In this lab, I explored different types of motors and used Arduino to program the motors’ behavior. Each type of motor has it’s own advantages and disadvantages, so experimenting with a variety of types was a good introduction to which scenarios call for different types of motors.
Part 1: Servo Motor Control
Servo motors use gears to rotate. There are two kinds of servos, one that can be positioned to an angle from 0 – 180, and the other is continuous rotation. Because of the gears, servo motors have high torque but do not rotate with high speed. They’re particularly useful for robotics and airplane wings because of their ability to be positioned to precise angles.
Schematic:

Here is how the circuit came together:

For the lab, I used a potentiometer as an analog input to dictate the angle that was written to the servo. Here is a video of it working:
Here is my code for part 1:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ////////////// PART 1: Servo Motor Control ////////////////// | |
| #include <Servo.h> | |
| // make an instance of the servo library | |
| Servo myservo; | |
| int potpin = A0; // analog pin used to connect the potentiometer | |
| int val; // variable to read the value from the analog pin | |
| void setup() { | |
| // attach the servo motor (can attach it to any pin, PWM is happening behind the scenes | |
| myservo.attach(9); | |
| } | |
| void loop() { | |
| // read the sensor value | |
| val = analogRead(potpin); | |
| // scale it to use it with the servo (value between 0 and 180) | |
| int angle = map(val, 0, 1023, 0, 180); | |
| // sets the servo position according to the scaled value | |
| // myservo.write(angle); | |
| // this will help smooth out any servo jitters: | |
| // millis() counts milliseconds from when the program started | |
| if(millis() % 20 < 2){ // if 20 milliseconds has gone by | |
| // functionally same as delay(20) | |
| myservo.write(angle); | |
| } | |
| } |
Part 2: DC Motor Control
For the next part of the lab, we set up a simple DC motor circuit. DC motors have continuous rotation, and can switch direction of rotation based on direction of current. A simple application of a DC motor is in fans. Compared to other motors, DC motors are very hard to control because once they are connected they just continuously turn, however they are very easy to work with. In this lab we used an H-bridge as the motor driver to switch polarity (and direction of rotation using code). Other traits of DC motors are that they are cheap and have relatively low levels of torque.

Here is how my DC motor circuit came together:

When the button was pressed, the direction of the rotation changes. This functionality is made possible from the H-bridge. Here is the video:
It’s pretty hard to see it switch because it is rotating so fast, but the wings attached to the top of the motor makes it a little easier to tell.
Next, I used PWM and Arduino’s analogWrite() function to change the direction and speed of the motor. It is hard to see, but when the button is pressed, it switched direction and turns at a lower speed. Although there is current constantly flowing, sometimes there is not a high enough level for it to consistently switch directions and speed. This is because it takes a lot to get its rotation started (like starting a car).
Here is a video of the direction switch and varying speed. Again, it is hard to see that it is turning slower but the flag helps a little. Notice the inconsistencies due to insufficient current.
Here is my code for the DC motor. The commented out area from lines 25 – 34 is for the first video, when the button simply switched directions. Following the commented out section is where I programmed the button to switch directions and speed. For that, notice that I switched motor2Pin to be analogWrite() to use PWM rather than digitalWrite for both motorPins of the H-bridge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ////////////// PART 2: DC Motor Control ////////////////// | |
| const int switchPin = 2; // switch input | |
| const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A) | |
| const int motor2Pin = 5; // H-bridge leg 2 (pin 7, 2A) | |
| const int enablePin = 9; | |
| void setup(){ | |
| // set the switch as an input: | |
| pinMode(2, INPUT); | |
| // set all the other pins you're using as outputs: | |
| pinMode(9, OUTPUT); | |
| pinMode(5, OUTPUT); | |
| pinMode(3, OUTPUT); | |
| pinMode(2, INPUT); | |
| //set enablePin high so that motor can turn on: | |
| digitalWrite(9, HIGH); | |
| } | |
| void loop(){ | |
| // read the button signal | |
| int buttonSig = digitalRead(switchPin); | |
| // if the switch is HIGH, motor will turn on one direction | |
| // if(buttonSig == HIGH){ | |
| // digitalWrite(motor1Pin, HIGH); | |
| // digitalWrite(motor2Pin, LOW); | |
| // } | |
| // else, motor will turn in the other direction | |
| // else{ | |
| // digitalWrite(motor1Pin, LOW); | |
| // digitalWrite(motor2Pin, HIGH); | |
| // } | |
| // now, try to use PWM and analogWrite to change speed | |
| // switched motor2Pin to 5, because 4 was not a PWM pin | |
| if(buttonSig == HIGH){ | |
| digitalWrite(motor1Pin, HIGH); | |
| analogWrite(motor2Pin, 0); | |
| } | |
| // else, motor will turn in the other direction | |
| else{ | |
| digitalWrite(motor1Pin, LOW); | |
| analogWrite(motor2Pin, 60); | |
| } |
Part 3: Stepper Motor Control
Last, I set up a stepper motor. For this motor, I used a Sparkfun EasyDriver as the motor driver as opposed to the H-bridge. This made the wiring much easier. Stepper motors also have continuous rotation, and precise control over how much they rotate. This is because they can be set to go a certain number of steps and stop. For this particular stepper, 48 steps completed one full rotation. Unfortunately, stepper motors are expensive and require the most complicated code. Steppers are used in 3d printers, laser printers, CNC machines, and any application where a precise level of rotation is needed.
I used the Sparkfun guide for wiring the EasyDriver to both the motor and the Arduino. I also connected the button to power and the Arduino. The schematic got a little chaotic, so I used a little bit of color to differentiate.

As mentioned, the wiring is much easier to follow than that of the H-bridge. Note: in this photo, the button is connected to pin A0, but that was switched to pin 8 for the actual functionality of the motor (in the code, schematic and video).

Here is a video of the stepper working. When the button is pressed, the motor rotates one full revolution, with a delay before the next round. If the button is off, it revolves the other way. For the stepper used in this lab, 48 steps made a full revolution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ////////////// PART 3: Stepper Motor Control ////////////////// | |
| // use the stepper library | |
| #include <Stepper.h> | |
| //Stepper myStepper; | |
| // declare pin functions | |
| #define stp 2 | |
| #define dir 3 | |
| #define MS1 4 | |
| #define MS2 5 | |
| #define EN 6 | |
| void setup(){ | |
| pinMode(stp, OUTPUT); | |
| pinMode(dir, OUTPUT); | |
| pinMode(MS1, OUTPUT); | |
| pinMode(MS2, OUTPUT); | |
| pinMode(EN, OUTPUT); | |
| pinMode(8, INPUT); // button | |
| } | |
| // make it go one complete revolution at a time | |
| // incorporate button to switch directions | |
| void loop(){ | |
| int buttonSig = digitalRead(8); | |
| if(buttonSig == LOW){ // if button is off | |
| digitalWrite(dir, LOW); // LOW position moves motor "forward" | |
| for(int i = 0; i < 48; i++){ | |
| digitalWrite(stp, HIGH); | |
| delay(1); | |
| digitalWrite(stp, LOW); // set it LOW so it can be signalled again | |
| delay(1); | |
| } | |
| delay(1000); | |
| } | |
| else{ // if button is pressed | |
| digitalWrite(dir, HIGH); // HIGH position moves motor "backward" | |
| for(int i = 0; i < 48; i++){ | |
| digitalWrite(stp, HIGH); | |
| delay(1); | |
| digitalWrite(stp, LOW); // set it LOW so it can be signalled again | |
| delay(1); | |
| } | |
| delay(1000); | |
| } | |
| } |