arduino mechatronics manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8....

32
Arduino Mechatronics Manual Georgia Institute of Technology | ME2110: Creative Decisions and Design | 8-17-2020

Upload: others

Post on 13-Nov-2020

7 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

Arduino Mechatronics Manual

Georgia Institute of Technology | ME2110: Creative Decisions and Design | 8-17-2020

Page 2: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

1

Contents Getting Started............................................................................................................................................................ 2

Setup Steps .............................................................................................................................................................. 2

Download Arduino IDE ...................................................................................................................................... 2

Download Libraries ............................................................................................................................................. 3

Connecting the Arduino..................................................................................................................................... 6

Introduction................................................................................................................................................................. 7

The Arduino UNO ...................................................................................................................................................... 7

Why the Arduino? ................................................................................................................................................ 7

The ME2110 Arduino Interface Board ....................................................................................................... 7

Writing an Arduino Sketch ............................................................................................................................... 8

Arduino Programming Basics ......................................................................................................................... 9

Using the ME2110 Library .............................................................................................................................12

Callable Functions and Variables ................................................................................................................14

Electromechanical Components .......................................................................................................................16

Digital vs Analog Signals .................................................................................................................................16

Sensors ....................................................................................................................................................................16

Actuators ................................................................................................................................................................20

Troubleshooting ......................................................................................................................................................25

myDuino Example Sketch ....................................................................................................................................26

Actuator Test Code .................................................................................................................................................29

Reference Images ....................................................................................................................................................30

Page 3: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

2

Getting Started This section will give quick references on how to get started with the Arduino Uno. However, to understand the Arduino Uno and how it is being used in ME2110, you must read through the rest of the manual. Have your Arduino board and USB cable near your computer. Do not plug them in yet.

Setup Steps • Download Arduino software (IDE) • Download necessary libraries • Connect your Arduino to your computer

Download Arduino IDE The Arduino IDE (Integrated Development Environment) allows you to write programs and upload them to your Arduino board. Arduino also offers a web editor service called Arduino Create (https://www.arduino.cc). You can use the web editor if you run into issues with the IDE, but for this course, we will be using the IDE. Download the latest version of the IDE from arduino.cc/download. Do not download the Arduino app from the app store.

Windows Installation 1. Download the Windows Installer. Do not perform a non-admin install or download

the Windows app. 2. After finishing the download, navigate to your download path and double-click on

“arduino-1.x.x-rx-windows.exe” (where the x depends on the version you downloaded). If a security warning window shows up, click on “Run” or “Allow” and accept the License Agreement. Click on “next” to choose the folder to install the IDE and click on “Install”.

3. Connect the Arduino board to the computer using a proper USB cable. The board will automatically draw power from the USB connection of the computer and the green LED (labeled ON) will turn on.

4. Windows should initiate its driver installation process when the board is plugged in. In some cases your computer won’t be able to find the drivers by itself. If so, you need to point it to the proper folder.

• Windows XP: If Windows Update asks about the path for the software, select “Yes, for this time only” and the “Install from a list or specific location”

• Other: A popup window asks you to install the driver automatically or to look for it in the computer, choose to look for the driver on your computer.

5. If the installation doesn’t start automatically, click on the Start Menu and open the device manager.

6. Look for the Arduino device under the category “Other Devices” or “Unknown Devices” and select “Update Driver” or “Update Driver Software” clicking with the right button of the mouse.

7. Click on “browse” and select the “Drivers” folder (not the folder “FTDI USB Drivers”) in the Arduino software IDE folder. Press “OK” and “Next”. If a dialog box about a test

Page 4: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

3

on the Windows Logo shows up, click on “Continue Anyway”. Windows will now install the driver.

8. In the “Device Manager”, under “Ports (COM & LPT)”, you should see a port similar to “Arduino Uno (COM4)”.

Mac Installation 1. If you are using 10.8 (Mountain Lion) or later, go to System Preferences and open

the “Security & Privacy” panel. In the “General” tab, under the heading “Allow applications downloaded from”, click the toggle for “Anywhere”.

2. Once the Arduino software IDE has finished downloading, double-click on the .zip file to expand the application

3. Copy the Arduino application to the Applications folder, or anywhere else you wish to install the software.

4. Connect your Arduino board to the computer with the USB cable. The board will be automatically powered from the USB connection and the green LED (labeled ON) will turn on.

5. You do not need to install any drivers to work with the board. 6. Depending on the version of the OS X that you are running, you might get a dialog

box asking if you wish to open the “System Preferences”. Click the “Network Preferences” button and click “Apply”.

7. The board will show up as “Not Configured”, but it is already working. You can quit System Preferences.

Download Libraries Download the “myDuino” libraries as a ZIP folder from GitHub. Login using your GaTech credentials if you are prompted.

https://github.gatech.edu/TeamEPICS/ME2110_Arduino

Page 5: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

4

Figure 1. Download the ZIP file from GitHub

Extract the files in the ZIP folder. In the folder, there is another folder called myDuino, which is the library folder you need to add. Copy and paste this folder into the “libraries” folder inside the “Arduino” folder of your PC. If you cannot find the “Arduino” folder in the “Program Files x86” folder, you may have installed the Arduino IDE incorrectly.

Sample file path: Local Disk (C:) > Program Files (x86) > Arduino > Libraries

This folder is located in a similar location on a Mac. Search your “Library” folder within Finder until you find the folder titled “Arduino.” Open this folder and paste the myDuino folder into the “Libraries” folder inside it.

If the Arduino IDE is already open, you need to close and reopen it for the changes to take effect. In the toolbar, check Sketch > Include Library for the greyed-out heading “Contributed libraries” to make sure myDuino shows up under it.

Page 6: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

5

Figure 2. Checking to ensure that the library has been uploaded correctly

You must include the library in your code when you want to use it. When writing a new Arduino sketch, ensure that your code has #include <myDuino.h> at the top.

Figure 3. The include statement at the start of the code

Page 7: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

6

Connecting the Arduino

Ensure that the Arduino board is connected to your laptop using the USB cable. If the Arduino board is connected to your laptop via USB, power is being supplied to it through the USB cable, so you do not need to plug in the power supply directly to the Arduino. You will still need to plug in power to the interface board in order to power the actuators. However, if you want to upload the code and then use the Arduino away from your laptop, you must supply power directly to the Arduino, in addition to the power to the interface board.

Communicating with the Arduino After the Arduino software IDE has been installed, you can now upload a program.

1. Open the Arduino IDE and appropriate program you would like to upload to the microcontroller.

2. Ensure that the correct Board Type (“Arduino/Genuino Uno”) has been selected under “Tools”.

3. Ensure that the correct Communication Port has been selected under Tools > Port. It will most likely only provide one option. Whenever you disconnect the Arduino from your laptop, you will have to redo this step.

4. Use the button to verify the code does not have syntax or build errors. This can be done without having the Arduino connected to your laptop.

5. Use the button to upload the code to the Arduino. This will also verify the code, so Step 4 is optional.

Figure 4. Selecting the correct Board and Port

Page 8: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

7

Introduction The rest of this manual contains all the information needed to be successful in using the Arduino Uno in ME2110: Creative Decisions and Design. It contains the steps required to get the necessary programs to get the Uno set up and running, a quick start to programming in the Arduino environment with examples, background information on the sensors and actuators (electromechanical components), and a wealth of other useful details.

The Arduino UNO

Figure 5. Simplified layout of the Arduino UNO microcontroller

Why the Arduino? The Arduino is one of the simplest options to introduce students to programming and electromechanical systems. It uses a variant of C++ that is similar to Java and, to an extent, Python. Students who have had a course using MATLAB will have some familiarity with C++ functionality. Arduino also has an incredibly large support network – if you run into a coding or setup issue, there is likely an answer available online.

The aim of using Arduino and the pre-coded libraries to minimize the coding burden as much as possible. However, we encourage you to continue exploring code, controls, and electronics as the lines between mechanical engineering, electrical engineering, and computer science blur even further.

The ME2110 Arduino Interface Board A custom circuit board has been designed and developed for this course. It connects directly to the Arduino’s header pins and uses two- or three-piece prong connectors to attach to sensors and actuators. The table and image below describe the connection points.

It is important to note that the sensors (IR sensor, potentiometer, and encoder) require very specific wire connections. Failure to connect the correct wires to the corresponding terminals can result in poor readings or damage to components. See

Page 9: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

8

the Electromechanical Components Section of the manual to learn more about these connections.

Digital Inputs (DI# on case): Connect to buttons, switches, banana plugs

Sensors (labelled): Connect to encoder, IR sensor, potentiometer

Reset: Resets the program to run from setup()

LEDs (Colors vary): Programmable indicators

Motor Pins (M1 & M2 on case): Connect to motors

Digital Outputs (DO# on case): Connect to valves and solenoids

Figure 6. Layout of connectors on interface board

Writing an Arduino Sketch Programs in Arduino are called “sketches.” This is where the microcontroller will read sensors and control actuators. The ME2110 library downloaded from GitHub handles most of the coding required, reducing the overall complexity of the coding portion. An in-depth discussion of the functions within the library is included later in this manual.

Each time you initialize the Arduino IDE, a new blank sketch is opened for you. Every sketch in Arduino has two core functions prepopulated:

Page 10: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

9

1. A setup function a. Anything you want to set up (variables, initial variable states, etc.) will be

done here and run only once. 2. A loop function

a. Once the setup function has run, the microcontroller will run this loop repeatedly. Generally, the bulk of your code will reside here.

Figure 7. Blank Arduino Sketch

Arduino Programming Basics As mentioned previously, Arduino uses a variant of C++ to run its programs. This manual will not go in-depth on programming C++ but for those who are interested, there are numerous tutorials and guides found online. For programming help, Arduino has a language reference resource on their website at https://www.arduino.cc/reference/en/. The following is a brief outline of core structures and background information to set up your Arduino.

Syntax Learning to write in C++ is somewhat similar to MATLAB. Key things to note are to remember to declare any variables and their type (int, char, etc.) before using them, use opening and closing curly braces () for functions such as if statements, and include semicolons at the end of function calls. In MATLAB, you can leave the semicolon off of a line of code and the code will still run, but in C++, forgetting a semicolon will trigger a syntax error.

Variables Although variables can be set up in the setup function, this is not recommended for this course because it creates a local variable that only exists within the setup function. It is common to set variables up before the setup function. This makes them global variables that can be referenced within any loop or function in the code.

Page 11: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

10

When you create a new variable in MATLAB, you can just assign a value to the variable name and be ready to go (for example, x = 1). Because MATLAB uses your computer’s processor to run calculations, it is not concerned with conserving memory so it automatically stores every variable as a 32-bit float. The Arduino board is much less powerful than your laptop, so we need to conserve resources by using only as much memory as we need. When you declare a new variable in C++, you need to specify its type. Table 1 shows the different variable types that can be used. The table is arranged from least memory used at the top to most memory used at the bottom. When choosing a variable type to use, you should choose the least memory-intensive variable that still has enough range to accommodate the values you will be storing.

Using the example before, if we want to create an integer variable x with an assigned value of 1, we would use the line of code int x = 1. You can also just use int x to define the variable and assign the value later with x = 1.

Table 1. Variable Types Arduino Data Type Precision & Interpretation Range boolean (bool) 8-bit true or false, HIGH or LOW, 1 or 0

unsigned char (or byte)

8-bit (unsigned) 0 to 255

char (recommended for characters)

8-bit (signed) -128 to 127

short 16-bit (signed) -32768 to 32768

unsigned int 32-bit (unsigned) 0 to 4294967295

int 32-bit (signed) -2147483648 to 2147483647

unsigned long 32-bit (unsigned) 0 to 4294967295

long 32-bit (signed) -2147483648 to 2147483647

float 32-bit float +/-10-38 to +/-1038 (with decimal points)

double 64-bit float +/-10-305 to +/-10305 (with decimal points)

Page 12: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

11

If-Else Conditions An if statement is one of the basic logic building blocks - it is one of the ways you can use Arduino to make decisions. The basic layout of an if statement in English can be viewed as:

If this condition has been met, then do task 1. If this other condition has been met, do task 2. Otherwise do task 3.

Programmatically it can be represented in pseudocode as:

if (condition == True) do task 1; else if (other condition == True) do task 2; else do task 3;

Note that the double equal sign must be used in the condition to compare the values. Using a single equal sign is reserved for assigning variables. More details can be found in the link below. https://www.arduino.cc/reference/en/language/structure/control-structure/if/

Loops Loops are an implementation of “if-else” statements. The code inside a loop will be run continuously until a number of iterations have passed (for loop) or until a condition is no longer true (while loop). The loop function that almost every Arduino program uses is an example of an infinite while loop. An example of a for loop could be:

Do task 1 until the 5 iterations have passed Programmatically:

for (int i = 0; i <= 5; i++) do task 1;

You can use Boolean operators in the logic conditions of your loops or if statements. In C++, these are ! (logical not), && (logical and), and || (logical or).

Learn more about for loops and while loops in the links below. https://www.arduino.cc/reference/en/language/structure/control-structure/for/ https://www.arduino.cc/reference/en/language/structure/control-structure/while/

Page 13: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

12

Using the ME2110 Library The ME2110 Library simplifies much of the coding required for this class. Many of the sensors and actuators can be controlled from a simple function call. In order to access these functions, you will need to ensure that each program you write has an #include statement at the very top of the file, as mentioned earlier. This ensures the proper libraries are referenced when a function is called.

The ME 2110 library requires an instance of its code in order to access its functionality. One analogy is that a library can be considered as a pool of knowledge on cooking, and an instance of that library is a cookbook. The cookbook allows you to access the knowledge contained in the library, such as ingredients or cooking steps in the form of recipes.

As mentioned in the “Download Libraries” section, ensure that all new sketches have the following:

#include <myDuino.h> myDuino robot(1); void encHandler() robot.doEncoder(); void setup() attachInterrupt(digitalPinToInterrupt(2), encHandler, CHANGE); attachInterrupt(digitalPinToInterrupt(3), encHandler, CHANGE); Serial.begin(9600); void loop() //main code here

If you are copying and pasting code as text from a different program into the Arduino IDE, this may cause an error depending on the way the text is formatting. It is best to retype the code in Arduino while using the original source as a reference rather than copying and pasting code from the internet or a Word document.

The first item in the sketch needs to be #include <myDuino.h> to be able to use the library functions. Consider this to be “fetching the cookbook containing the recipes to make a robot.” This will be called when you create an instance of the library. myDuino robot(1); is an instance of the library. robot can be changed to any name that makes sense. Any new instance will need to have a new name. For the purposes of this class, you only need a single instance per sketch. The (1) is required but is an unused component that is required to make the code work.

Page 14: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

13

To ensure the encoder is read correctly, you must include a wrapper function. This is done with void encHandler() and its contents. A special function called an interrupt is used to read the encoders. attachInterrupt(digitalPinToInterrupt(X), encHandler, CHANGE); is added to the void setup() function to use it whenever the encoder is turned. Note that there is one interrupt per channel of the encoder. To learn more about interrupts, see https://en.wikipedia.org/wiki/Interrupt and to learn more about encoders, see the Electromechanical Components section of this manual. The last inclusion is optional but useful for debugging your sketch as you work. Serial.begin(9600); opens a communication port between the Arduino and your computer. You can print sensor data and other useful information to this monitor. The serial monitor is analogous to the command window printout in MATLAB, where you would use fprintf to display text or variables in the window.

Figure 8. Opening the serial monitor

Page 15: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

14

Callable Functions and Variables We will assume that your instance of the ME2110 Library is called “robot” for the examples below. Use these functions to interact with the electromechanical components of the mechatronics kit. A sample program with the functions is provided in the Example Programs section of this manual.

The basic coding setup for using this library will be some form of:

instance.functionName(args); where

• instance is the instance of the ME2110 Library, robot will be used in this manual • functionName is one of the functions listed below • args are input arguments, if required

Some functions return values, such as the readButton function returning the state of the button. If a function returns a value, you can store it in a variable so you can access it later.

robot.readButton(int pin); • Input arguments: 1, 2, 3, or 4 • Returns: 0 or 1

o 0 = button is pressed; 1 = button is released; may vary depending on whether switch has been wired normally open or normally closed

• Purpose: Retrieves current value of a button

robot.readIR(); • Input arguments: None • Returns: integer value between 0-1023

o 0= very far away, 1023 = very close • Purpose: retrieves IR sensor readings

robot.readPOT(); • Input arguments: None • Returns: integer value between 0-1023

o 0 = minimum voltage; 1023 = maximum voltage • Purpose: Retrieves potentiometer output

robot.doEncoder(); • Input arguments: None • Returns: None • Purpose: Wrapper function for encoder functionality. Do not use in loop() code.

robot.encoderCount();

Page 16: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

15

• Input arguments: None • Returns: Signed integer • Purpose: Retrieves current count of encoder

robot.encoderDir(); • Input arguments: None • Returns: 1 (Clockwise) or -1 (Anticlockwise) • Purpose: Indicates which direction the encoder is being turned

robot.LED(int pin, int onOFF); • Input arguments:

o pin: 1 or 2 o onOFF: 0 (off) or 1 (on)

• Returns: None • Purpose: Turns a specified LED on or off

robot.digital(int pin, int onOFF); • Input arguments:

o pin: 1, 2, 3, or 4 o onOFF: 0 (off) or 1 (on)

• Returns: None • Purpose: Turns a specified digital output (solenoids or pneumatic valves) on or off

robot.moveMotor(int mot, int dir, int spd); • Input arguments:

o mot: 1 or 2 (which motor) o dir: 1 (forward) or 2 (backward), depends on how the motor is wired o spd: 0 (stopped) to 255 (full speed)

• Returns: None • Purpose: Turns a specific motor one direction at a specified speed

Miscellaneous millis()

• Returns: number of milliseconds since program started running

Serial.println(String(var) + “Text”); • Purpose: prints the information in parentheses in the serial monitor • Use String(var) to print the value of a variable, where var is the name of the variable • You can print any text by putting it in quotation marks • Print a combination of outputs by putting a + between them

Page 17: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

16

Electromechanical Components This section will describe the various sensors and actuators included in the mechatronics kit. The sensors used in ME2110 include switches, an infrared distance sensor, a potentiometer and an encoder. The actuators include DC motors and DC solenoids.

Digital vs Analog Signals The mechatronics kit contains digital inputs and outputs as well as analog sensors (inputs). Signals are time-varying quantities that are used to convey information and can be classified as analog or digital. For engineers, the quantity being measured is often a voltage. Analog signals are continuous signals that vary along a range of voltages. Digital signals, on the other hand, are discrete and can only vary between a finite set of values, most commonly between a HIGH and LOW threshold voltage.

Sensors Switches The switches included with this kit are digital inputs; that is, they return a HIGH or LOW value to the user. Switches are a basic electrical component used to control the current flow through a circuit by “opening” or “closing” the circuit. Mechanical switches, like the ones in the mechatronics kits, are comprised of two conductive terminals connected to an external circuit that allows for current flow when they are touched. When the contacts are separated, an open circuit is created and there is no current flow.

Figure 9. Typical switch in open and closed states

There are two distinct categories of switches: momentary or maintained. Momentary switches only maintain their state (OPEN or CLOSED) if they are actuated. The long-arm and roller switches in the mechatronics kit are examples of momentary switches. Maintained switches, as their name suggests, maintains their last state until activated into a new one. The pushbutton switch (can also be referred to as a toggle switch) is an example.

Figure 10. Pushbutton switch (left) and long-arm switch (right)

Page 18: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

17

Switches often have more than two terminals and are characterized by the number of “poles” and “throws” available. The number of poles on a switch define how many separate circuits it can control while the number of throws define how many positions the poles can switch to. For example, the pushbutton switch is a single-pole, single-throw (SPST) because it only has a single input and output. This is indicated by the two wires coming from the switch.

The long-arm and roller switches are single-pole, double-throw (SPDT) switches. There are three pins on a SPDT switch, common (COM), normally closed (NC), and normally open (NO). There are only two wires coming from these switches. One wire is always soldered to the COM terminal, while the other is soldered to either the NC or NO terminal. The names of the NO and NC terminals describe their default state. The way your switch is wired determines whether the switch will return a 0 (if normally open) or a 1 (if normally closed) when the switch is not being pressed. If you are not sure which way your switch is wired, you should test it before you attempt to use it to make logic decisions.

Figure 11. Diagram of a single-pole, double-throw (SPDT) switch

For the ME2110, switches are typically used as SPST digital inputs (HIGH or LOW). When the switch is actuated and the circuit is closed, the microcontroller will read a HIGH and vice versa. This is known as positive logic. If the switches were wired for negative logic, the microcontroller would read a LOW when the circuit was closed. To properly connect the long-arm and roller switches, solder leads onto the COM and NO terminal pins while leaving the NC terminal unattached.

IR Distance Sensor The infrared (IR) distance sensor is capable of detecting objects with a range of 4 to 30 cm. The sensor is comprised of an infrared LED and a detector. A light beam is emitted by the LED. The reflection of this beam is received by the detector. The angle at which the light is received determines the distance that the object is from the IR sensor. More information on this sensor can be found in its data sheet.

Page 19: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

18

Figure 12. The IR distance sensor

The output of the IR sensor is an analog voltage that typically ranges between 0 and 3.3V. The Arduino IR function returns an integer value. Calibration must be performed to determine the relationship between this value and the distance from the sensor. The data from the sensor will contain noise that must be filtered if very accurate results are needed.

The IR sensor must be wired in a particular way, as shown in Figure 13. Place the IR sensor and the Arduino case flat on the table in front of you. The side of the IR sensor with the white plug should be facing the Arduino case. Secure the wires in the three-prong connector such that none of the wires cross each other (i.e., all of the wires go straight across).

Figure 13. Wiring of the IR sensor relative to the board

Arduino Case

Page 20: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

19

Potentiometer A rotary potentiometer is a resistive device that can be used as a variable resistor or a voltage divider. In ME2110, it will primarily function as a way to measure an angle of rotation.

Figure 14. 5kΩ rotary potentiometer

In a rotary potentiometer, the resistive material is shaped in an arc. Turning the knob moves a conductive wiper that changes the effective length of the resistor, and therefore changes the resistance. The resistance of a potentiometer varies between ~0 and its rated resistance. The potentiometers in the mechatronics kits are 5kΩ and have a 300° rotational range. The Arduino board measures the voltage drop across the potentiometer and returns an integer value between 0 and 1023. Calibration must be performed to determine the relationship between this value and the angle measure of the potentiometer. The potentiometer should be wired to the Arduino board in the same “straight-across” configuration as the IR sensor.

Encoder A rotary encoder is a device that measures the angular movement of a shaft or axle. The encoder in the mechatronics kit is a mechanical quadrature encoder. Unlike the potentiometer, the encoder can rotate freely and continuously.

Figure 15. Rotary encoder

The rotational motion of the shaft of the encoder is converted to electrical pulses, or counts, that are interpreted by the microcontroller. The Arduino can tell the user which direction the encoder is rotating, as well as how many “counts” it has rotated since the program started. You will need to count how many counts are in one full rotation of the encoder, since it can vary. The encoder should be wired to the Arduino board in the same “straight-across” configuration as the IR sensor and potentiometer.

Page 21: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

20

Actuators DC Motors Direct current (DC) motors are one of the most commonly used electromechanical actuators and can be subdivided into brushed and brushless motors. The provided DC motors are brushed motors. The DC motor works by inducing an electromagnetic field using coils of wire around an armature. Around the inside of the casing are permanent magnets that attract or repel the wire coils, and as the shaft turns, the brushes reverse the polarity of the armature, causing the shafts to rotate. The direction that a DC motor rotates is controlled by the polarity of the applied voltage through its two leads. Simply swap the leads, and the motor will spin in the opposite direction.

Figure 16. Large DC motor (left) and small DC motor (right)

Variable voltages are needed to control the speed of a DC motor, but most microcontrollers can only supply a digital output (where LOW is 0V and HIGH is a fixed voltage, often around 5V). Pulse width modulation (PWM) is a very common method of generating a variable power signal to control the speed of a DC motor. By switching a digital output HIGH and LOW rapidly, the PWM signal creates an average voltage that is applied to the motor. A PWM signal is defined by 3 quantities; the pulse width, period and voltage.

Page 22: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

21

The pulse width refers to the length of time that the signal is HIGH during a single period. The ratio of pulse width to period is known as the duty cycle. Multiplying the duty cycle by the voltage level of the digital HIGH returns the average voltage output. For example, a PWM signal with a duty cycle of 50% on a 12V digital output will create an average 6V supply. Using PWM allows engineers to control the speed of a DC motor without the need of a variable analog voltage supply. In the ME2110 driver library, the motor speed is input as a value between 0 and 255, not as the duty cycle value. Additionally, be aware that at low speeds, the motors have a deadband, or a range of values to which the system does not respond.

Solenoids A solenoid is essentially a coil of wire that produces a magnetic field when an electrical current is passed through it, as shown in Figure 19 (remember the right-hand rule). Solenoids can be either “push”, “pull”, or both. Push solenoids repel ferromagnetic materials (the plunger) while a pull solenoid attracts them. The solenoids in the mechatronics kits are pull solenoids. Solenoids are typically used as switches or valves and operate on binary ON or OFF states. Further information on the power and resistance of the solenoids can be found in the data sheet.

Figure 18. Small and large solenoids

Figure 17. PWM signals with varying duty cycles

Page 23: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

22

Figure 19. Magnetic field produced by a coil (coil is inside the metal housings seen above)

Pneumatics The pneumatics system in ME2110 include a pneumatic tank, 2 pneumatic actuators, 2 solenoid valves, a needle valve, a T-connector, and 4 and 6 mm tubing.

Figure 20. Various pneumatic components

Page 24: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

23

Figure 21. Pneumatic valves

Connect the pneumatic system as shown in Figure 22. Bicycle pumps are available to provide the necessary pressure for the system, up to a maximum pressure of 100 psi. The system will not operate properly if the pressure drops below 25 psi. The pump connects to the tank through a piece of 4mm diameter tubing attached to the one-way coupler on the back of the pressure gauge. After filling the tank, the 4mm tubing should be removed from the tank prior to disconnecting the bicycle pump. This prevents the air from escaping the tank.

Figure 22. Connection of the pneumatic system

The pneumatic valves control the airflow of the system by switching between open and closed states. In the closed state, ports 1 and 2 are disconnected. When the controller applies a voltage to the valve, the solenoid valve opens, and air is allowed to flow from the tank, through ports 1 and 2 and into the pneumatic actuator. The actuator extends and remains so until the valve is closed again. A spring in the actuator returns it to its initial position. The excess air in the system escapes through a third port in the valve that remains open. The t-connector can be used to connect a second actuator by splitting the airflow from the valve. The needle valve is included with the kit and allows the user to manually control airflow. The

Page 25: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

24

threaded top knob can be twisted to adjust the amount of airflow through the valve. You can think of the pneumatic valves in Figure 21 as an on/off light switch (because they either let no air or complete airflow through), while the needle valve is a dimmer switch (because varying amounts of air can be let through the valve).

Page 26: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

25

Troubleshooting One major advantage of text-based programming is that it is easy to identify your errors. If your code has an error, read the error message to see if you can identify what it is. There is plenty of documentation online if you need to Google the error message to see how other people have fixed it. Below are some persistent errors from previous semesters of ME2110.

• [Component] is not functioning as expected. o Check the wiring connection. Make sure that none of the wires are broken, and

that no exposed wire is in contact with another. Check the metal gates of the green connector to make sure that they have good contact with the wires (and that the wires are stripped back far enough to make good contact).

o Test the component with the actuator troubleshooting code (if the issue is with an actuator) or with the serial monitor in the myDuino Test code. If the component works now, then there is a problem with your code.

o If the component still does not work, get either an LED indicator (if the problematic component is an actuator) or a working component from the TAs. If the new component or test piece still does not function correctly with the test code, there may be a problem with your board.

• The code verifies but will not upload. o Check to make sure that the correct Port is selected in the Tools menu.

• The values for the encoder/potentiometer/etc are unexpected. o Make sure you are using the right variable type to store each value – for

example, the encoder direction is either negative or positive, so you must choose a signed variable to store the value in.

• The motor does not spin at all, or spins very slowly. o Ensure that the power supply is connected to the interface board.

• Conditional loops are not responding as expected. o Check the wiring of your switches. You may need to reverse the logic in the

code, or physically change the wiring of the terminal. o If the problem persists, use the serial monitor to print variable values to check

them at different points in the code. • There is an error related to the library instance.

o Make sure you have created a new sketch, not a new tab within the same sketch, otherwise the two instances of the library will cause an error.

• The Arduino IDE will not open. o Delete the Arduino15 folder located in:

C:\Users\YourUsername\AppData\Local on Windows or /Users/YourUsername/Library on Mac. *Note that this is a hidden folder, so you need to type the address into your file explorer instead of navigating to it.

o A workaround is to use the Arduino Web Editor mentioned earlier in this manual.

Page 27: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

26

myDuino Example Sketch #include <myDuino.h> //Declare and Initialize library Instance myDuino robot(1); //Declare and Initialize Variables int but1 = 0; int but2 = 0; int but3 = 0; int but4 = 0; int encCount = 0; int encDir = 0; unsigned long IRVal = 0; unsigned long potVal = 0; unsigned long m1spd = 0; unsigned long m2spd = 0; unsigned long initTime = 0; unsigned long stopTime = 0; int runOnce = 0; int runCode = 0; //Create Encoder Handler void encHandler() robot.doEncoder(); //Setup code void setup() //Attach Encoder Handler to Encoder Channels attachInterrupt(digitalPinToInterrupt(2), encHandler, CHANGE); attachInterrupt(digitalPinToInterrupt(3), encHandler, CHANGE); //Initialize Serial Monitor Serial.begin(9600); //Main Loop void loop() //Wait for start trigger but1 = robot.readButton(1); if (but1 == 0) runCode = 1; runOnce += 1; // Set start time and end time if (runOnce == 1) initTime = millis(); stopTime = initTime + 20000;

Page 28: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

27

//Keep checking for start trigger else but1 = robot.readButton(1); // Indicate that robot is waiting for start signal robot.LED(1,1); //Triggered, start main block if (runCode == 1 && (millis() < stopTime)) // Indicate that main block is running robot.LED(2,1); robot.LED(1,0); // Poll buttons but1 = robot.readButton(1); but2 = robot.readButton(2); but3 = robot.readButton(3); but4 = robot.readButton(4); // Read sensors IRVal = robot.readIR(); potVal = robot.readPOT(); encCount = robot.encoderCount(); encDir = robot.encoderDir(); // Move motors, m1 controlled by IR m2 controlled by potentiometer m1spd = IRVal*255/1024; m2spd = potVal*255/1024; robot.moveMotor(1,2,m1spd); robot.moveMotor(2,1,m2spd); // Example logic, connect actuators to buttons if (but2 == 0) robot.digital(1,1); robot.digital(2,1); robot.digital(3,1); robot.digital(4,1); if (but3 == 0) robot.digital(1,1); robot.digital(2,0); robot.digital(3,1); robot.digital(4,0); if (but4 == 0) robot.digital(1,0); robot.digital(2,0); robot.digital(3,0); robot.digital(4,0);

Page 29: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

28

// Print relevant information Serial.println(String(millis()) + " Init: " + String(initTime) + " Stop: " + String(stopTime) + " Count: " + String(encCount) + " Dir: " + String(encDir) + " B1: " + String(but1) + " B2: " + String(but2) + " B3: " + String(but3) + " B4: " + String(but4)); + " Pot: " + String(potVal) + " IR: " + String(IRVal) + " m1: " + String(m1spd) + " m2: " + String(m2spd) // Kill everything after time has run out else robot.moveMotor(1,1,0); robot.moveMotor(2,1,0); robot.digital(1,0); robot.digital(2,0); robot.digital(3,0); robot.digital(4,0); robot.LED(2,0); robot.LED(1,0); runCode = 0; runOnce = 0;

Page 30: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

29

Actuator Test Code #include <myDuino.h> myDuino robot(1); int count = 1; void setup() // put your setup code here, to run once: void loop() // put your main code here, to run repeatedly: // turns the outputs (solenoids, valves, motors) on and off in a repeating 1-second cycle robot.digital(1,0); robot.digital(2,0); robot.digital(3,0); robot.digital(4,0); robot.moveMotor(1,1,0); robot.moveMotor(2,1,0); delay(1000); robot.digital(1,1); robot.digital(2,1); robot.digital(3,1); robot.digital(4,1); robot.moveMotor(1,count,255); robot.moveMotor(2,count,255); delay(1000); if (count == 1)count = 2; // switches motor speed on each loop to test both directions else count = 1;

Page 31: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

30

Reference Images

Figure 23. Arduino UNO Board

Page 32: Arduino Mechatronics Manual2110.me.gatech.edu/sites/default/files/images/arduino... · 2020. 8. 17. · Getting Started This section will give quick references on how to get started

31

Figure 24. Schematic for ME2110 Arduino interface board