Escolar Documentos
Profissional Documentos
Cultura Documentos
UNIT OBJECTIVE
When you have completed this unit, you will be able to demonstrate the operation of
Arduino IDE and MTS-100 Tutor for Arduino.
DISCUSSION
The MTS-100 Tutor for Arduino has been designed to provide the student with small
microcontroller-controlled systems in modular form. These I/O modules can be
patched together and connected to Arduino Uno to create various microcontroller
control systems.
Arduino Uno board is the core of MTS-100 Tutor for Arduino. It is the first in a series
of USB Arduino boards, and the reference model for the Arduino platform.
The MTS-100 Tutor for Arduino provides 25 modules plus a half-size breadboard.
The universal breadboard offers a convenient way to students for constructing the
circuits that are not provided by the MTS-100.
EQUIPMENT REQUIRED
1. Personal Computer
2. MTS-100 Tutor for Arduino
3. MTS-100 CD
4. USB Cable
5. Jumper Wires
6. AC Power Cord
1-1
Exercise 1-1 Software Installation
EXERCISE OBJECTIVE
DISCUSSION
Arduino is an open development platform. You can download from Arduino website
(Arduino.cc) and install it on your computer.
Fig. 1-1 shows the Arduino IDE window. The Arduino Integrated Development
Environment - or Arduino Software (IDE) - contains a text editor for writing sketches,
a message area, a text console, a toolbar with buttons for common functions and a
series of menus. It connects to the Arduino and Genuino hardware to upload
programs and communicate with them.
1-2
Menu Bar
Toolbar
The toolbar buttons allow you to verify and upload programs, create, open, and save
sketches, and open the serial monitor.
1-3
Text Editor
Programs written using Arduino are called sketches. These sketches are written in
the text editor. Sketches are saved with the file extension .ino. It has features for
cutting/pasting and for searching/replacing text. Arduino text editor is similar to
general-purpose word processors.
Once you outgrow a single file and wish to break the project up into multiple files,
you can do so in the Arduino IDE with multiple tabs. Each tab represents a single file
stored in the same directory as the sketch file (.ino). If using File > New command,
you’ll open another Arduino window for editing another program. If you want to write
another sketch in the same window, click the button in the editor and select New
Tab from the dropdown menu, a New filename entry dialog will appear in the
console, as shown in Fig. 1-2.
Fig. 1-2 New filename entry dialog (New Tab and Rename)
If you enter a file name in the Name for new file: box and then click
button, a new tab with a filename you entered will open. If you want to change a
filename, click button and select Rename from the dropdown menu, a New
filename entry dialog will appear in the console (see Fig. 1-2). Enter a filename in the
Name for new file: box, and then click button.
If you wish to show the line numbers in the editor, open the Preferences dialog using
the File > Preferences command, as shown in Fig. 1-3. Place a checkmark in the
Display line numbers checkbox, and then click button.
1-4
Fig. 1-3 References dialog
Text Console
The text console below the editor is used to display text output by the Arduino
environment including complete error messages and other information.
Message Area
The message area gives feedback while saving and exporting and also displays
errors. When uploading the sketch to the Arduino board, the Arduino Software (IDE)
will display a message when the upload is complete, or show an error.
Before uploading your sketch, you need to select the correct items from the Tools >
Board and Tools > Port menus. Once you've selected the correct serial port and
board, press the upload button in the toolbar or select the Upload item from the File
menu. On most boards, you'll see the RX and TX LEDs blink as the sketch is
uploaded.
In addition, the lower right corner of the Arduino IDE window displays the current
board and serial port.
1-5
Shortcuts
Keyboard shortcuts allow you to execute Arduino IDE commands. Table 1-1
presents the Arduino IDE menu commands and keyboard shortcuts.
1-6
Menu Command Shortcut Description
Select and highlight the whole
Edit > Select All +
content of the editor.
Open the Find and Replace window
where you can specify text to search
Edit > Find... +
inside the current sketch according to
several options.
Highlight the next occurrence - if any
- of the string specified as the search
Edit > Find Next +
item in the Find window, relative to
the cursor position.
Edit > Use Select text to search inside the
+
Selection For Find current sketch
Check your sketch for errors
Sketch>Verify/ compiling it; it will report memory
+
Compile usage for code and variables in the
message area.
Open Serial Monitor window. Open
the serial monitor window and
initiates the exchange of data with
Tools > Serial
+ + any connected board on the currently
Monitor
selected Port. This usually resets the
board, if the board supports Reset
over serial port opening.
PROCEDURE
On your MTS-100 CD, there are two files in the Software folder:
1. arduino-1.6.1-windows.exe file (Windows installer) and
2. arduino-1.6.1-windows.zip file (Windows ZIP file for non admin install)
1-7
Fig. 1-4 License Agreement dialog
1-8
4. Click button. The Installation Folder dialog opens as shown in Fig.
1-6.
1-9
6. Once the installation is completed, the Completed window opens as shown in Fig.
1-8. Click button to quit the Setup and close the window.
8. To launch the Arduino IDE, click Start > Arduino or double-click the
on the desktop, the Arduino IDE window opens as shown in Fig. 1-1.
1-10
Exercise 1-2 Hardware Installation
EXERCISE OBJECTIVE
DISCUSSION
The MTS-100 Tutor for Arduino (see Fig. 1-9) has been designed to provide the
student with small microcontroller-controlled systems in modular form. These I/O
modules can be patched together and connected to Arduino Uno to create various
microcontroller control systems, such as connecting the DIP switch and LED Bar to
the digital pins of Arduino Uno to form a basic I/O control system. This design
eliminates, as much as possible, the breadboarding of components and saves time
for the actual study of the circuit theory and Arduino programming.
1-11
The MTS-100 Tutor for Arduino is housed in a sloped front enclosure that allows for
ease of operation. The MTS-100 is connected to the computer using a USB cable.
Experiments performed on the MTS-100 usually consist of patching together, with
wire jumpers, signals from one module to another. Each module is separated
visually by the use of bordered area and labels. Listed below are the modules of the
MTS-100 Tutor for Arduino.
1. ARDUINO UNO
2. POWER
3. 4x4 KEYPAD
4. DIP SWITCH
5. CDS & MICROPHONE
6. JOYSTICK
7. ACCELEROMETER
8. HUMIDITY/TEMPERATURE SENSOR
9. IR LINE TRACER
10. ULTRASONIC
11. SLIDE POTENTIOMETER
12. LED MATRIX
13. LED BAR
14. 1W LED
15. RGB LED
16. SERIAL RGB LED
17. LCD 16x2
18. 4-DIGIT 7-SEGMENT DISPLAY
19. BUZZER
20. RELAY
21. SERVO MOTOR
22. DC MOTOR
23. STEP MOTOR
24. BLUETOOTH
25. Wi-Fi
26. BREADBOARD
1-12
A description of each follows:
ARDUINO UNO
The word "Uno" means one in Italian. Arduino Uno shown in Fig. 1-10 is the core of
MTS-100 Tutor for Arduino trainer. The Uno board is the first in a series of USB
Arduino boards, and the reference model for the Arduino platform.
The Arduino Uno is a microcontroller board based on the ATmega328P. Fig. 1-11
shows the parts side of Arduino Uno board, which has 14 digital input/output pins (of
which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a
USB connection, an external power jack, an ICSP (In-Circuit Serial Programming)
header and a reset button. The Arduino Uno contains everything needed to support
the microcontroller; simply connect it to a computer with a USB cable or power it with
an AC-to-DC adapter or battery to get started.
1-13
Fig. 1-11 Arduino Uno (parts side)
1. RESET button: This button is used to reset the microcontroller of Arduino Uno. It
is internally wired to the RESET pin of POWER header.
2. USB Port: To upload a sketch to Arduino Uno, simply connect the USB port on
Arduino Uno to a USB port on a computer using a USB cable. In addition, the
USB port also provides dc power with maximum current draw of 0.5A.
3. External Power Jack: External (non-USB) power can come either from a 12V
AC-to-DC adapter or battery. The adapter can be connected by plugging a
2.1mm center-positive plug into the board's power jack. Leads from a battery can
be inserted in the GND and VIN pins of the POWER header.
1-14
(3) Pins 0 and 1 are the receive (RX) and transmit (TX) pins of serial Universal
Asynchronous Receiver/Transmitter (UART), respectively. These two pins
are used to receive (RX) and transmit (TX) TTL serial data. On the Arduino
Uno board, you'll see the RX and TX LEDs blink as the sketch is uploaded.
(4) Pins ~3, ~5, ~6, ~9, ~10, ~11: These 6 digital pins can be used as Pulse
Width Modulation (PWM) outputs with analogWrite() function.
(5) Pins ~10, ~11, 12, 13: Serial Peripheral Interface (SPI) pins, where pin ~10 is
SS (Slave Select), pin ~11 MOSI (Master Out Slave In), pin 12 MISO (Master
In Slave Out), and pin 13 is SCK (Serial Clock).
(6) GND: Ground pin.
(7) AREF: The reference voltage used for analog input (i.e. the value used as
the top of the input range) can be configured with analogReference()
function.
(8) SDA and SCL Pins: Labeled on the back side, the SDA (serial data line) and
SCL (serial clock line) of I2C bus are on the pin headers close to the AREF
pin.
5. Analog Input Pins: 6-pin header (female) labeled A0~A5, for analog signal input.
In addition, pins A5 and A4 can be also used as SCL and SDA, respectively.
6. Power Pins: 8-pin header (female), the leftmost pin is not connected. The
Arduino Uno can be powered via the USB connection or with an external power
supply. The power source is selected automatically. The power pins are:
(1) IOREF: This pin on the Arduino Uno board provides the voltage reference
with which the microcontroller operates. A properly configured shield can
read the IOREF pin voltage and select the appropriate power source or
enable voltage translators on the outputs to work with the 5V or 3.3V.
(2) RESET: Bring this pin low to reset the microcontroller of Arduino Uno. It is
internally wired to the RESET button.
(3) 5V: This pin outputs a regulated 5V from the regulator on the board. The
board can be supplied with power either from the DC power jack (7-12V), the
USB connector (5V), or the VIN pin of the board (7-12V). Supplying voltage
via the 5V or 3.3V pins bypasses the regulator, and can damage your board.
(4) 3.3V: A 3.3-volt supply generated by the on-board regulator. Maximum
current draw is 50 mA.
1-15
(5) GND: Two ground pins.
(6) VIN: The input voltage to the Arduino Uno when it's using an external power
source (as opposed to 5 volts from the USB connection or other regulated
power source). You can supply voltage through this pin, or, if supplying
voltage via the power jack, access it through this pin. The board can operate
on an external supply from 6 to 20 volts. If supplied with less than 7V,
however, the 5V pin may supply less than five volts and the board may
become unstable. If using more than 12V, the voltage regulator may overheat
and damage the board.
POWER
The MTS-100 built-in power regulator provides +5V/3A and +3.3V/1A for other
modules. Power supplies +5V and +3.3V are accessible on the P1 and P2 headers,
respectively. In addition, the P3 header is GND. See Fig. 1-12.
4x4 KEYPAD
The 4×4 keypad shown in Fig. 1-13 is a 16-button keypad which provides a useful
human interface component for microcontroller projects. Each key is a pushbutton,
with one end connected to one row, and the other end connected to one column.
Four row pins (R0~R3) and four column pins (C0~C3) are connected to the P6A
header.
1-16
Fig. 1-13 4x4 Keypad
DIP SWITCH
Fig. 1-14 shows the 8-pole DIP switch, which is a combination of eight switches to
provide switch states to the host device, typically a microcontroller. Each switch is a
slide switch, with one end connected through a pull-up resistor to +5V, and the other
end connected to ground. Switch outputs are connected to the P5 header pins.
When a switch is open, the output is high. When a switch is closed (in ON position),
the output is low.
1-17
Fig. 1-15 CDS & Microphone
JOYSTICK
Fig. 1-16 shows the joystick circuit. It consists of a tact switch (center pushbutton)
and two rotary potentiometers: horizontal potentiometer (H pot) and vertical
potentiometer (V pot). All the joystick pins are connected to the P24 header.
ACCELEROMETER
The accelerometer shown in Fig. 1-17, is used to detect the motion of the MTS-100.
It is a 3-axis, capacitive MEMS accelerometer chip with 12 bits of resolution. The
3-axis accelerometer signals are connected to the P26 header.
1-18
Fig. 1-17 Accelerometer
HUMIDITY/TEMPERATURE SENSOR
Fig. 1-18 shows the DHT11 Humidity/Temperature sensor, which is widely used to
measure ambient temperature and humidity. The measured temperature/humidity
data is sent over the single DATA line to the P27 header.
IR LINE TRACER
Fig. 1-19 shows the IR line tracer, which consists of three reflective IR sensors for
tracking control. The output signals of three reflective IR sensors are connected to
the P23A header.
1-19
Fig. 1-19 IR Line Tracer
ULTRASONIC
Fig. 1-20 shows the ultrasonic sensor which is used to measure the distance
between the ultrasonic sensor and the obstacle. Trigger and Echo signals are
connected to the P22B header.
1-20
SLIDE POTENTIOMETER
Fig. 1-21 shows two slide potentiometers. One is horizontally placed and called the
horizontal potentiometer (H pot). The wiper (middle terminal) of the H pot is
connected to the P7-1 header pin. The other is vertically placed and called the
vertical potentiometer (V pot). The wiper of the V pot is connected to the P7-2
header pin. Potentiometers are analog devices used for analog inputs.
LED MATRIX
Fig. 1-22 shows a single color 8×8 LED dot matrix, which is usually used for
displaying the characters and symbols when interfacing with a microcontroller. The
LED dot matrix contains 64 separate LEDs, eight row pins (cathodes), and eight
column pins (anodes). The 8 column pins are wired to the P12A header pins, and
the 8 row pins are wired to the P12B header pins.
1-21
LED BAR
Fig. 1-23 shows the 10-bit LED Bar, which consists of 10 discrete LEDs. The anodes
of the ten LEDs are tied through pull-up resistors to +5V, so every LED is driven by a
low input. When the LED input (cathode) is low, the corresponding LED will be on. If
the LED input (cathode) is high, the corresponding LED will be off. The ten LED
inputs are connected to the P9 header.
1W LED
Fig. 1-24 shows the high-power and high-brightness LED, 1W LED. It is used for
dimming control using PWM. The control signal is connected to the P8 header.
1-22
RGB LED
An RGB LED is a tri-color LED, which consists of red, green, and blue LED elements.
The MTS-100 adopts 4 PLCC6-package RGB LEDs as shown in Fig. 1-25. These
RGB LEDs are common-anode type. The scanning inputs are connected to the P10
header.
20 serial RGB LEDs are arranged as an M pattern as shown in Fig. 1-26. The control
signal is connected to the P10B header. SW10 is the power switch of serial RGB
LED. To use the serial RGB LED, you must place SW10 in the ON position.
LCD 16x2
The LCD module, LCD 1602 with backlight is shown in Fig. 1-27. LCD 1602 can
operate with 8- or 4-bit parallel interface when it is inserted in the P13A1 header. In
this case, control signals and data bus are connected to the P13A header. SW13 is
the power switch of the parallel LCD backlight.
1-23
When using I2C interface, LCD 1602 is inserted in the P13C header, SDA and SCL
signals are connected to the P13B1 header. The backlight of serial LCD is controlled
with backlight() and noBacklight() functions.
The 4-digit common-anode 7-segment display with decimal points and colon is
shown in Fig. 1-28. Segment inputs are connected to the P11A header, and digit
scan signal are connected to the P11B header. In addition, the colon is used for
blinking seconds of a digital clock.
BUZZER
Fig. 1-29 shows two buzzers: one is a self drive buzzer (LS14-1), the other is an
external drive buzzer (LS14-2). The trigger signal of the self drive buzzer is
connected to the P14-1 header pin, whereas the trigger signal of external drive
buzzer is connected to the P14-2 header pin.
1-24
Fig. 1-29 Buzzer
RELAY
Two SPDT relays RY1 and RY2 are shown in Fig. 1-30. The control signal to the
RY1 relay is connected to the P15-1 header pin. The contacts of RY1 relay are
connected to the terminals 1A, 1B, and 1C on the terminal block P16.
The control signal to the RY2 relay is connected to the P15-2 header pin. The
contacts of RY2 relay are connected to the terminals 2A, 2B, and 2C on the terminal
block P16.
SERVO MOTOR
There are two micro servo motors Y-axis servo (M1) and X-axis servo (M2). Each
servo motor can operate alone. When installed on the 2-axis servo bracket as shown
in Fig. 1-31, they can operate as a 2-axis robot for 2-axis motion control. The PWM1
control signal for Y-axis servo (M1) is connected to the P17B-1 header pin. The
PWM2 control signal for X-axis servo (M2) is connected to the P17B-2 header pin.
1-25
Fig. 1-31 Servo motor
DC MOTOR
MTS-100 is equipped with two separate DC motors, DCM1 and DCM2, as shown in
Fig. 1-32. Motor’s lead wires are wired to the P18B header in factory. The control
signals to the motor driver are connected to the P18A1 header.
STEP MOTOR
The step motor shown in Fig. 1-33, is a 200-step 6-wire step motor, which is used for
unipolar and bipolar drive control. The drive signals are connected to the P19D
header. Motor’s lead wires are wired in factory. The switch SW19 controls the enable
inputs EN12 and EN34 of motor driver IC.
1-26
Fig. 1-33 Step motor
BLUETOOTH
Fig. 1-34 shows the Bluetooth module on the MTS-100 Tutor for Arduino. TxD and
RxD pins of the Bluetooth module are connected to the P28A header.
Wi-Fi
Fig. 1-35 shows the Wi-Fi module on the MTS-100 Tutor for Arduino. TxD and RxD
pins of the Wi-Fi module are connected to the P29A1 header.
1-27
BREADBOARD
The half size breadboard is shown in Fig. 1-36. The universal breadboard offers a
convenient way to students for constructing the circuits that are not provided by the
MTS-100. The breadboard is constructed with sets of 6 vertical pins connected in
common. Thus when any component is inserted, the remaining tie points are
available for interconnecting wires or as test points for the circuit. In performing an
experiment, the use of the soldering iron on breadboard should be inhibited.
Arduino Sketch
Programs written in Arduino language are called sketches. These sketches are
written in the text editor of Arduino IDE and saved with the file extension .ino. An
Arduino sketch consists of four sections:
1-28
Digital I/O Functions
To use a digital pin for inputting or outputting control signal, first this pin must be
configured as an input or an output with pinMode(), and then you can read data from
this pin with digitalRead() function, or write data to this pin with digitalWrite()
function.
pinMode() function
Description:
The pinMode() function configures the specified pin to behave either as an input
or an output.
Syntax:
pinMode(pin, mode);
Parameters:
1. pin: the number of the pin whose mode you wish to set. (int)
2. mode: INPUT, OUTPUT, or INPUT_PULLUP
(1) INPUT: Arduino pins configured as INPUT with pinMode() are said to be
in a high-impedance state. Pins configured as INPUT make extremely
small demands on the circuit that they are sampling, equivalent to a
series resistor of 100 Megohms in front of the pin. This makes them
useful for reading a sensor.
(2) INPUT_PULLUP: The Atmega microcontroller on the Arduino has
internal pull-up resistors (resistors that connect to power internally) that
you can access. If you prefer to use these instead of external pullup
resistors, you can use the INPUT_PULLUP argument in pinMode().
(3) OUTPUT: Arduino pins configured as OUTPUT with pinMode() are said
to be in a low-impedance state. This means that they can provide a
substantial amount of current (40mA) to other circuits.
For example, to configure the digital pin 11 as an output pin, use this statement:
pinMode(11, OUTPUT);
Arduino Uno uses Atmel mega 328P microcontroller. The input/output pins of mega
microcontrollers are controlled with the following functions:
1-29
digitalWrite() function
Description:
The digitalWrite() function sets a pin configured as OUTPUT to either a HIGH or
a LOW state at the specified pin.
Syntax:
digitalWrite(pin, value);
Parameters:
1. pin: the pin number.
2. value: HIGH or LOW
The following example writes a LOW value to the digital pin 10:
digitalWrite(10, LOW);
digitalRead() function
Description:
The digitalRead() function reads the value from a specified digital pin, either
HIGH or LOW.
Syntax:
val = digitalRead(pin);
Parameters:
1. pin: the number of the digital pin you want to read (int).
2. val: the variable stores the value (HIGH or LOW) returned
The following example reads the value on the digital pin 2 and stores it in the
whatsValue variable:
whatsValue = digitalRead(2);
Time Functions
The execution speed of microcontrollers is very fast. Sometimes you need to delay
the execution on purpose, a delay() function is used in sketches to handle the
execution time. Arduino provides two delay functions:
1-30
delay() function
Description:
The delay() function pauses the program for the amount of time (in miliseconds)
specified as parameter.
Syntax:
delay(ms);
Parameters:
1. ms: the number of milliseconds to pause (unsigned long) ranging from 0 to
232-1 (4,294,967,295).
For example, to pause the execution for 1.23 seconds (1230 ms), use this
statement:
delay(1230);
delayMicroseconds() function
Description:
The delayMicroseconds() function pauses the program for the amount of time
(in microseconds) specified as parameter.
Syntax:
delayMicroseconds(us);
Parameters:
1. us: the number of microseconds to pause (unsigned int), ranging from 0 to
216-1 (65,535). However, use a number less than 16,383 in order to avoid a
big deviation.
delayMicroseconds(150);
for Statement
The for statement is widely used in most programming languages and it is useful for
any repetitive operation. In Arduino sketches, the for statement is used to repeat a
block of statements enclosed in braces. An increment counter is usually used to
increment and terminate the loop. See the syntax:
1-31
for(op1 ; op2 ; op3)
{ statement block
}
The op1 (initialization) happens first and exactly once. Each time through the loop,
the op2 (condition) is tested; if it's true, the statement block, and the op3 (increment)
is executed, then the condition is tested again. When the condition becomes false,
the loop ends.
The following example is designed to execute all the statements in the statement
block 10 times:
In this exercise, you’ll use LED Bar to show the lighting effect of running LED -- An
ON LED runs from left to right, and then from right to left.
PROCEDURE
Circuit Connections
Perform the following steps and refer to the wiring diagram in Fig. 1-37 to complete
the connections for running LED control.
1. Connect the Arduino Uno board to your computer using the USB cable.
2. Connect a correct AC voltage from a wall outlet to your MTS-100 using the AC
power cord, and then turn on the power.
1-32
3. Connect the Arduino pins 2-11 to the header pins P9-1 ~ P9-10 (LED BAR). This
makes the LED Bar available.
Program Design
4. Open the Arduino IDE window. In the text editor, write your sketch as shown
below and save it as “ex1-2-1.ino”. Note: You can omit the program comments to
save time for the actual study of the sketch.
1-33
//========== Main Code ============
void loop()
{ for(int i=0;i<10;i++)
{ digitalWrite(LED[i], 0); // Turn on LEDs one by one from left to right
delay(100); // A delay of 0.1 seconds
digitalWrite(LED[i], 1); // Turn off LEDs one by one from left to right
}
for(int i=9;i>=0;i--)
{ digitalWrite(LED[i], 0); // Turn on LEDs one by one from right to left
delay(100); // A delay of 0.1 seconds
digitalWrite(LED[i], 1); // Turn off LEDs one by one from right to left
}
}
Sketch uses 1,172 bytes (3%) of program storage space. Maximum is 32,256
bytes.
Global variables use 29 bytes (1%) of dynamic memory, leaving 2,019 bytes
for local variables. Maximum is 2,048 bytes.
7. You should see a running effect shown on the LED Bar. One ON-LED runs from
left to right, and then from right to left, and repeat.
1-34