291 lines
6.9 KiB
Markdown
291 lines
6.9 KiB
Markdown
---
|
|
author: Fred Cahill, Shawn Nock
|
|
institute: Unlondon Digital Media Assoc.
|
|
title: Arduino for the Arts
|
|
lang: en-CA
|
|
colorlinks: true
|
|
...
|
|
|
|
# What's in your kit?
|
|
|
|
## Kit Contents
|
|
|
|
- Arduino Uno R3 Clone
|
|
- Solderless Breadboard
|
|
- Connecting wires
|
|
- LEDs
|
|
- Resistors, Potentiometer
|
|
- Buzzer
|
|
- IR Remote
|
|
- IR Receiver
|
|
|
|
## What is Arduino?
|
|
|
|
\begin{center}
|
|
$\mu$C + reset button + led + USB communication
|
|
\end{center}
|
|
|
|
It's a kit (on a board) with the bare minimum components to easily use the $\mu$C
|
|
hardware. They do the basic, boring design needed for any board, so users only
|
|
need to add the neat stuff.
|
|
|
|
## Arduino UNO
|
|
|
|
The Arduino variety that we are using is the Arduino UNO.
|
|
|
|
- Processor: Atmel Atmega328p
|
|
- Memory: 2K RAM + 32K Flash
|
|
- FT232RL Logic-level Serial$\leftrightarrow$USB Chip
|
|
|
|
## Arduino Software
|
|
|
|
The Arduino folks also adapted an *Integrated Development Environment*
|
|
(IDE) to their boards. This IDE allows users to easily write programs
|
|
for their boards and then write the programs to the $\mu$C.
|
|
|
|
\Large Get the Arduino IDE: [https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software)
|
|
|
|
# Circuit Basics
|
|
|
|
## Diode
|
|
|
|
\begin{columns}[c]
|
|
\column{0.50\textwidth}
|
|
|
|
\begin{itemize}
|
|
\item One way value for current\footnotemark[1]
|
|
\item LED $\equiv$ Light Emitting Diode
|
|
\item Band marks (-)\footnotemark[2]
|
|
\item Longer leg marks (+)
|
|
\end{itemize}
|
|
|
|
\column{0.50\textwidth}
|
|
\begin{center}
|
|
\includegraphics[width=0.75\textwidth]{images/diode.png}
|
|
\vspace{5mm}
|
|
\includegraphics[width=0.50\textwidth]{images/led.jpg}
|
|
\end{center}
|
|
\end{columns}
|
|
|
|
\footnotetext[1]{\tiny \url{https://learn.sparkfun.com/tutorials/diodes}}
|
|
\footnotetext[2]{\tiny \url{https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity}}
|
|
|
|
## Diode Problems
|
|
|
|
* Diodes don't limit current
|
|
* Diodes aren't perfect (some current turned to heat)
|
|
* Too much current = Too much heat = __BANG__
|
|
* How do we limit current?
|
|
|
|
## Resistor
|
|
|
|
\begin{columns}[c]
|
|
\column{0.50\textwidth}
|
|
|
|
\begin{itemize}
|
|
\item \emph{Resist} the flow of current
|
|
\item Needed for LEDs: $\approx\SI{400}{\ohm}$\\
|
|
(safe for $\le\SI{6}{\volt}$)
|
|
\item Button Pull-up/down: $\ge\SI{10}{\kilo\ohm}$
|
|
\item Color coded, Google it
|
|
\end{itemize}
|
|
\column{0.50\textwidth}
|
|
|
|
\includegraphics[width=0.98\textwidth]{images/resistor.png}
|
|
|
|
\end{columns}
|
|
|
|
## Ohm's Law
|
|
|
|
Ohm's Law relates current to potential and resistance.
|
|
|
|
$$ V = IR $$
|
|
$$ I=\frac{V}{R} $$
|
|
$$ R = \frac{V}{I} $$
|
|
|
|
* V = Potential in Volts (\si{\volt})
|
|
* I = Current in Amperes (\si{\ampere})
|
|
* R = Resistance in Ohms (\si{\ohm})
|
|
|
|
## Ohm's Law: Example
|
|
|
|
The datasheet for an LED says that the maximum continuous current is
|
|
\SI{15}{\milli\ampere}. Your circuit operates at \SI{5}{\volt}\footnotemark[1]. How
|
|
big should your resistor be?
|
|
|
|
$$ \si{\ohm} = \frac{\SI{5}{\volt}}{\SI{0.015}{\ampere}} = 333.\overline{3}\si{\ohm} $$
|
|
|
|
How much current for our *cheet sheet* value?
|
|
|
|
$$ \si{\ampere} = \frac{\SI{5}{\volt}}{\SI{400}{\ohm}} = \SI{12.5}{\milli\ampere} $$
|
|
|
|
\footnotetext[1]{\tiny Actually, this calculation is inaccurate. LEDs will have a *forward voltage drop* of between \SI{300}{\milli\volt} and \SI{700}{\milli\volt} this should be subtracted from \si{\volt} above... but it's not critical.}
|
|
|
|
## Buttons
|
|
|
|
- Buttons connect _or_ disconnect two wires/parts
|
|
- Momentary Switch: Normally Closed (NC), Normally Open (NO)
|
|
- Toggle Switch
|
|
|
|
## Digital Signals
|
|
|
|
- Vcc: The power supply of the digital circuit elements
|
|
- GND: The reference voltage (usually \SI{0}{\volt})
|
|
- Connecting a part to Vcc = Logical 1
|
|
- Connecting to GND = Logical 0
|
|
|
|
## Transducers {.fragile}
|
|
|
|
Transducers turn electrical energy into another sort of energy:
|
|
|
|
| |
|
|
-------|--------:
|
|
Speaker|Electrical $\rightarrow$ Sound
|
|
Microphone|Sound $\rightarrow$ Electrical
|
|
LED|Electrical $\rightarrow$ Light
|
|
LED|Light $\rightarrow$ Electrical
|
|
Piezoelectric|Electrical $\rightarrow$ Motion
|
|
|
|
## Piezo Buzzer
|
|
|
|
- Piezoelectric elements change shape when voltage is applied
|
|
- Thin discs can be made to oscillate and create sound.
|
|
- Contains oscillator circuit
|
|
- Two connections: Vcc, GND
|
|
- Use a switch; connected = annoying tone, disconnected = glorious silence
|
|
|
|
|
|
## Power
|
|
|
|
The power supply provides the energy to drive the system *and* defines logical 1.
|
|
|
|
Can be a:
|
|
|
|
* Voltage Regulator (converts one potential to another)
|
|
* Batteries (Lemon, NiMH, LiPo)
|
|
* Solar Panel
|
|
|
|
In our circuits, your laptop is converting it's power source to 5V and
|
|
delivering power to our circuit via USB. You also have a battery pack
|
|
for computer-free shenanigans.
|
|
|
|
## $\mu$Controller
|
|
|
|
Microcontroller ($\mu$C) is a *processor*, *memory* and a few *peripherals* on a standalone
|
|
chip.
|
|
|
|
Processor
|
|
: is a group of transistors that understands a few dozen
|
|
commands (ADD, SUB, JUMP..)
|
|
|
|
Memory
|
|
: a circuit that can hold values.
|
|
|
|
Peripherals
|
|
: Vary chip to chip, but often include timers, radios, communication interfaces
|
|
|
|
Seems complicated, but really simple. They literally read a command
|
|
(and data) from memory, then execute the command. At the end of the
|
|
command, the next command is read from the next memory cell and the
|
|
process is
|
|
repeated^[some commands change the next command memory address]
|
|
|
|
# Let's start programming
|
|
|
|
## Configure Arduino
|
|
|
|
\begin{center}
|
|
\includegraphics[width=0.98\textwidth]{images/arduino-board.png}
|
|
\end{center}
|
|
|
|
* Board: Arduino UNO
|
|
* Processor: ATmega328
|
|
* Port: \ldots
|
|
|
|
## The Code Environment
|
|
|
|
\begin{center}
|
|
\includegraphics[width=0.5\textwidth]{images/arduino-toolbar.png}
|
|
\end{center}
|
|
|
|
## Your first Program
|
|
|
|
~~~ C
|
|
/* the setup function runs once on reset / power */
|
|
void setup() {
|
|
/* set pin 13 as an output */
|
|
pinMode(13, OUTPUT);
|
|
}
|
|
|
|
/* the loop function repeats forever */
|
|
void loop() {
|
|
digitalWrite(13, HIGH); // turn on LED
|
|
delay(1000); // wait for a second
|
|
digitalWrite(13, LOW); // turn the off LED
|
|
delay(1000); // wait for a second
|
|
}
|
|
~~~
|
|
|
|
## Buzzer: Hardware
|
|
|
|
\begin{center}
|
|
\includegraphics[width=0.98\textwidth]{images/buzzer-breadboard.png}
|
|
\end{center}
|
|
|
|
## Buzzer: Software
|
|
|
|
~~~ C
|
|
#define BUZZER 8 /* Make BUZZER same as pin 8 */
|
|
|
|
void setup() {
|
|
pinMode(BUZZER, OUTPUT);
|
|
digitalWrite(BUZZER, HIGH); /* Turn off buzzer */
|
|
}
|
|
void loop() {
|
|
digitalWrite(BUZZER, LOW); /* Turn on buzzer */
|
|
delay(100); /* wait for 100ms */
|
|
digitalWrite(BUZZER, HIGH); /* Turn off buzzer */
|
|
delay(900); /* wait 900ms */
|
|
}
|
|
~~~
|
|
|
|
## Push Button: Hardware
|
|
|
|
\begin{center}
|
|
\includegraphics[width=0.98\textwidth]{images/buzzer-button-breadboard.png}
|
|
\end{center}
|
|
|
|
## Push Button: Software (Part 1)
|
|
|
|
~~~ C
|
|
#define BUTTON 7
|
|
#define BUZZER 8
|
|
|
|
int button_state = 0;
|
|
|
|
void setup() {
|
|
pinMode(BUTTON, INPUT);
|
|
pinMode(BUZZER, OUTPUT);
|
|
digitalWrite(BUZZER, HIGH);
|
|
}
|
|
~~~
|
|
|
|
## Push Button: Software (Part 2)
|
|
|
|
~~~ C
|
|
void loop() {
|
|
button_state = digitalRead(BUTTON);
|
|
if (button_state == HIGH) {
|
|
digitalWrite(BUZZER, LOW);
|
|
} else {
|
|
digitalWrite(BUZZER, HIGH);
|
|
}
|
|
}
|
|
~~~
|
|
|
|
## The End?
|
|
|
|
\begin{center}
|
|
\LARGE{Questions?}
|
|
\end{center}
|