1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
\chapter{Communication between Software and Arduino}
\thispagestyle{empty}
\label{sec:sw-env}
\newcommand{\LocSWfig}{\Origin/user-code/sw-env/figures}
\newcommand{\LocSWscicode}{\Origin/user-code/sw-env/scilab}
\newcommand{\LocSWscibrief}[1]{{\texttt
Origin/user-code/sw-env/scilab/#1}, see \fnrefp{fn:file-loc}}
\newcommand{\LocSWardcode}{\Origin/user-code/sw-env/arduino}
\newcommand{\LocSWardbrief}[1]{{\tt \seqsplit{
Origin/user-code/sw-env/arduino/#1}}, see \fnrefp{fn:file-loc}}
\newcommand{\LocSWchkcode}{\Origin/tools/scilab}
\newcommand{\LocSWchkbrief}[1]{{\tt \seqsplit{
Origin/tools/scilab/#1}}, see \fnrefp{fn:file-loc}}
\newcommand{\LocSWfirmcode}{\Origin/tools/floss-firmware}
\newcommand{\LocSWfirmbrief}[1]{{\tt \seqsplit{
Origin/tools/floss-firmware/#1}}, see \fnrefp{fn:file-loc}}
\newcommand{\LocFIMpycode}{\Origin/tools/python} %added for python
\newcommand{\LocFIMpybrief}[1]{{\tt \seqsplit{%
Origin/tools/python/#1}}, see \fnrefp{fn:file-loc}} % added for python
\newcommand{\LocFIMjuliacode}{\Origin/tools/julia} %added for julia
\newcommand{\LocFIMjuliabrief}[1]{{\tt \seqsplit{%
Origin/tools/julia/#1}}, see \fnrefp{fn:file-loc}} % added for julia
%%%%%%OpenModelica Starts
\newcommand{\LocFIMOpenModelicacode}{\Origin/tools/openmodelica/windows/} %added for OpenModelica
\newcommand{\LocFIMOpenModelicabrief}[1]{{\tt \seqsplit{%
Origin/tools/openmodelica/windows/#1}}, see \fnrefp{fn:file-loc}} % added for OpenModelica
%%%%%OpenModelica Ends
In this chapter, we shall briefly walk through the software
environment that needs to be set up before we could start with the
\arduino\ board-based experiments. We shall start with the \arduino\
compatible Integrated Development Environment (IDE), termed as Arduino
IDE, that would be used to load the FLOSS firmware on to the
microcontroller. The FLOSS firmware to be loaded could be developed to serve
different purposes as per the requirement. For example,
\begin{itemize}
\item To run \arduino\ stand-alone, without waiting for any commands
from other software or hardware, for the specified time or until
power off
\item To decode the commands sent by other software, such as Scilab, Python,
Julia, OpenModelica, etc., through a serial port, and
execute the given instructions %\item Combination of the above two
\end{itemize}
Next, we shall discuss other open source software
tools and a related toolbox that can communicate with \arduino\
over a serial port using RS232 protocol.
% Subsequently, we shall discuss other open-source software tools such as Python, Julia, and OpenModelica.
\section{Arduino IDE}\label{arduino-ide}
\label{sec:ard-start}
Arduino development environment is compatible with popular desktop
operating systems. In this section, we will learn to set up this tool
for the computers running Microsoft Windows or Linux. Later, we shall
explore the important menu options in the Arduino IDE and run a sample
program. The following two steps have to be followed whatever operating
system is used:
\begin{enumerate}
\item To begin, we need an \arduino\ board with a USB cable (A plug to
B plug) as shown in \figref{arduino}.
\item Connect it to a computer and power it up. The moment you connect \arduino\
to the computer, an on-board power LED will turn ON.
\end{enumerate}
\subsection{Downloading and installing on Windows}
First, carry out the steps numbered 1 and 2 given above.
Starting from download, we shall go through the steps to set up
Arduino IDE on Windows OS:
\begin{enumerate}
\setcounter{enumi}2
\item Visit the URL, {\tt https://www.arduino.cc/en/software}. On the
right right side of the page, locate the link \emph{Windows ZIP file} and click on it.
This may redirect you to the download/donate page. Read the instructions and proceed with the
download.
\item Extract the downloaded ZIP file to Desktop. Do not alter any
file or directory structure.
\item Click on the Windows Start Menu, and open up the ``Control
Panel''.
\item While in the Control Panel, navigate to ``System and Security'',
click on ``System'' and then choose the ``Device Manager''.
\item Look for ``Other devices'' in the ``Device Manager'' list,
expand and locate ``Unknown device''. This may be similar to what
is shown in \figref{win-device-manager}. In case, you don't see
``Unknown device,'' look for ``Ports (COM \& LPT)'' and expand it to locate
``USB Serial Device (COM2)''.
This may be similar to what is shown in \figref{win-device-manager-com}.
\item Right-click on the ``Unknown device'' (or ``USB Serial Device (COM2)'' as shown in
the previous step) and select the ``Update Driver Software'' (or ``Update driver'') option
as shown in \figref{win-dri-update}.
\item Next, choose the ``Browse my computer for Driver software''
option.
\item Navigate to the newly extracted Arduino folder on the Desktop and
select ``drivers'' folder.
\item Windows will now finish the driver installation. The Arduino IDE
is ready for use.
\end{enumerate}
To launch Arduino IDE, browse to extracted Arduino folder on the Desktop and double click on ``arduino.exe''.
\begin{figure}
\centering
\includegraphics[width=\linewidth]{\LocHWfig/hw-device-manager.jpg}
\caption{Windows device manager}
\label{win-device-manager}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\linewidth]{\LocSWfig/device-manager-com.png}
\caption{Windows device manager}
\label{win-device-manager-com}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\linewidth]{\LocHWfig/update-driver.png}
\caption{Windows update driver option}
\label{win-dri-update}
\end{figure}
\subsection{Downloading and installing on GNU/Linux Ubuntu}
We will now explain the installation of Arduino software on the
GNU/Linux operating system. We shall perform the installation on the 64-bit
Ubuntu 18.04 LTS operating system. These
instructions will work for other GNU distributions too, with little or
no modification. First, carry out the steps numbered 1 and 2 given
above. Then carry out the following:
\begin{enumerate}
\setcounter{enumi}2
\item First, update your system. Open the terminal emulator, type,
{\tt sudo apt-get update} and press Enter.
\item Find out your operating system support for 64-bit
instructions. Open the terminal emulator and type, {\tt uname -m}
\item If it returns ``x86\_64'', then your computer has 64-bit
operating system. There is no visible performance difference in 32
and 64-bit Arduino versions.
\item Download the suitable Arduino Software version (32 or 64-bit)
from \\ {\tt https://www.arduino.cc/en/software}. As mentioned
earlier, we will perform experiments with a 64-bit installation.
\item At the time of writing this book, we worked with version 1.8.13.
Assuming that you have downloaded the tar file in
the Downloads directory, execute the following
commands on the terminal:
\begin{quote}
{\tt cd {\large\textasciitilde}/Downloads\\
tar -xvf arduino-1.8.13-linux64.tar.xz\\
sudo mv arduino-1.8.13 /opt}
\end{quote}
\item In the same terminal session, install the required Java Runtime
Environment with a command like,
{\tt sudo apt-get -y install openjdk-8-jre}
\item \label{itm:port-check} Execute the
following command on the terminal to list the serial port number.\\
{\tt ls /dev/ttyACM*}\\
Note down the serial device filename. Suppose that it
is {\tt ttyACM0}.
\item \label{itm:port-access} To make the USB port available to all users, set the read-write
permission to the listed port:
{\tt sudo chmod a+rw /dev/ttyACM0}. Each time you plug the \arduino\
into the computer, you need to execute the commands given in the steps
numbered \ref{itm:port-check} and \ref{itm:port-access}.
% \item \label{itm:create-shortcut} Create a shortcut on the desktop:\\
% {\tt cd {\large \textasciitilde}/Desktop\\
% ln -s /opt/arduino-1.8.13/arduino}
% \item \label{itm:give-permission} Give executable permission to this file through the following
% command on the terminal: {\tt chmod +x arduino}
% Ubuntu opens executable text files with an editor instead of
% executing them. To be able execute a file, open the ``Files''
% program from the launcher, go to menu ``Edit'', ``Preferences'', tab
% ``Behavior'' and set ``Executable Text Files'' to ``Ask each time'',
% as shown in \figref{ard-lin-executable}.
\end{enumerate}
% \begin{figure}
% \centering
% \includegraphics[scale=0.5]{\LocHWfig/executable.png}
% \caption{Executable permission to Arduino IDE}
% \label{ard-lin-executable}
% \end{figure}
% Then double click the Arduino shortcut on the Desktop and, click ``Run''
% in the dialog window to start the Arduino IDE. The dialog box is shown in \figref{ard-lin-run} for reference.
% \begin{figure}
% \centering
% \includegraphics[scale=0.5]{\LocHWfig/run.png}
% \caption{Confirmation for executing Arduino script}
% \label{ard-lin-run}
% \end{figure}
The Arduino IDE is now ready for use. To launch it, carry out the steps given below:
\begin{enumerate}
\item Open a terminal by pressing the Alt+Ctrl+T keys together.
\item Navigate into the {\tt opt} directory, as shown in \figref{arduino-opt}.\\
{\tt cd /opt/arduino-1.8.13/}
\item Start the Arduino IDE by executing the command {\tt ./arduino}
\end{enumerate}
% There are chances that you might not
% get the Arduino shortcut on your Desktop after executing the commands given in
% steps numbered \ref{itm:create-shortcut} and \ref{itm:give-permission}.
% In that case, you can navigate into the {\tt /opt/} directory and execute the
% commands as given in \figref{arduino-opt} to start the Arduino IDE.
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocSWfig/launch-arduino-opt.png}
\caption{Linux terminal to launch Arduino IDE}
\label{arduino-opt}
\end{figure}
\subsection{Arduino Development Environment}
\label{sec:Arduino-IDE}
The Arduino development environment, as shown in \figref{ard-ide},
consists of
a text editor for writing code, a message area, a text console, a
toolbar with buttons for common functions, and a series of menus. It
connects to the Arduino hardware to upload programs and communicate
with them.
\begin{figure}
\centering
\includegraphics[width=\linewidth]{\LocHWfig/arduino-ide.jpg}
\caption{Arduino IDE}
\label{ard-ide}
\end{figure}
Software written using Arduino is called sketches. These sketches are
written in the text editor. Sketches are saved with the file extension
``.ino''. The frequently used icons shown in the toolbar, below the menu bar, are explained next. The names of these icons can be viewed by hovering the mouse pointer over each of them.
\begin{enumerate}
\item Verify: Checks your code for errors
\item Upload: Compiles your code and uploads it to the Arduino I/O
board
\item New: Creates a new sketch
\item Open: Presents a menu of all the sketches in your
sketchbook - clicking one will open it within the current window
\item Save: Saves your sketch
\item Serial Monitor: Opens the serial port window - the location of
this is shown in the top right-hand corner of \figref{ard-ide}
\end{enumerate}
Note that these appear from left to right in the editor window. Next, we shall go through the additional useful options under the menu.
\begin{enumerate}
\item File
\begin{enumerate}
\item Examples: Examples that come at the time of installation
\item Page Setup: Configures the page parameters for the printer
\item Preferences: Customizes font, language, and other parameters for
the IDE
\end{enumerate}
\item Sketch
\begin{enumerate}
\item Include Library: Adds a library to your sketch by inserting {\tt
\#include} statements at the start of your code
\end{enumerate}
\item Tools
\begin{enumerate}
\item Auto Format: Indents code so that opening and closing curly
braces line up
\item Archive Sketch: Archives a copy of the current sketch in .zip
format. The archive is placed in the same directory as the sketch.
\item Board: Selects the board that you're using
\item Port: This menu contains all the serial devices (real or
virtual) on your machine. It should automatically refresh every time
you open the top-level tools menu.
\item Programmer: This can be used to select a hardware programmer when programming a board or chip and not using the on-board USB-serial
connection. Normally you won't need this, but if you're burning a
bootloader to a new microcontroller, you will use this.
\item Burn Bootloader: The items in this menu allow you to burn a
bootloader onto the microcontroller on an Arduino board. This is not
required for normal use of an Arduino board but is useful if you
purchase a new ATmega microcontroller (which normally comes without a
bootloader). Ensure that you've selected the correct board from the
Boards menu before burning the bootloader.
\end{enumerate}
\end{enumerate}
\subsection{Testing Arduino with a sample program}
\label{sec:testing-arduino}
Now, as we have a basic understanding of Arduino IDE, let us try an
example program.
\begin{enumerate}
\item Open the Arduino IDE by clicking the shortcut ``arduino'' from
Desktop in Ubuntu. In MS Windows browse to extracted Arduino folder
on Desktop and double click on ``arduino.exe''.
\item In the Arduino IDE, to know the path of your sketch files,
navigate to File, then Preferences and then locate the ``Sketchbook
location'' text box at the top. You may change the path of your
storage location. In this book, we will keep it unchanged. The path
will be different for Windows and Ubuntu.
\item To load a sample program, navigate and click on sketch ``File'',
then Examples, then 01.Basics, and then Blink.
\item A new IDE instance will open with Blink LED code. You may close
the previous IDE window now.
\item Click ``verify'' to compile. The ``status bar'' below the text editor
shall show ``Done compiling'' on success.
\item Connect \arduino\ board to PC. You may connect the board
before writing the sketch too.
\item Now, navigate to ``Tools'', then Port, and select the available
port. If the port option is greyed out (or disabled) then reinsert the
USB cable to the PC.
\item Now select the upload button to compile and send the firmware to
the Arduino Uno board.
\item If the upload is successful, you will notice the on-board orange LED
next to the Arduino logo will start blinking.
\item It is safe to detach the USB cable at any moment.
\end{enumerate}
Arduino programming syntax is different from other languages. In an
embedded setup, a program is expected to run forever. To facilitate
this, the Arduino programming structure has two main functions: {\tt setup()}:
Used to initialize variables, pin modes, libraries, etc. The setup
function will run only once after each powerup or board reset.
{\tt loop()}: Code inside this function runs forever. An Arduino program
must have {\tt setup()} and {\tt loop()} functions. We will give several examples
in this book to explain this usage.
An inbuilt offline help is available within the IDE. You may access
the explanation on IDE by navigating to ``Help'' and then
Environment.
\subsection{FLOSS Firmware}
We have provided a code to check whether the FLOSS firmware has been
properly installed. The first few lines of this code follow.
\begin{ardcode}
\acaption{First 10 lines of the FLOSS firmware}{First 10 lines of
the FLOSS firmware. Available at
\LocSWfirmbrief{floss-firmware.ino}. Following the
steps given in sections \ref{sec:Arduino-IDE} and
\ref{sec:testing-arduino}, open this code in
Arduino IDE and upload it to \arduino.
Once the upload is successful, you should expect a success message
at the bottom of Arduino IDE, as shown in \figref{ard-ide}. }
\label{ard:firmware}
\lstinputlisting[firstline=1,lastline=10]
{\LocSWfirmcode/floss-firmware.ino}
\end{ardcode}
% \subsection{Arduino firmware to work with scilab toolbox}
% \label{sec:firmware}
% A firmware is basically a program that continuously runs inside a
% microcontroller. It is a collection of routines corresponding to the
% required functionalities. It is typically written in Assembly and C
% programming language. It is compiled and converted into
% binary(hexadecimal values with addresses) for the target
% microcontroller. The binary file(also called hex file) is then
% uploaded to the microcontroller’s internal ROM. The firmware that has to
% be used to work with scilab toolbox is at \ardref{ard:firmware}. It
% is an Arduino IDE compatible file and can be opened in an Arduino
% IDE. Let us see a brief explanation of this firmware.
% The firmware used for Arduino Uno in this book has the following tasks
% to perform:
% \begin{enumerate}
% \item Reading instructions from a computer(running Scilab) over serial
% interface and decoding them.
% \item Performing the task mentioned in the instruction.
% \item Optionally sending data back to the computer over an serial
% interface.
% \end{enumerate}
% Let us see a simple example of reading values from the LDR that is
% on the shield.
% The firmware waits for a particular character (command) to be sent
% from the computer. The character “A”, in quoted form, as shown here,
% is reserved for analog read
% routine. So if Scilab wants analog values from the microcontroller, it
% sends the character “A” to Arduino Uno. On receiving “A”, the Arduino
% Uno
% jumps to the routine of Analog read. Here it again waits for the
% computer to now send the pin number from where it is supposed to read
% the LDR value. This pin number is in ASCII text. Arduino Uno first
% checks if the ASCII lies between a valid range. If yes, it takes its
% ASCII value as a valid pin number. The value 48 is subtracted from it
% to reveal the character and thus the pin number. This pin number is
% then sent to the analogRead() function. The analogRead() function is
% an inbuilt Arduino function imported from the header file. The
% analogRead() function then actually reads from the pin and returns the
% analog value. This value is then sent back to the
% computer(Scilab). The correct firmware must be loaded inside Arduino
% Uno to be able to successfully carry out any of the experiment
% explained throughout this book. It is strongly recommended to confirm
% this before proceeding.
|