\chapter {Interfacing a Thermistor}
\thispagestyle{empty}
\label{thermistor}

\newcommand{\LocTHERMfig}{\Origin/user-code/thermistor/figures}
\newcommand{\LocTHERMscicode}{\Origin/user-code/thermistor/scilab}
\newcommand{\LocTHERMscibrief}[1]{{\tt \seqsplit{%
        Origin/user-code/thermistor/scilab/#1}}, see \fnrefp{fn:file-loc}}
\newcommand{\LocTHERMardcode}{\Origin/user-code/thermistor/arduino}
\newcommand{\LocTHERMardbrief}[1]{{\tt \seqsplit{%
        Origin/user-code/thermistor/arduino/#1}}, see \fnrefp{fn:file-loc}}

%%%%%%%%%%%python starts
\newcommand{\LocTHERMpycode}{\Origin/user-code/thermistor/python}
\newcommand{\LocTHERMpybrief}[1]{{\tt \seqsplit{%
        Origin/user-code/thermistor/python/#1}}, see \fnrefp{fn:file-loc}}
%%%%%%%%%%%python ends

%%%%%%%%%julia
\newcommand{\LocTHERMjuliacode}{\Origin/user-code/thermistor/julia}
\newcommand{\LocTHERMjuliabrief}[1]{{\tt \seqsplit{%
        Origin/user-code/thermistor/julia/#1}}, see \fnrefp{fn:file-loc}}
%%%%%%julia

%%%%%%%%%OpenModelica starts
\newcommand{\LocTHERMOpenModelicacode}{\Origin/user-code/thermistor/OpenModelica}
\newcommand{\LocTHERMOpenModelicabrief}[1]{{\tt \seqsplit{%
        Origin/user-code/thermistor/OpenModelica/#1}}, see \fnrefp{fn:file-loc}}
%%%%%%OpenModelica ends

A thermistor, usually made of semiconductors or metallic oxides, is a
temperature dependent/sensitive resistor. Depending on the temperature
in the vicinity of the thermistor, its resistance changes. Thermistors
are available in two types, NTC and PTC. NTC stands for Negative
Temperature Coefficient and PTC for Positive Temperature
Coefficient. In NTC thermistors, the resistance decreases with the
increase in temperature and vice versa. Whereas, for PTC types, the
resistance increases with an increase in temperature and vice
versa. The temperature ranges, typically, from $-55^{\circ}$ Celsius
to $+125^{\circ}$ Celsius.

Thermistors are available in a variety of shapes such as beads, rods,
flakes, and discs. Due to their compact size and low cost, they are
widely used in the applications where even imprecise temperature
sensing is sufficient. They, however, suffer from noise and hence need
noise compensation. In this chapter we shall interface a thermistor
with the \arduino\ board.

\section{Preliminaries}
A typical thermistor and its symbolic representation are shown in
\ref{fig:therm} and \ref{fig:thermsym} respectively. The thermistor is
available on the shield provided with the kit.  It is a bead type
thermistor having a resistance of 10k at room temperature. A voltage
divider network is formed using thermistor and another fixed 10k
resistor. A voltage of 5 volts is applied across the series
combination of the thermistor and the fixed 10k resistor. Voltage
across the fixed resistor is sensed and is given to the ADC via pin
4. Hence at room temperature, both the resistors offer 10k resistance
resulting in dividing the 5V equally. A buzzer is also connected on
pin 3 which is a digital output pin.
Connections for this experiment are shown in \ref{fig:therm-conn}
and \ref{fig:buzzer-conn}.  Nevertheless, the user doesn't need to
connect any wire or component explicitly.


\begin{figure}
  \centering
  \subfloat[Pictorial representation of a thermistor\cite{therm-wiki}]{
    \includegraphics[width=\smfig]{\LocTHERMfig/NTC-bead.jpg}
    \label{fig:therm}} \hfill
  \subfloat[Symbolic representation of a thermistor]{
    \includegraphics[width=\tnfig]{\LocTHERMfig/therm-sym.png}
    \label{fig:thermsym}}
  \caption{Pictorial and symbolic representation of a thermistor}
\end{figure}


\begin{figure}
  \centering
  \subfloat[Thermistor connection diagram]{
    \includegraphics[width=\smfig]
    {\LocTHERMfig/THERMISTOR-Diagram-crop.pdf}
    \label{fig:therm-conn}} \hfill
  \subfloat[Buzzer connection diagram]{
    \includegraphics[width=\smfig]
    {\LocTHERMfig/BUZZER-Diagram-crop.pdf}
    \label{fig:buzzer-conn}}
  \caption{Internal connection diagrams for thermistor and buzzer on the shield}
\end{figure}

\section{Connecting a thermistor with \arduino\ using a breadboard}
This section is useful for those who either don't have a shield or don't want to use the shield
for performing the experiments given in this chapter.

A breadboard is a device for holding the components of a circuit and connecting
them together. We can build an electronic circuit on a breadboard without doing any
soldering. To know more about the breadboard and other electronic components,
one should watch the Spoken Tutorials on Arduino as published on
  {\tt https://spoken-tutorial.org/}. Ideally, one should go through all the
tutorials labeled as Basic. However, we strongly recommend the readers should
watch the fifth and sixth tutorials, i.e., {\tt First Arduino Program} and
  {\tt Arduino with Tricolor LED and Push button}.

In case you have a thermistor, and you want to connect it with \arduino\ on a breadboard,
please refer to \figref{fig:ard-therm-bread}. The connections given in this figure
can be used to read values from the thermistor connected to analog pin 4 on \arduino\
board.
\begin{figure}
  \centering
  \includegraphics[width=\textwidth]{\LocTHERMfig/thermistor.png}
  \caption{A thermistor to read its values with Arduino Uno using a breadboard}
  %\redcolor{connected on pin no. D12}}
  \label{fig:ard-therm-bread}
\end{figure}
As shown in \figref{fig:ard-therm-bread}, one leg of the thermistor is connected
to 5V on \arduino\ and the other leg to the analog pin 4 on  \arduino. A resistor is also
connected to the same leg and grounded. From \figref{fig:therm-conn} and \figref{fig:ard-therm-bread}, one can infer that a resistor
along with the thermistor is used to create a voltage divider circuit. The varying
resistance of the thermistor is converted to a varying voltage. Finally, this voltage is used
by the analog pin 4 of \arduino\ in its logic.

\begin{figure}
  \centering
  \includegraphics[width=\textwidth]{\LocTHERMfig/thermistor-buzzer.png}
  \caption{A thermistor to control a buzzer with Arduino Uno using a breadboard}
  %\redcolor{connected on pin no. D12}}
  \label{fig:ard-therm-buzzer}
\end{figure}
The connections shown in \figref{fig:ard-therm-buzzer} can be used to control a buzzer,
depending on the values from the thermistor. As shown in \figref{fig:ard-therm-buzzer},
digital pin 3 on \arduino\ is connected to the one of the legs of the buzzer. Another
leg of the buzzer is connected to GND of \arduino.


\section{Interfacing the thermistor from the Arduino IDE}
\subsection{Interfacing the thermistor}
In this section we will learn how to read values from the thermistor
connected at pin 4 of the \arduino\ board. We shall also see how to
drive a buzzer depending upon the thermistor values. The shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}. The reader should go through the
instructions given in \secref{sec:ard-start} before getting started.

\begin{enumerate}
  \item A simple code to read the values from thermistor is given in
        \ardref{ard:therm-read}. The Arduino IDE based command for the analog read functionality is given by.
        \lstinputlisting[firstline=9,lastline=9]{\LocTHERMardcode/therm-read/therm-read.ino}
        where {\tt A4} represents the analog pin 4 to be read.
        The read value is stored in variable {\tt val} and is
        displayed using \lstinputlisting[firstline=10,lastline=10]{\LocTHERMardcode/therm-read/therm-read.ino}
        The command on next line

        \lstinputlisting[firstline=11,lastline=11]  {\LocTHERMardcode/therm-read/therm-read.ino}
        is used to put a delay of 500 milliseconds. This is to avoid very fast display of the read values. The entire reading and display operation is carried out 40 times.

        The values can be observed over the {\tt Serial Monitor} of Arduino IDE.
        The numbers displayed range from 0 to 1023. At room temperature you may get the
        output of ADC around 500. If a heating or cooling source is available,
        one can observe the increase or decrease in the ADC output. Although
        the thermistor is of NTC type, the ADC output increases with increase
        in temperature. This is because the voltage across the fixed resistor
        is sensed.

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor. Accordingly, they should observe the change in the thermistor values
        on the {\tt Serial Monitor}.

  \item In this experiment, we will turn the buzzer on depending
        on the temperature sensed by the thermistor. This experiment
        can be considered as a simple fire alarm circuit that
        detects fires based on a sudden change in temperature and
        activates the buzzer.

        The program for this is
        available at \ardref{ard:therm-buzzer}. We shall use the ADC output
        to carry this out. The buzzer is connected to pin 3, which is a
        digital output pin. The ADC output value is displayed on the serial
        monitor. At the same time, it is compared with a user-defined
        threshold, which has been set as 550 in this experiment. One may note that
        this threshold would vary according to the location and time of performing
        this experiment. Accordingly, the readers are advised to change this threshold
        in \ardref{ard:therm-buzzer}. For testing purposes, one may note the
        normal thermistor readings generated from the execution of \ardref{ard:therm-read}
        and set a threshold that is approximately 10 more than these readings.

        In this experiment, as soon as the ADC output exceeds 550, the buzzer is given a digital
        high signal, turning it on. The following lines of code perform this
        comparison and sending a {HIGH} signal to digital pin 3 on \arduino:
        \lstinputlisting[firstline=14,lastline=21]{\LocTHERMardcode/therm-buzzer/therm-buzzer.ino}
        A delay of half a second is introduced
        before the next value is read. While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like \ardref{ard:therm-read}.

\end{enumerate}

\begin{exercise}
  Carry out the following exercise:
  \begin{enumerate}
    \item Put the thermistor in the vicinity of an Ice bowl. Take care not
          to wet the shield while doing so. Note down the ADC output value for
          0$^{\circ}$Celsius.
  \end{enumerate}
\end{exercise}

\subsection{Arduino Code}
\label{sec:therm-arduino-code}
\addtocontents{ard}{\protect\addvspace{\codclr}}

\begin{ardcode}
  \acaption{Read and display the thermistor values} {Read and display
    the thermistor values.  Available at
    \LocTHERMardbrief{therm-read/therm-read.ino}.}
  \label{ard:therm-read}
  \lstinputlisting{\LocTHERMardcode/therm-read/therm-read.ino}
\end{ardcode}

\begin{ardcode}
  \acaption{Turning the buzzer on using thermistor values}
  {Turning the buzzer on using the thermistor values read by
    ADC.  Available at
    \LocTHERMardbrief{therm-buzzer/therm-buzzer.ino}.}
  \label{ard:therm-buzzer}
  \lstinputlisting{\LocTHERMardcode/therm-buzzer/therm-buzzer.ino}
\end{ardcode}

\section{Interfacing the thermistor from \scilab}
\subsection{Interfacing the thermistor}
In this section we will explain a \scilab\ script to read the thermistor
values. Based on the acquired values, we will change
the state of the buzzer.  The shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}.
The reader should go through the instructions given in
\secref{sec:sci-start} before getting started.

% The {\tt cmd\_analog\_in} command
% will be used to read from thermistor connected to an analog input
% pin. The experiments will be carried out using \scilab.

\begin{enumerate}
  \item In the first experiment, we will read the thermistor values and display it in
        \scilab\ Console. The code for this experiment is
        given in \sciref{sci:therm-read}. As explained earlier in \secref{sec:light-sci},
        we begin with serial port initialization. Then, we read the input coming from
        analog pin 4 using the following command:
        \lstinputlisting[firstline=4,lastline=4]
        {\LocTHERMscicode/therm-read.sce}
        Note that the one leg of the thermistor on
        the shield is connected to analog pin 4 of \arduino\,
        as given in \figref{fig:therm-conn}. The read value is stored in variable {\tt val} and
        displayed in the \scilab\ Console by the following command:
        \lstinputlisting[firstline=5,lastline=5]
        {\LocTHERMscicode/therm-read.sce} where {\tt val} contains
        the thermistor values ranging from 0 to 1023. The changes in
        the thermistor resistance is sensed as a voltage change between 0 to
        5V. The ADC maps the thermistor voltage readings in to values
        ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        volts. At room temperature you may get the
        output of ADC around 500. If a heating or cooling source is available,
        one can observe the increase or decrease in the ADC output. To
        encourage the user to have a good hands-on, we run these commands in
        a {\tt for} loop for 20 iterations.

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor. Accordingly, they should observe the change in the thermistor
        values on \scilab\ Console.


        % In the first experiment, \sciref{sci:therm-read} is used to read
        %   values from thermistor. First the serial port is opened using the
        %   command {\tt open\_serial} and passing the correct port number to
        %   it. The command {\tt cmd\_analog\_in} is used to read from the
        %   analog pin. The pin number is passed to this command as an
        %   argument. The read value is stored in some variable. The value is
        %   then displayed on the scilab console. A sleep of 500 millisecond is
        %   executed using the {\tt sleep} command and then the reading process
        %   is repeated 20 times by putting it in a {\tt for} loop. After the
        %   loop is finished the serial port is closed using the {\tt
        %     close\_serial} command.

  \item This experiment is an extension of the previous
        experiment. Here, we will use a \scilab\ script to
        turn a buzzer on using the thermistor values. This experiment
        can be considered as a simple fire alarm circuit that
        detects fires based on a sudden change in temperature and
        activates the buzzer.

        The program for this is available at
        \sciref{sci:therm-buzzer}.  As explained earlier,
        the ADC maps the thermistor voltage readings in to values
        ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        volts. In this experiment we compare the ADC output value with a user-defined
        threshold, which has been set as 550 in this experiment. One may note that
        this threshold would vary according to the location and time of performing
        this experiment. Accordingly, the readers are advised to change this threshold
        in \sciref{sci:therm-buzzer}. For testing purposes, one may note the
        normal thermistor readings generated from the execution of \sciref{sci:therm-read}
        and set a threshold that is approximately 10 more than these readings.

        In this experiment, as soon as the value exceeds 550, the buzzer is turned on. The following lines of code perform this
        comparison and sending a {HIGH} signal to digital pin 3 on \arduino:
        \lstinputlisting[firstline=6,lastline=10]{\LocTHERMscicode/therm-buzzer.sce}
        A delay of half a second is introduced
        before the next value is read. While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like \sciref{sci:therm-read}.
\end{enumerate}


\begin{exercise}
  Carry out the exercise below: Convert the ADC output readings to
  degree Celsius. There are two ways to do so.
  \begin{enumerate}
    \item  In the first method,
          \begin{align}
            \frac{1}{T}=A+B*\ln(R)+C*(\ln(R))^3
            \label{therm-abc}
          \end{align}
          equation \ref{therm-abc} can be used if the value of A, B, C and R are
          known. The temperature T is in kelvin and thermistor resistance R is
          in ohms. The values of A, B and C can be found out by measuring
          thermistor resistance against three known values of temperatures. The
          values of temperature must be within the operating range and should
          typically include the room temperature. Once a set of three values of
          T and R are known it will result in three equations with three
          unknowns. The values of A, B, C can be found out by solving the three
          equations simultaneously. Once the values of A, B, C are known, the
          same equation can be used to directly convert resistance to kelvin. It
          can be then converted to Celsius. This method is preferred when the
          temperature coefficient of thermistor is not known or is known very
          approximately. This method is bit cumbersome but can give accurate
          temperature conversion.

    \item In the second method,
          \begin{align}
            \frac{1}{T}=\frac{1}{T_0}+\frac{1}{\beta}*\ln\left(\frac{R}{R_0}\right)
            \label{therm-beta}
          \end{align}
          equation \ref{therm-beta} can be used if the value of $\beta$ i.e. the
          Temperature Coefficient of Resistance of the thermistor used is
          known. The value of $\beta$ can be found in the data sheet of the
          thermistor used. $R$ is the resistance of thermistor at temperature
          $T$ in kelvin.  $R_0$ is the resistance of thermistor at room
          temperature $T_0$ in kelvin.
  \end{enumerate}
\end{exercise}

\subsection{Scilab Code}
\label{sec:therm-scilab-code}
\addtocontents{cod}{\protect\addvspace{\codclr}}

\begin{scicode}
  \ccaption{Read and display the thermistor values} {Read and display
    the thermistor values.  Available at
    \LocTHERMscibrief{therm-read.sce}.}
  \label{sci:therm-read}
  \lstinputlisting{\LocTHERMscicode/therm-read.sce}
\end{scicode}

\begin{scicode}
  \ccaption{Turning the buzzer on using thermistor values}
  {Turning the buzzer on using the thermistor values read by
    ADC.  Available at \LocTHERMscibrief{therm-buzzer.sce}.}
  \label{sci:therm-buzzer}
  \lstinputlisting{\LocTHERMscicode/therm-buzzer.sce}
\end{scicode}

\section{Interfacing the thermistor from Xcos}
In this section, we discuss how to read and use the thermistor values using
Xcos blocks. The reader should go
through the instructions given in \secref{sec:xcos-start} before
getting started.

\begin{enumerate}
  \item First we will read the thermistor values and display it.  When the
        file required for this experiment is invoked, one gets the GUI as in
        \figref{fig:therm-read}.  In the caption of this figure, one
        can see where to locate the file.

        As discussed in earlier chapters, we start with the initialization
        of the serial port. Next, using {\tt Analog Read} block, we read
        the values of thermistor connected on analog pin 4.
        Next, we use a scope to plot the values
        coming from this pin. When this Xcos file is simulated,
        a plot is opened, as shown in \figref{fig:therm-read-output}.

        \begin{figure}
          \centering
          \includegraphics[width=\smfigp]{\LocTHERMfig/therm-read-xcos.png}
          \caption[Xcos diagram to read thermistor values]{Xcos diagram to
            read thermistor values.  This is what one sees when
            \LocTHERMscibrief{therm-read.zcos}, is invoked.}
          \label{fig:therm-read}
        \end{figure}

        We will next explain how to set the parameters for this simulation.
        To set value on any block, one needs to right click and open the
          {\tt Block Parameters} or double click.  The values for each block
        is tabulated in \tabref{tab:therm-read}.  All other parameters are to
        be left unchanged.
        \begin{table}
          \centering
          \caption{Xcos parameters to read thermistor}
          \label{tab:therm-read}
          \begin{tabular}{llc} \hline
            Name of the block & Parameter name             & Value     \\ \hline
            ARDUINO\_SETUP    & Identifier of Arduino Card & 1         \\
                              & Serial com port number     & 2\portcmd \\ \hline
            TIME\_SAMPLE      & Duration of acquisition(s) & 100       \\
                              & Sampling period(s)         & 0.1       \\ \hline
            ANALOG\_READ\_SB  & Analog Pin                 & 4         \\
                              & Arduino card number        & 1         \\ \hline
            CSCOPE            & Ymin                       & 200       \\
                              & Ymax                       & 600       \\
                              & Refresh period             & 100       \\ \hline
            CLOCK\_c          & Period                     & 0.1       \\
                              & Initialisation Time        & 0         \\ \hline
          \end{tabular}
        \end{table}
        \begin{figure}
          \centering
          \includegraphics[width=\smfigp]{\LocTHERMfig/therm-read.png}
          \caption{Plot window in Xcos to read thermistor values}
          \label{fig:therm-read-output}
        \end{figure}

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor. Accordingly, they should observe the change in the thermistor
        values in the output plot, as shown in \figref{fig:therm-read-output}.

  \item In the second experiment, we will switch on a buzzer
        depending on the thermistor readings (ADC output).
        When the file required for this
        experiment is invoked, one gets the GUI as in \figref{fig:therm-buzzer}.
        In the caption of this figure, one can see where to locate the file.

        \begin{figure}
          \centering
          \includegraphics[width=\lgfig]{\LocTHERMfig/therm-buzzer-xcos.png}
          \caption[Xcos diagram to read the value of thermistor, which is
            used to turn the buzzer on] {Xcos diagram to read the value
            of the thermistor, which is used to turn the buzzer on.
            This is what one sees when
            \LocTHERMscibrief{therm-buzzer.zcos}, is invoked.}
          \label{fig:therm-buzzer}
        \end{figure}

        We will next explain how to set the parameters for this simulation.
        To set value on any block, one needs to right click and open the
          {\tt Block Parameters} or double click.  The values for each block
        is tabulated in \tabref{tab:therm-buzzer}.  In the CSCOPE\_c block, the
        two values correspond to two graphs, one for digital write and other
        for analog read values. All other parameters are to be left
        unchanged. When this Xcos file is simulated, a plot is opened,
        as shown in \figref{fig:therm-buzzer-output}.

        \begin{table}
          \centering
          \caption{Xcos parameters to read thermistor and switch the buzzer}
          \label{tab:therm-buzzer}
          \begin{tabular}{llc} \hline
            Name of the block  & Parameter name             & Value     \\ \hline
            ARDUINO\_SETUP     & Identifier of Arduino Card & 1         \\
                               & Serial com port number     & 2\portcmd \\ \hline
            TIME\_SAMPLE       & Duration of acquisition(s) & 100       \\
                               & Sampling period(s)         & 0.1       \\ \hline
            ANALOG\_READ\_SB   & Analog pin                 & 4         \\
                               & Arduino card number        & 1         \\ \hline
            CMSCOPE            & Ymin                       & 0 300     \\
                               & Ymax                       & 1 600     \\
                               & Refresh period             & 100 100   \\ \hline
            CLOCK\_c           & Period                     & 0.1       \\
                               & Initialisation time        & 0         \\ \hline
            SWITCH2\_m         & Datatype                   & 1         \\
                               & threshold                  & 550       \\
                               & pass first input if field  & 0         \\
                               & use zero crossing          & 1         \\ \hline
            DIGITAL\_WRITE\_SB & Digital pin                & 3         \\
                               & Arduino card number        & 1         \\ \hline
          \end{tabular}
        \end{table}

        \begin{figure}
          \centering
          \includegraphics[width=\lgfig]{\LocTHERMfig/therm-buzzer.png}
          \caption{Plot window in Xcos to read thermistor values and the state of LED}
          \label{fig:therm-buzzer-output}
        \end{figure}

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like the Xcos file shown in
        \figref{fig:therm-read}.
\end{enumerate}


\section{Interfacing the thermistor from Python}
\subsection{Interfacing the thermistor}
In this section, we discuss how to carry out the experiments of the
previous section from Python.  We will list the same two experiments,
in the same order.  The shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}.
The reader should go through the instructions given in
\secref{sec:python-start} before getting started.

\begin{enumerate}
  \item In the first experiment, we will read the thermistor values.
        The code for this experiment is given in \pyref{py:therm-read}.
        As explained earlier in \secref{sec:light-py}, we begin with
        importing necessary modules followed by setting up the serial port.
        Then, we read the input coming from analog pin 4 using the
        following command:
        \lstinputlisting[firstline=26,lastline=26]
        {\LocTHERMpycode/therm-read.py} Note that the one leg of the thermistor on
        the shield is connected to analog pin 4 of \arduino\,
        as given in \figref{fig:therm-conn}. The read value is displayed
        by the following command:
        \lstinputlisting[firstline=27,lastline=27]
        {\LocTHERMpycode/therm-read.py} where {\tt val} contains
        the thermistor values ranging from 0 to 1023. To
        encourage the user to have a good hands-on, we run these commands in
        a {\tt for} loop for 20 iterations.

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe the change in values being printed on on the
        Command Prompt (on Windows) or Terminal (on Linux), as the case maybe.

        % In the first experiment, \pyref{py:therm-read} is used to read
        % values from thermistor. First the serial port is opened using the
        % command {\tt open\_serial} and passing the correct port number to
        % it. The command {\tt cmd\_analog\_in} is used to read from the
        % analog pin. The pin number is passed to this command as an
        % argument. The read value is stored in some variable. The value is
        % then displayed on the scilab console. A sleep of 500 millisecond is
        % executed using the {\tt sleep} command and then the reading process
        % is repeated 20 times by putting it in a {\tt for} loop. After the
        % loop is finished the serial port is closed using the {\tt
        %     close\_serial} command.

  \item This experiment is an extension of the previous
        experiment. Here, we will use a Python script to
        turn a buzzer on using the thermistor values. This experiment
        can be considered as a simple fire alarm circuit that
        detects fires based on a sudden change in temperature and
        activates the buzzer.

        The program for this is available at
        \pyref{py:therm-buzzer}.  As explained earlier,
        the ADC maps the thermistor voltage readings in to values
        ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        volts. In this experiment we compare the ADC output value with a user-defined
        threshold, which has been set as 550 in this experiment. One may note that
        this threshold would vary according to the location and time of performing
        this experiment. Accordingly, the readers are advised to change this threshold
        in \pyref{py:therm-buzzer}. For testing purposes, one may note the
        normal thermistor readings generated from the execution of \pyref{sci:therm-read}
        and set a threshold that is approximately 10 more than these readings.

        In this experiment we compare the ADC output value with 550
        and as soon as the value exceeds 550 the buzzer is turned on. The following lines of code perform this
        comparison and sending a {HIGH} signal to digital pin 3 on \arduino:
        \lstinputlisting[firstline=30,lastline=34]{\LocTHERMpycode/therm-buzzer.py}
        A delay of half a second is introduced
        before the next value is read. While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like \pyref{py:therm-read}.

        % In the second experiment, we will use the python script to
        %       turn a buzzer on and off using the thermistor values. The changes in
        %       the thermistor resistance is sensed as a voltage change between 0 to
        %       5V. The ADC maps the thermistor voltage readings in to values
        %       ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        %       volts. In this experiment we compare the ADC output value with 550
        %       and as soon as the value exceeds 550 the buzzer is turned on. See
        %       \sciref{py:therm-buzzer} for the complete code. We use {\tt if}
        %       loop to achieve this functionality. Command {\tt cmd\_digital\_out}
        %       is used to turn the buzzer on and off.  The correct port number on
        %       which the buzzer is connected is passed to this command as an
        %       argument. The entire process is repeated 500 times.
\end{enumerate}

\subsection{Python Code}
\label{sec:therm-pyhton-code}
\addtocontents{pyd}{\protect\addvspace{\codclr}}

\begin{pycode}
  \pcaption{Read and display the thermistor values} {Read and display
    the thermistor values.  Available at
    \LocTHERMpybrief{therm-read.py}.}
  \label{py:therm-read}
  \lstinputlisting{\LocTHERMpycode/therm-read.py}
\end{pycode}

\begin{pycode}
  \pcaption{Turning the buzzer on using thermistor values}
  {Turning the buzzer on using the thermistor values read by
    ADC.  Available at \LocTHERMpybrief{therm-buzzer.py}.}
  \label{py:therm-buzzer}
  \lstinputlisting{\LocTHERMpycode/therm-buzzer.py}
\end{pycode}

\section{Interfacing the thermistor from Julia}
\subsection{Interfacing the thermistor}
In this section, we discuss how to carry out the experiments of the
previous section from Julia.  We will list the same two experiments,
in the same order.  The shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}.
The reader should go through the instructions given in \secref{sec:julia-start} before getting started.


\begin{enumerate}
  \item In the first experiment, we will read the thermistor values.
        The code for this experiment is given in
        \juliaref{julia:therm-read}. As explained earlier in \secref{sec:light-julia}, we begin with importing the SerialPorts
        \cite{julia-serial-ports} package and the module ArduinoTools followed by setting up the serial port.
        Then, we read the input coming from analog pin 4 using the
        following command:
        \lstinputlisting[firstline=7,lastline=7]
        {\LocTHERMjuliacode/therm-read.jl} Note that the one leg of the thermistor on
        the shield is connected to analog pin 4 of \arduino\,
        as given in \figref{fig:therm-conn}. The read value is displayed
        by the following command:
        \lstinputlisting[firstline=8,lastline=8]
        {\LocTHERMjuliacode/therm-read.jl} where {\tt val} contains
        the thermistor values ranging from 0 to 1023. To
        encourage the user to have a good hands-on, we run these commands in
        a {\tt for} loop for 20 iterations.

        While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe the change in values being printed on on the
        Command Prompt (on Windows) or Terminal (on Linux), as the case maybe.

  \item This experiment is an extension of the previous
        experiment. Here, we will use a Julia source file to
        turn a buzzer on using the thermistor values. This experiment
        can be considered as a simple fire alarm circuit that
        detects fires based on a sudden change in temperature and
        activates the buzzer.

        The program for this is available at
        \juliaref{julia:therm-buzzer}.  As explained earlier,
        the ADC maps the thermistor voltage readings in to values
        ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        volts. In this experiment we compare the ADC output value with a user-defined
        threshold, which has been set as 550 in this experiment. One may note that
        this threshold would vary according to the location and time of performing
        this experiment. Accordingly, the readers are advised to change this threshold
        in \juliaref{julia:therm-buzzer}. For testing purposes, one may note the
        normal thermistor readings generated from the execution of \juliaref{julia:therm-read}
        and set a threshold that is approximately 10 more than these readings.


        In this experiment we compare the ADC output value with 550
        and as soon as the value exceeds 550 the buzzer is turned on. The following lines of code perform this
        comparison and sending a {HIGH} signal to digital pin 3 on \arduino:
        \lstinputlisting[firstline=9,lastline=13]{\LocTHERMjuliacode/therm-buzzer.jl}
        A delay of half a second is introduced
        before the next value is read. While running this experiment,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like \juliaref{julia:therm-read}.

\end{enumerate}

\subsection{Julia Code}
\label{sec:therm-julia-code}
\addtocontents{juliad}{\protect\addvspace{\codclr}}

\begin{juliacode}
  \jcaption{Read and display the thermistor values} {Read and display
    the thermistor values. Available at
    \LocTHERMjuliabrief{therm-read.jl}.}
  \label{julia:therm-read}
  \lstinputlisting{\LocTHERMjuliacode/therm-read.jl}
\end{juliacode}

\begin{juliacode}
  \jcaption{Turning the buzzer on using thermistor values}
  {Turning the buzzer on using the thermistor values read by
    ADC.  Available at \LocTHERMjuliabrief{therm-buzzer.jl}.}
  \label{julia:therm-buzzer}
  \lstinputlisting{\LocTHERMjuliacode/therm-buzzer.jl}
\end{juliacode}

\section{Interfacing the thermistor from OpenModelica}
\subsection{Interfacing the thermistor}
In this section, we discuss how to carry out the experiments of the
previous section from OpenModelica.  We will list the same two experiments,
in the same order.  The shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}.
The reader should go through the instructions given in
\secref{sec:OpenModelica-start} before getting started.

\begin{enumerate}
  \item In the first experiment, we will read the thermistor values. The code for this experiment is given in
        \OpenModelicaref{OpenModelica:therm-read}. As explained earlier in \secref{sec:light-OpenModelica},
        we begin with importing the two packages: Streams and SerialCommunication followed
        by setting up the serial port. Then, we read the input coming from analog pin 4 using the
        following command:
        \lstinputlisting[firstline=16,lastline=16]
        {\LocTHERMOpenModelicacode/therm-read.mo} Note that the one leg of the thermistor on
        the shield is connected to analog pin 4 of \arduino\,
        as given in \figref{fig:therm-conn}. The read value is displayed
        by the following command:
        \lstinputlisting[firstline=17,lastline=17]
        {\LocTHERMOpenModelicacode/therm-read.mo} where {\tt val} contains
        the thermistor values ranging from 0 to 1023.

        While simulating this model,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe the change in values being printed on on the output window of OMEdit, as shown in \figref{om-sim-success}.

  \item This experiment is an extension of the previous experiment. Here,
        we will turn a buzzer on using the thermistor values. This experiment
        can be considered as a simple fire alarm circuit that
        detects fires based on a sudden change in temperature and
        activates the buzzer.

        The program for this is available at
        \OpenModelicaref{OpenModelica:therm-buzzer}.  As explained earlier,
        the ADC maps the thermistor voltage readings in to values
        ranging from 0 to 1023. This means 0 for 0 volts and 1023 for 5
        volts. In this experiment we compare the ADC output value with a user-defined
        threshold, which has been set as 550 in this experiment. One may note that
        this threshold would vary according to the location and time of performing
        this experiment. Accordingly, the readers are advised to change this threshold
        in \OpenModelicaref{OpenModelica:therm-buzzer}. For testing purposes, one may note the
        normal thermistor readings generated from the execution of \OpenModelicaref{OpenModelica:therm-read}
        and set a threshold that is approximately 10 more than these readings.

        In this experiment we compare the ADC output value with 550
        and as soon as the value exceeds 550 the buzzer is turned on. The following lines of code perform this
        comparison and sending a {HIGH} signal to digital pin 3 on \arduino:
        \lstinputlisting[firstline=19,lastline=23]{\LocTHERMOpenModelicacode/therm-buzzer.mo}
        A delay of 500 milliseconds is introduced
        before the next value is read. While simulating this model,
        the readers should try holding (or rubbing) the thermistor with their fingertips.
        Doing so will transfer heat from the person holding the
        thermistor, thereby raising the temperature of the thermistor.
        Accordingly, they should observe whether the threshold of 550 is achieved
        and the buzzer is enabled.

        \paragraph{Note:} Once the thermistor value reaches 550 (the threshold), the value will remain the same
        (unless it is cooled). Therefore, the buzzer will continuously produce the sound, which might be
        a bit annoying. To get rid of this, the readers are advised to
        execute some other code on \arduino\ like \OpenModelicaref{OpenModelica:therm-read}.

\end{enumerate}

\subsection{OpenModelica Code}
Unlike other code files, the code/ model for running experiments using OpenModelica are
available inside the OpenModelica-Arduino toolbox, as explained in \secref{sec:load-om-toolbox}.
Please refer to \figref{om-examples-toolbox} to know how to locate the experiments.

\label{sec:therm-OpenModelica-code}
\addtocontents{OpenModelicad}{\protect\addvspace{\codclr}}

\begin{OpenModelicacode}
  \mcaption{Read and display the thermistor values} {Read and display
    the thermistor values.  Available at
    Arduino -> SerialCommunication ->
    Examples -> push -> therm\_read.}
  \label{OpenModelica:therm-read}
  \lstinputlisting{\LocTHERMOpenModelicacode/therm-read.mo}
\end{OpenModelicacode}

\begin{OpenModelicacode}
  \mcaption{Turning the buzzer on using thermistor values}
  {Turning the buzzer on using the thermistor values read by
    ADC.  Available at
    Arduino -> SerialCommunication ->
    Examples -> push -> therm\_buzzer.}
  \label{OpenModelica:therm-buzzer}
  \lstinputlisting{\LocTHERMOpenModelicacode/therm-buzzer.mo}
\end{OpenModelicacode}
