Vous êtes sur la page 1sur 34

UNIT 1 The MTS-100 Tutor for Arduino

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

Arduino is an open-source physical computing platform based on a simple I/O board


and a development environment that implements the Processing/Wiring language.
Arduino can be used to develop stand-alone interactive objects or can be connected
to software on your computer.

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

After completing this exercise, you should be able to:


1. Familiarize yourself with the operation of Arduino IDE.
2. Install the Arduino IDE Software on your PC.

DISCUSSION

Arduino Software (IDE)

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.

Fig. 1-1 Arduino 1.6.1 window

1-2
Menu Bar

The menu bar consists of five menus:


1. File: File menu provides the menu commands such as New, Open,
Sketchbook, Examples, Close, Save, Save As, Upload, Page Setup,
Print, Preferences, Quit.
2. Edit: Similar to common word processors, Edit menu provides the menu
commands such as Undo, Redo, Cut, Increase/Decrease Indent, Find…
commands, etc. Undo, Redo, Cut, Copy, Copy for Forum, Copy as
HTML, Paste, Select All, Comment/Uncomment, Increase Indent,
Decrease Indent, Find, Find Next, Find Previous, Use Selection for Find.
3. Sketch: Sketch menu includes the menu commands Verify/Compile, Show
Sketch Folder, Add File, Import Library. Sketches are the programs
written in Arduino language.
4. Tools: Tools menu includes the menu commands such as Auto Format,
Archive Sketch, Fix Encoding & Reload, Serial Monitor, Board, Port,
Programmer, Burn Bootloader.
5. Help: Help menu contains the menu commands such as Getting Started,
Environment, Troubleshooting, Reference, Find in Reference,
Frequently Asked Questions, Visit Arduino.cc, About Arduino.

Toolbar

The toolbar buttons allow you to verify and upload programs, create, open, and save
sketches, and open the serial monitor.

1. Verify button: Check your code for errors compiling it.


2. Upload button: Compile your code and upload it to the configured board.
3. New button: Create a new sketch.
4. Open button: Present a menu of all the sketches in your sketchbook. Clicking
one will open it within the current window overwriting its content.
5. Save button: Save your sketch.
6. Serial Monitor button: 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.

Table 1-1 Arduino IDE 1.6.1 menu commands and shortcuts


Menu Command Shortcut Description
Create a new instance of the editor,
File > New + with the bare minimum structure of a
sketch already in place.
Allow to load a sketch file browsing
File > Open… + through the computer drives and
folders.
Close the instance of the Arduino
File > Close +
Software from which it is clicked.
Save the sketch with the current
name. If the file hasn't been named
File > Save +
before, a name will be provided in a
"Save as.." window.
Compile and load the binary file onto
File > Upload + the configured board through the
configured Port.
Open the Preferences window where
some settings of the IDE may be
File > References +
customized, as the language of the
IDE interface.
Send the current sketch to the printer
File > Print + according to the settings defined in
Page Setup.
Close all IDE windows. The same
sketches open when Quit was chosen
File > Quit +
will be automatically reopened the
next time you start the IDE.
> New Tab + + Open a new tab.
Go back of one or more steps you did
Edit > Undo +
while editing.
When you go back, you may go
Edit > Redo +
forward with Redo.
Remove the selected text from the
Edit > Cut +
editor and places it into the clipboard.
Duplicate the selected text in the
Edit > Copy +
editor and places it into the clipboard.
Put the contents of the clipboard at
Edit > Paste +
the cursor position, in the editor.

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)

Installing Arduino Software 1.6.1

1. Insert MTS-100 CD into your CD-ROM drive.

2. Double-click the arduino-1.6.1-windows.exe file in the Software folder on


MTS-100 CD to start installation. A License Agreement dialog opens as shown in
Fig. 1-4.

1-7
Fig. 1-4 License Agreement dialog

3. Click button. The Installation Options dialog appears as shown in Fig.


1-5.

Fig. 1-5 Installation Options dialog

1-8
4. Click button. The Installation Folder dialog opens as shown in Fig.
1-6.

Fig. 1-6 Installation Folder dialog

5. Click button to install Arduino in the default destination folder. The


Installing window opens and the Setup starts to install Arduino software in the
selected destination folder, as shown in Fig. 1-7.

Fig. 1-7 Installing window

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.

Fig. 1-8 Completed window

7. Copy the Adafruit_NeoPixel-master, Arduino-LiquidCrystal-I2C-library-master,


DHT-sensor-library-master, and MMA8452_Accelerometer folders in the
Libraries folder on MTS-100 CD to your (home folder)/Arduino/Libraries folder.

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.

Unzipping Windows ZIP file for non admin install

1. Uncompress the arduino-1.6.1-windows.zip file in the Software folder on


MTS-100 CD. An arduino-1.6.1 folder will be produced. In this folder, you’ll see
an Arduino.exe file. You can run the arduino.exe file on Windows-based
computers to open the Arduino IDE window, as shown in Fig. 1-1.

2. Copy the Adafruit_NeoPixel-master, Arduino-LiquidCrystal-I2C- library-master,


DHT-sensor-library-master, and MMA8452_Accelerometer folders in the
Libraries folder on MTS-100 CD to your (home folder)/Arduino-1.6.1/Libraries
folder.

1-10
Exercise 1-2 Hardware Installation

EXERCISE OBJECTIVE

After completing this exercise, you should be able to:


1. Familiarize yourself with the hardware of MTS-100 Tutor for Arduino.
2. Write sketches to control the LED Bar.

DISCUSSION

MTS-100 Tutor for Arduino

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.

Fig. 1-9 MTS-100 Tutor for Arduino

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.

Fig. 1-10 Arduino Uno photo

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.

4. Digital I/O Pins: 18-pin header (female)


(1) 14 digital pins (0 to 13): All pins are TTL compatible (5V). All digital pins can
source or sink up to 40 mA of current to other devices/circuits. There are
20~50kΩ pullup resistors built into the Atmega chip that can be accessed
from software. These built-in pullup resistors are accessed by setting the
pinMode() as INPUT_PULLUP.
(2) Pin 13: There is a built-in LED (L LED) driven by digital pin 13. When the
digital pin 13 is high, the L LED is on; when the digital pin is low, it is off.

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.

Fig. 1-12 Power supply

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.

Fig. 1-14 DIP switch

CDS & MICROPHONE


Fig. 1-15 shows the CdS and microphone circuit. The microphone is a sound
sensitive device which converts sound waves to electrical signals. The microphone
output is amplified and then connected to the P20-2 header pin. The CdS is a light
sensitive device, which is used to sense the brightness of light. The CdS signal is
connected through a buffer to the P20-1 header pin.

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.

Fig. 1-16 Joystick

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.

Fig. 1-18 DHT11 Humidity/Temperature Sensor

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.

Fig. 1-20 Ultrasonic sensor

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.

Fig. 1-21 Slide Potentiometer

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.

Fig. 1-22 LED Matrix

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.

Fig. 1-23 LED Bar

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.

Fig. 1-24 1W LED

1-22
RGB LED

Fig. 1-25 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.

SERIAL RGB LED

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.

Fig. 1-26 Serial RGB LED

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.

Fig. 1-27 LCD 16x2

4-DIGIT 7-SEGMENT DISPLAY

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.

Fig. 1-28 4-digit 7-segment display

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.

Fig. 1-30 Relay

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.

Fig. 1-32 DC motor

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.

Fig. 1-34 Bluetooth module

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.

Fig. 1-35 Wi-Fi module

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.

Fig. 1-36 Breadboard

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. Global Declaration: Essentially, Arduino language is C language. Like other


programming languages, some tasks must be completed at the start of a sketch,
such as declaring global variables (can be used by every function in a program),
and including header file (or library).
2. Initialization: When a sketch starts, basic setup will be executed, such as
initializing variables, configuring pin modes, starting libraries, or function intended
to execute once. For example, the setup() function will only run once, after each
powerup or reset of the Arduino board.
3. Main Code: The program body will be executed repeatedly.
4. Functions: This section allows you to write your own functions that can be called
by other functions. You can omit this section if you have no functions to be
written.

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.

For example, to pause 150 μs, use this statement:

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
}

There are three parts to the for loop header:


1. op1: initialization
2. op2: condition
3. op3: increment

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:

for(int i=0 ; i<10 ; i++)


{ statement block
}

Designing a Running LED

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.

Fig. 1-37 Wiring diagram of running LED

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.

//========== ex1-2-1.ino ============


//========== Global Declaration ============
// pins 2~11 connected to P9 (LED Bar, active-low drive
const int LED[]={2,3,4,5,6,7,8,9,10,11}; // LED Bar pins connected to digital pins 2-11
//========== Initialization ============
void setup()
{ for(int i=0;i<10;i++)
{ pinMode(LED[i], OUTPUT); // Configure digital pins 2-11 as outputs
digitalWrite(LED[i], 1); // Turn all LEDs off
}
}

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
}
}

Compiling and Uploading

5. Once the editing is completed, press + keys to compile your sketch.


If “Error compiling” appears in the console, the message area will show the error
message with line number. If “Done compiling” appears in the console, the
message area will display memory usage for code and variables as follows:

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.

6. Press + keys to upload the code to the Arduino board.

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

Vous aimerez peut-être aussi