\documentclass[a4paper,12pt]{article}

\usepackage[backend=bibtex]{biblatex} 
\usepackage{geometry} 
\usepackage{titling} 
\usepackage{titlesec}
\usepackage[english]{babel} 
\usepackage[hidelinks]{hyperref} 
\usepackage{listings} 
\usepackage{xcolor}
\usepackage{graphicx} 
\usepackage{forest} 
\usepackage{tikz-qtree} 
\usepackage[siunitx, european, straightvoltages, cute inductors]{circuitikz} 
\usepackage{setspace} 
\usepackage{ragged2e}
\usepackage{graphicx}
\graphicspath{ {./images/} }

\addbibresource{ref.bib}

\definecolor{codegreen}{rgb}{0,0.6,0} 
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82} 
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
	backgroundcolor=\color{backcolour}, 
	commentstyle=\color{codegreen}, 
	keywordstyle=\color{magenta},
	numberstyle=\tiny\color{codegray}, 
	stringstyle=\color{codepurple}, 
	basicstyle=\ttfamily\footnotesize,
	breakatwhitespace=false, 
	breaklines=true, 
	captionpos=b, 
	keepspaces=true, 
	numbers=left, 
	numbersep=5pt,
	showspaces=false, 
	showstringspaces=false, 
	showtabs=false, 
	tabsize=8
}
\lstset{style=mystyle}

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=0cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30]
\tikzstyle{subroutine} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=yellow!30, double distance=1]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow} = [thick,->,>=stealth]


\titleformat{\section} {\Huge} {} {0em} {}[\titlerule] 
\geometry{a4paper,total={170mm,257mm},left=25mm,right=25mm,}

\author{Lucas Standen} 
\title{Creating a simple temprature sensing circuit}

\begin{document} 
\maketitle

\newpage
\tableofcontents 
\newpage

\setlength{\parskip}{1em}

{\setlength{\parindent}{0cm}
	\section{System Planning} 
	\subsection{Problem analysis} 
	My circuit will sense temperature, and will be taking into consideration pet owners, worried about their homes over-heating 
	for their pets, this will be especially helpful for owners of sensitive pets such as fish. People who own these pets often 
	leave them at home alone, which can be deadly on summer days, my device plans to alert the owner, and can be attached to other 
	systems such as a cooling system.

	My system, will flash an LED and pulse a buzzer to make it clear that it is too hot, have an indicator to tell
	the user that something has gone wrong, and have a pin to free to attach to an external system.  It will have a
	adjustment dial to change the threshold, so the user can specify what temperature is too hot.

	\subsection{Who is it for?} 
	My project will be used by pet owners, focussing on fish, to keep the tank at the
	correct temperature.  This is a broad range of people as many people own fish\cite{FISH}. Many fish die due to
	their tanks getting too hot, especially in the summer, my project is perfect for these fish owners.

	\section{Design specification} 
	\subsection{Design brief}
	This project will need to achieve a few goals to be deemed as a success, they are as follows:
	\begin{description}
		\item[] Display a status LED to tell the user all is working as expected
		\item[] Check the temperature against a known value
		\item[] Have the ability to notify the user if the temperature is above the known value
		\item[] Have the ability to inform the user if the temperature has risen above the known value and then fallen again
		\item[] To have the system reset easily
	\end{description}
	If my project can meet all of these goals it will be a useable temperature sensing system, primarily designed for fish tanks,
	but could be used in any number of temperature dependant environments.
	\subsection{System Design} 
	\subsubsection{Specific ideal values}
	For my system, there will be ideal values that id like to tune for, so my circuit switches on at the correct temperatures, 
	and for the correct amount of time.
	\begin{center}
		\begin{description}
			\item[Temprature that it should turn on at] It should switch at approximately 
				\textbf{\({26}^\circ C \)} 
				with a \textbf{5\%} tolerance. This is just the value I will tune it too, in reality the user could set any value
				they like.
			\item[Time it should be on for] It should switch on for \textbf{10} seconds, pulsing from on to off. With it high to low
				ratio of \textbf{1:1}, so its on for 1 to second and off for another.
		\end{description}
	\end{center}
	\subsubsection{Outline of internal workings}
	The project will need to do the following things internally to function as outlined in the design brief. These are tasks that 
	will be ran on the microcontroller by the assembly code I need to write.
	\begin{description}
		\item[] Read the temperature
		\item[] Compare the temperature to a known value
		\item[] The output is a flashing led and buzzer
		\item[] The output is a flashing led and buzzer
	\end{description}
	
	\subsubsection{Outline of component roles}
	My system will contain the following components to function scouring to my outline of internal workings: 
	\begin{description}
		\item[Mircocontroller] This will be used to control all the other components \item[Thermistor] This will
			sense the temperature 
		\item[Potentiomiter] This will set the activation threshold 
		\item[Red, Green and Amber LED's] These will indicate the state of the device 
		\item[Buzzer] This will indicate that it is too
			hot 
		\item[Button] This will reset the device
	\end{description} 
	With these components I will make a circuit that can be used to sense and warn a user about
	high temperatures. The design will revolve around the micro controller, with everything else coming off it as a
	sub system like so:

	\begin{center} 
		\begin{tikzpicture} 
			\tikzset{edge from parent/.style={draw,edge from parent path={(\tikzparentnode.south)-- +(0,-8pt)-| (\tikzchildnode)}}} 
			\Tree
			[.Button
			[.Microcontroller
			[.Inputs
			[.Thermistor ] [.Potentiomiter ]
			] [.Outputs
			[.LED(green) ] [.LED(red) ] [.LED(amber) ] [.Buzzer ]
			]
			]
			]
		\end{tikzpicture} 
	\end{center}

	As one can see a button will control the Microcontroller, by drawing all the current that the power supply
	can through the button, one can make the Microcontroller reset. The Microcontroller will have 2 inputs, and
	4 outputs. The potentiometer will be used to set the threshold in which the warnings begin, this will be done
	inside the microcontroller, with a subtraction between the Thermistor value, and the potentiometer value. The
	needed outputs will pulse to be especially clear that something is wrong.

	\subsection{Planning the inner workings}
	\subsubsection{Flowchart}
	Here is my code, built into an abstracted flow chart, to make the reading of the program easier. 
	It is spread across 2 pages, to ensure it is big enough to read.

	\begin{tikzpicture}[node distance=2cm]
		\node (start) [startstop] {Start};
		\node (in1) [io, below of=start] {Read temperature};
		\node (in2) [io, below of=in1] {Read threshold value};
		\node (dec1) [decision, below of=in2, yshift=-2cm] {Is the temperature too hot?};
		\node (sub1) [subroutine, right of=dec1, xshift=6cm] {Flash};
		\node (proc1) [process, below of=dec1, yshift=-2cm, xshift=6cm] {Reset the status flag};


		\draw [arrow] (start) -- (in1);
		\draw [arrow] (in1) -- (in2);
		\draw [arrow] (in2) -- (dec1);
		\draw [arrow] (dec1) -- node[anchor=north] {Yes} (sub1);
		\draw [arrow] (sub1) |- (start);
		\draw [arrow] (dec1) |- node[anchor=east] {No} (proc1); 
		\draw [arrow] (proc1) |- (start);
	\end{tikzpicture}
	\newpage
	\begin{tikzpicture}[node distance=2cm]
		\node (flash) [subroutine, below of=dec1, yshift=-4cm] {Flash};
		\node (proc2) [process, below of=flash] {Set counter to 5};
		\node (out1) [io, below of=proc2] {Set LED and buzzer on};
		\node (proc3) [process, below of=out1] {Wait 1 second};
		\node (proc4) [process, below of=proc3] {Decrement 1 from the counter};
		\node (dec2) [decision, below of=proc4, yshift=-1cm] {Is counter == 0};
		\node (out3) [io, below of=dec2, yshift=-1cm] {Set LED and buzzer off};
		\node (return) [subroutine, below of=out3] {Return};
		\node (out2) [io, right of=dec2, xshift=6cm] {Set LED and buzzer off};
		\node (proc5) [process, above of=out2] {Wait 1 second};

		\draw [arrow] (flash) -- (proc2);
		\draw [arrow] (proc2) -- (out1);
		\draw [arrow] (out1) -- (proc3);
		\draw [arrow] (proc3) -- (proc4);
		\draw [arrow] (proc4) -- (dec2);
		\draw [arrow] (dec2) -- node[anchor=north] {No} (out2);
		\draw [arrow] (dec2) -- node[anchor=east] {Yes} (out3);
		\draw [arrow] (out3) -- (return);
		\draw [arrow] (out2) -- (proc5);
		\draw [arrow] (proc5) |- (out1);
	\end{tikzpicture}



	\subsubsection{How will it function outside of the microcontroller?} 
	Bellow is the diagram for my circuit, it works mostly via the code on the
	micro controller, so this is just connecting things between live and the microcontroller.   
	\begin{flushleft}
		\begin{circuitikz}
			\draw (-8,5) to[short,o-o] (8,5){}; % power rail 
			\draw (0,5) node[vcc]{5V};

			\draw (-8,-6) to[short,o-o] (8,-6){}; % ground rail 
			\draw (0,-6) node[ground]{};

			\draw (0,3) to[short,o-] (7,3){}; % push button 
			\draw (7,3) to[push button,-o] (7,-6){};

			\ctikzset{multipoles/thickness=4} 
			\ctikzset{multipoles/external pins thickness=2} 
			\draw (0,0)node[dipchip,
				num pins=18, external pins width=0.3, 
				external pad fraction=3, 
				scale=1.8, 
				rotate=90](Micro){
				\rotatebox{-90}{PICAXE 18m2}}; % micro controller

			\draw (-7, 5) to[thermistor,a=\tiny{100K},o-o] (-7,0){}; % thermistor 
			\draw (-7, 0) to[resistor,a=\tiny{100K},o-o] (-7,-6){}; % thermistor divider resistor

			\draw (-7, 0) to[short, o-] (-6,0){}; %thermistor divider wire 
			\draw (-6, 0) to[short, -] (-6,3){}; 
			\draw(-6, 3) to[short, -] (-4, 3){}; 
			\draw (-4, 3) to[short, -] (Micro.pin 18){};

			\draw (-8, 5) to[potentiometer, a=\tiny{10K}, -] (-8, -2){}; 
			\draw (-8, -2) to[short, -] (-8, -6){};

			\draw (-7.5, 1.5) to[short, o-] (-5, 1.5){}; % potentiometer wire 
			\draw (-5, 1.5) to[short,-] (-5, 4){};
			\draw (-5, 4) to[short,-] (-3, 4){}; 
			\draw (-3, 4) to[short, -] (Micro.pin 17){};


			\draw (Micro.pin 14) to[short,-o] (0,5){}; %microcontroller live 
			\draw (Micro.pin 5) to[short,-o](0,-6){}; %microcontroller ground

			\draw (Micro.pin 6) to[empty led] (1, -4){}; %output red 
			\draw (Micro.pin 7) to[buzzer] (2, -6){}; %output buzzer 
			\draw (Micro.pin 8) to[empty led] (3, -4){}; %output amber 
			\draw (Micro.pin 9) to[empty led] (4, -4){}; %output green

			\draw (1,-4) to[resistor,-o,a=\tiny{220}] (1,-6){}; % output resistor 
			\draw (3,-4) to[resistor,-o,a=\tiny{220}] (3,-6){}; % output resistor 
			\draw (4,-4) to[resistor,-o,a=\tiny{220}] (4,-6){}; % output resistor
		\end{circuitikz} 
	\end{flushleft} 
	The way this works is the potential divider on the
	left feeds into the micro controller which performs a comparison between it and the potentiometer, using the ADC
	pins on the pic chip. The button seen on the right is being used as a reset switch, for a short time, it can
	cut short circuit the system, cutting power to the microcontroller, effectively acting as a reset switch. The
	outputs at the bottom are in order; a red LED that flashes when the circuit detects it is too hot; a buzzer that
	flashes at the same time; an amber LED that turns on after the flashing has stopped to inform the user that it
	was too hot at some point; and a green status LED to inform the user that all is working.  

	\subsubsection{The code controlling everything else}
	Bellow is the code for the micro controller. It is 58 lines long and commented. It contains 12 unique instructions.
	\lstinputlisting[]{./final.asm} 
	This code starts with an initialisation section, that sets the micro controller's input and output pins to do
	the correct things. Then it defines a subroutine that flashes the LED and buzzer and sets the status led. And
	finally the main function runs in a loop to continue checking if it is too hot. 
	\subsubsection{Errors in code}
	Through out the development of project, a few errors came up in my code, I fixed them, however here is a documented
	list of them
	\subsubsection{Forgetting to specify the output register}
	\lstinputlisting[]{./final.1.err} 
	this error was fixed by adding ", w" to the end of the line (before the comment), as it had been missing an argument 
	before this point.
	\subsubsection{Bit test file, skip if set or skip if clear}
	While this didn't stop my code from compiling, it did stop the code from working properly, with it triggering when it
	was the right temperature, and stopped when it was too hot. It was a simple fix however, all that needed to change
	was to change a C to an S.

	\section{System Realisation} 
	After designing and writing the functionality, code, and the circuit it was time to build it. Here is the finished results.
	\subsection{Circuit realisation}

	\includegraphics[width=\textwidth]{off.jpg.jpeg}	

	Here is my finished design prototyped on a bread board, I have cut the wires to an adequate length to ensure it is cleanly made.
	I left the potential divider uncut, as I changed what value components I was using many times throughout building. In this photo
	an external (off breadboard) thermistor is being used.

	I decided to use a multi-turn potentiometer instead of a traditional one, this allowed me and the end user, to calibrate the
	system to far smaller amounts.

	\subsection{Calibrating the sensors and testing the system} 
	To calibrate my system I used water at a known temperature and a thermometer to measure. I then tuned my potentiometer to the adequate 
	value to ensure the circuit functioned as intended.

	Here is the circuit at a normal state with water at \({20}^\circ C \).
	\\
	\\
	\includegraphics[width=\textwidth]{normal.jpg.jpeg}	
	\\
	As one can see the green LED is on at the correct time!
	\\
	\includegraphics[width=\textwidth]{offtemp.jpg.jpeg}	
	\\
	After adding more hot water one can see it switch to the higher point.
	\\
	\includegraphics[width=\textwidth]{hotLED.jpg.jpeg}	
	\\
	As the temperature rises above the \({26}^\circ C \) switching threshold the red LED begins to blink.
	\\
	\includegraphics[width=\textwidth]{hightemp.jpg.jpeg}	
	\\
	\includegraphics[width=\textwidth]{formerhot.jpg.jpeg}	
	\\
	As one can see after it falls back to a low temperature the amber status LED comes on to let the user know
	it has been too hot.
	\\
	\includegraphics[width=\textwidth]{lowtemp.jpg.jpeg}	
	\\
	This temperature caused the amber LED to come on after being at \({26}^\circ C \) previously.
	\\
	\subsection{Results}
	As shown in the previous sections the circuit has worked as intended, it switches at a user defined temperature, which for testing 
	purposes I have set to \({26}^\circ C \). The tuning process is easy and simple to work with, only requiring the turning of a potentiometer.
	This could be set out of box in a production setting, or it could be tweaked by the use to their needs.

	As intended It triggered at \({26}^\circ C \), in reality it was probably slightly out but not to a noticeable margin.

	It also was triggered for the full 10 seconds as planned, with everything being accurate enough to not have any noticeable errors/inconsistencies.

	\section{System Evaluation} 
	\subsection{What could go better?}
	To make this system better I would have liked to make it test temperatures using a difference amplifier to compare to another known temperature as this
	would have let me compare to room temperature, instead of set constants, this would be more helpful if the use case was slightly changed, to show relative
	temperature, which would be helpful in a factory to show temperatures next to a machine. This however was out of the scope of this project, as I was
	attempting to use a microcontroller to do all comparisons, a difference amplifier would have removed the need for the microcontroller, making the project 
	irrelevant.

	I also would have liked it if I could have added more features to the code, perhaps in some way adding an interrupt service to allow for further user control.
	Perhaps as a simple clear amber status LED.

	Finally I was not overly happy with my solution to resetting the microcontroller, I chose to, for a short time, short circuit the power rails of my bread board, 
	to cut power from the microcontroller, if I were to make any edits to this system I would have replaced this with a simple MOSFET system that could keep it 
	powered until a button was pressed. This sadly wasn't possible due to time constraints on this project. This is also caused a flaw in the 18M2 picaxe chip. The
	original 16F88 has a reset pin, that could have been used to clear the microcontroller's state , this isn't available on the 18M2 chip.

	\subsection{Conclusions}
	Overall, including short comings, I am happy with my system. It completed my design brief of a simple temperature sensing system in a simple way. I believe this 
	could be used for its goal of triggering alarms when fish tanks of other similar items, reach an unsafe temperature.

	\newpage

	\printbibliography 
} 
\end{document}
