Coursework - Arduino Motor Controller - Introduction
Introduction
In the second part of the lab, you will program an Arduino board for DC motor position control. Motor control is performed in various industrial applications for CNC machines, 3D printers, robots etc. The motor type for the applications is selected considering various factors such as the project budget, speed, torque, power and size constraints. Especially for robot design, DC motors, stepper motors and servo motors are generally used. The motor output, speed or torque can be adjusted with a gear mechanism. For example, Universal Robots company employs the harmonic drive gear mechanism with servo motors. In this case the robot can provide higher torque and the size of the robot joints can be constrained to the small sizes. , below, shows the direct drive gear mechanism integrated to servo motors of the robot designed by Universal Robot. shows the UR5 robot composed of 6 motors and having 5 kg payload.
![Direct Drive Gear Mechanism.](../images/DD-Gear-Mech.png)
![Universal Robotics UR5 Robot.](../images/UR5_Robot.png)
Coursework Description
In this lab, you will use a trimmer (potentiometer) to generate input signal for motor control, a DC motor integrated with gear mechanism and encoder, a motor controller board for controlling of motor speed, direction and position, and Arduino Nano board to program the motor operation with the trimmer. You will experience the motor control task following 5 different subtasks from A to E.
Introductory Exercises
Arduino Hardware & IDE Familiarization
Before starting to work on the tasks, familiarize yourself with the Arduino hardware and IDE.
Open the Arduino IDE software as shown in , where you will write your program.
![Arduino IDE.](../images/Arduino-IDE.png)
- The Toolbar includes buttons allowing you to upload programs, create, open, and save sketches (Programs written using Arduino Software IDE), and open the serial monitor. From left to right, the buttons perform the following functions:
- Verify: Checks your code if there is any error while compiling the code.
- Upload: Compiles the code and upload it to the Arduino board.
- New File: Creates new sketch (program).
- Open File: Open the sketch from the directory that you want.
- Save File: Saves your sketch (program).
- Serial Monitor: Can be used for debugging tool, monitoring input/output signals.
- Text Editor is for writing your code.
- Message Area gives feedback while exporting and saving the sketches, also displays errors.
- Text Console displays error messages and other information.
Now, you can test the Blink example from the Arduino.
- Connect your Arduino board to your PC.
- Select the right core and board. You can do this following the Tools > Board menu, as shown in .
![Arduino IDE - Tools > Board Menu.](../images/Arduino-IDE1.png)
![Arduino IDE - Tools > Port Menu.](../images/Arduino-IDE2.png)
- After you connected your board to a correct Port, you are ready to test the Blink example. Navigate to File > Examples > 01.Basics > Blink, as shown in .
![Arduino IDE - Loading Blink Example.](../images/Arduino-IDE3.png)
- Upload the sketch (Blink) to your Arduino using upload button. If the upload is successful, you will see the message
Done Uploading
in the Message Area. - Once the upload is complete, you will see on your board that the LED next to the letter L is blinking. You can play with the blinking speed by changing the delay time inside the delay() function. shows the position of the LED on the physical Arduino board.
![Arduino LED Position.](../images/Arduino-LED.png)
This is the end of the quick introduction of the Arduino IDE. You can now start to work on motor control tasks.
LED Example
In this activity, you will use Arduino hardware and connect components to Arduino pins. The materials that you will use are Arduino Nano board, a breadboard, a LED and a 330Ω resistor. Connect the cathode leg (-) of the LED to the ground pin (GND) of the Arduino board, and anode (+) leg to the resistor’s leg. Complete the circuit design connecting the other leg of the resistor to the one of the digital IO (Input/Output) pins of the Arduino. The IO pin will provide the DC power that the LED requires. An example of a circuit diagram appropriate for this activity is shown in . Here, pin 9 is used to power the LED.
![LED Connection to Arduino Circuit Diagram.](../images/Arduino-LED-circ-dia.png)
After you completed the connections of the components for the circuit design, you can program the Arduino to light up the LED. In the setup() function, you need to set the digital pin using the pinMode() function.
The syntax of the pinMode() function is pinMode(pin number, mode); for example: pinMode(9,OUTPUT);.
Your setup() function should appear as follows:
void setup() {
pinMode(9, OUTPUT);
}
Now, you need to set the state of the digital pin to HIGH (1) or LOW (0) with the function digitalWrite().
The syntax of the digitalWrite() function is digitalWrite(pin number, state); for example: digitalWrite(9,HIGH);.
Place this inside a loop() function so that it repeats, as follows:
void loop() {
digitalWrite(9, HIGH);
}
You can upload the code to your Arduino board and observe if the LED light changes. You can observe the status change of the light ON/OFF using the delay function. Moreover, you can monitor the status change of the LED in the Serial Monitor using the serial communication. You need to start the serial communication with the function Serial.begin(speed);. The speed is the transferred data bits per second.
Amend your setup() function as follows:
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
The Serial.println() function helps you to print data on the serial port and debug your code, as demonstrated in .
void loop() {
Serial.println("ON");
digitalWrite(9, HIGH);
delay(1000);
Serial.println("OFF");
digitalWrite(9, LOW);
delay(1000);
}
This is the end of the introductory activity. You can now start to work on the coursework tasks, explained on the next page.