summaryrefslogtreecommitdiff
path: root/user-code/push/pushbutton_11.tex
blob: 27dfb0e823a9b21b9435da1a5a6a5f7235f937ad (plain)
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
\chapter{Interfacing a Pushbutton}
\thispagestyle{empty}
\label{pushbutton}

\newcommand{\LocPushfig}{\Origin/user-code/push/figures}
\newcommand{\LocPushscicode}{\Origin/user-code/push/scilab}
\newcommand{\LocPushscibrief}{Orign/user-code/push/scilab}
\newcommand{\LocPushardcode}{\Origin/user-code/push/arduino}
\newcommand{\LocPushardbrief}{Orign/user-code/push/arduino}

\newcommand{\LocPushbrief}{Orign/user-code/push/code}

In this experiment we will learn about digital input in arduino. We will read the status of pushbutton switch connected to Arduino using scilab-arduino toolbox. This status can be used  to perform different operations. Monitoring status using digital logic is very basic and important task in many applications. This experiment will enable you to do the same. 

\section{Preliminaries}
Pushbutton is a simple switch which is used to connect/disconnect circuit. When push-button interfaced with microcontroller circuit is pressed, it changes logic level of the micro-controller pin connected to its terminal. 

After pressing the button two terminals get electrically connected (short) and thus current flows through the pushbutton. As you might expect, there is limit to maximum current that could flow through pushbutton. This maximum current is also called as rated current and is provided by manufacturer in the datasheet.

\section{Reading a Pushbutton from the Arduino IDE}

\subsection{Reading values from Arduino through Scilab}
In this section we will see how arduino conveys digital value to the scilab software installed in the computer. This is done by Scilab-arduino toolbox we are using. But at root level, digital data transfer between Arduino and Scilab happens because of RS232 serial communication.  

\subsubsection{RS232 serial communication}
Serial communication is wired communication standard where 2 computing units talk with each other using two wires. RS232 is one of the widely used standard of  data transfer. In this, data transfer over each wire takes place sequentially with 1 bit at a time. Each unit has separate receive and transmit connection, thus it s full duplex mode of communication. All Arduino boards have special hardware called universal asynchronous receiver transmitter (UART) to enable fast RS232 serial communication. RS232 serial transmit (Tx) and serial receive (Rx) connections are provided at digital pin 1 and 0 respectively. With the serial-USB adapter, this serial port communicates with computer with scilab installed. Scilab-arduino toolbox uses this serial port in the backend. Some of the Arduino variants like arduino mega provide multiple inbuilt serial ports. 

To start a serial communication few configuration parameters need to be set. They must be same for both the units involved in communication. Parameters needed for serial communication are Baud rate, start bit, stop bit, parity type, etc.

\subsection{Connections}
Pushbutton is connected  to the digital pin 4 of arduino using shield given in kit. It has 2 pair of terminals. Each pair is electrically connected. When pushbutton is pressed all the terminals become short.  

\subsection{Block diagram}

\section{Experiment: Reading digital input}
In this experiment we will read the pushbutton status in scilab console. We have to write a cmd\_digital\_read function. The general form of this function is given as:
\begin{lstlisting}
ans=cmd_digital_in(1, pin)
\end{lstlisting}


\section{Reading a Pushbutton from the Arduino IDE}
\subsection{Scilab script}



\subsection{Xcos implementation}
The Xcos implementation for this experiment is shown in the \figref{pushbuttonstat}.

\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocPushfig/push-button-status.png}
\caption{Xcos diagram}
\label{fig:pushbuttonstat}
\end{figure}

\subsubsection{Output}
Using serial connection between Arduino and scilab,  digital status of the pushbutton is observed on scilab console. In xcos, graphical plot of the same can be observed. When user presses the button, change in the logic value from low to high can be observed. 


\section{Experiment: Controlling LED using pushbutton}
In this experiment, we will learn to switch on/off LED using pushbutton. Here both the peripherals LED and button works with digital logic. Thus LED digital output is decided by the digital input received from pushbutton. Scilab code for direct implementation in scilab-arduino toolbox is mentioned below.

\subsection{Scilab script}



\subsection{Xcos implementation}
The Xcos implementation for this experiment is shown in the \figref{pushbuttonstat}.

\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocPushfig/led-push-button.png}
\caption{Xcos diagram}
\label{fig:ledpushbutton}
\end{figure}


\subsubsection{Output}
Each time user presses button, LED on the shield is switched on. When button is released LED is switched off again. Here we note that digital logic level of the Arduino pin connected to pushbutton changes only for the time button is being pressed. 


\subsubsection{Troubleshooting}
You can check whether pushbutton is working correctly or not by checking connections. If pushbutton is working correctly, all the 4 terminals show electrical short. You can check this with digital multimeter (DMM). When pushbutton is released 2 pair of terminals are not connected to the other 2 terminals on the other side. However, each pair is still shorted.


\subsection{Exercise}

\section{Arduino Code}
\lstset{style=mystyle}
\label{sec:push-arduino-code}
\addtocontents{cod}{\protect\addvspace{\codclr}}

\begin{ardcode}
\ccaption{Turning on the blue LED}{Turning on the blue LED.  Available
  at \LocPushardbrief/push-button-status/push-button-status.ino}
\label{ard:push-status}
\lstinputlisting{\LocPushardcode/push-button-status/push-button-status.ino}
\end{ardcode}

\begin{ardcode}
\ccaption{Turning on the blue LED}{Turning on the blue LED.  Available
  at \LocPushardbrief/led-push-button/led-push-button.ino}
\label{ard:led-blue}
\lstinputlisting{\LocPushardcode/led-push-button/led-push-button.ino}
\end{ardcode}



\section{Scilab Code}
\label{sec:push-scilab-code}
\addtocontents{cod}{\protect\addvspace{\codclr}}
\begin{scicode}
\ccaption{Interfacing a Pushbutton}
{Read and display the LDR values.  Available at
  \LocPushscibrief/push-button-status.sce.} 
\label{sci:push-100}
\lstinputlisting{\LocPushscicode/push-button-status.sce}
\end{scicode}

\begin{scicode}
\ccaption{Interfacing a Pushbutton}
{Change the LED state depending on the LDR values. Available at
  \LocPushscibrief/led-push-button.sce.} 
\label{sci:push-200}
\lstinputlisting{\LocPushscicode/led-push-button.sce}
\end{scicode}