--- author: Titus Ferguson, Shawn Nock institute: UnPacked Digital Literacy title: Arduino for the Classroom subtitle: Let's Talk Science Instructional Design Team lang: en-CA colorlinks: true ... ## Goals By the end of this class, you'll: - Be familiar with basic electionics concepts - Assemble a kit of sensors into a complete system - Know how to program the Arduino platform to collect and plot the sensor data ## UnPacked / Unlondon \begin{center} {\huge Enabling Exploration, Creativity, and Excellence In Art+Make+Tech} \end{center} Challenging and embracing ideas related to new technologies and social platforms through the education, entertainment and engagement of our membership and the community-at-large. - 121Studios: Coworking for Creatives - STEAM Outreach & Education - ExplodeConf - Nuit Blanche # What's in your kit? ## Kit Contents ###Arduino Uno R3 ###Solderless Breadboard ###Connecting wires ###Sensors - Temperature / Humidity - Light Sensor - CO2 Gas Sensor ## What is Arduino? \center{\includegraphics[width=0.75\textwidth]{images/arduino-annotated.png}} 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 Software The Arduino folks also adapted an *Integrated Development Environment* (IDE) to their boards. This IDE allows us to easily write programs for their boards and then transfer 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) Get the libraries and sample code for this workshop: [https://monadnock.ca/lts-workshop-Nov2017.zip](https://monadnock.ca/lts-workshop.zip) # Circuit Basics ## Current Current is the flow of charge through a circuit. Measured in Amperes (\si{\ampere}). ## Resistance / Impedance Circuits have a resistance to current flow that depends on the parts in the circuit. Measured in Ohms (\si{\ohm}) ## Voltage Voltage is a potential (akin to a pressure) pushing the current through a circuit. Current is said to flow from higher (+) voltage to lower (-) voltage. Measured in Volts (\si{\volt}) ## Ohm's Law; Light Voltage, Current and Resistance are related to each other. - As voltage increases, current increases - As voltage decreases, current decreases - As resistance increases, current decreases - As resistance decreases, current increases ## \si{\volt}, \si{\ohm}, \si{\ampere}: The Water Analogy If charge were water, then: - resistance = obstacles blocking flow - current = flow rate - voltage = change in height *or* pressure. ## 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 $\rightarrow$ Too much heat $\rightarrow$ \ \ \center{ \huge{\emph{What's that smell?} } } ## Resistor \begin{columns}[c] \column{0.50\textwidth} \begin{itemize} \item \emph{Resists}/limits the flow of current \item Needed for LEDs: $\approx\SI{1000}{\ohm}$\\ \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} ## Buttons - Buttons connect _or_ disconnect two wires/parts - Momentary Switch: Normally Closed (NC), Normally Open (NO) - Toggle Switch ## Circuits A circuit is a completed loop from HIGH potential (voltage) to LOW, which causes current to flow through some other components along the way. ## Transducers {.fragile} Often these *other* components are *transducers*, which convert 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 ## Power The power supply provides the energy to drive the system. Can be a: * Voltage Regulator (converts one potential to another) * Batteries * Solar Panel In our circuits, your computer is converting it's power source to $\SI{5}{\volt}$ and delivering power to our circuit via USB. ## $\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 dozen or so commands (ADD, SUB, JUMP..) Memory : a circuit that can hold values. Peripherals : Vary chip to chip, but often include timers, communications and ADC, DAC. Seems complicated, but really simple. They read a command from the start of memory, then execute the command. At the end of the command, read the next command from the next memory cell and repeat^[some commands change the address of the next fetched command] ## Digital Signals - Vcc: The power supply of the circuit elements - GND: The reference voltage (usually \SI{0}{\volt}) - Connecting a part to Vcc = Logical 1 or High - Connecting to GND = Logical 0 or Low - Connecting various pins to Vcc or Ground is all the $\mu$C can do to talk to the world [^4] [^4]: w/o fancy peripherals or dirty tricks ## $\mu$Controller INPUT and OUTPUT Most of the pins on the Arduino can be set for INPUT or OUTPUT mode. - INPUT mode pins listen for a signal (High or Low) from another device - OUTPUT mode pins drive the pin High or Low ## Floating Pins What's happens if an INPUT mode pin tries to read the value of a pin that is connected to nothing? Is that a $1$ or $0$? \center{\huge {No one knows!}} It's dependant of transient charges, static, nearby electric fields, the phase of the moon, \ldots Whenever you want to check a digital signal, make sure that something is *driving* it (ensuring Vcc or GND). ## $\mu$C + Digital Signals as Switches If one end of an LED is connected to ground, and the other end is connected to an OUTPUT pin on a $\mu$Controller, then: - If the $\mu$C sets the pin High (Vcc, $\SI{5}{\volt}$) then current will flow from the pin through the LED and turn it on. - If $\mu$C sets the pin Low (GND, $\SI{0}{\volt}$) then the current will not flow and the LED is off. # Let's start programming ## Configure Arduino \begin{center} \includegraphics[width=0.98\textwidth]{images/arduino-board.png} \end{center} * Board: Arduino/Genuino UNO * Port: \ldots ## Fetch the Class Code Download and extract: [https://monadnock.ca/static/lts-workshop-Nov2017.zip](https://monadnock.ca/static/lts-workshop-Nov2017.zip) - File$\rightarrow$Preferences - Browse for sketchbook - Point it at the `sketchbook` subfolder of the extracted download - You should now see a list of projects in the File$\rightarrow$Sketchbook menu. ## Setup the Sensor Libraries (Sketchbook) \begin{center} \includegraphics[width=0.5\textwidth]{images/preferences.png} \includegraphics[width=0.5\textwidth]{images/preferences2.png} \end{center} For the *Sketchbook Location* browse to the extracted lts-workshop-Nov2017/sketchbook directory. ## The Code Environment \begin{center} \includegraphics[width=0.95\textwidth]{images/arduino-toolbar.png} \end{center} ## Your first Program ~~~ C #define LED 13 /* the setup function runs once on reset / power */ void setup() { pinMode(LED, OUTPUT); } /* loop() repeats until reset or power off */ void loop() { digitalWrite(LED, HIGH); // turn on LED delay(1000); // wait for a second digitalWrite(LED, LOW); // turn the off LED delay(1000); } ~~~ # Lets Add Some Parts ## Breadboard ![Breadboard](images/breadboard.png)\ - Connectors gently pinch component leads, wires. - Have internal connections ## Power Up the Rails We use the long rows to distribute power. The Arduino outputs $\SI{5}{\volt}$ on the pin marked `5V`, the reference (GND) is marked `GND`. \begin{center} \includegraphics[width=0.92\textwidth]{images/rails.png} \end{center} ## DHT11: Temperature and Humidity Sensor \begin{center} \includegraphics[width=0.98\textwidth]{images/dht11-wide.png} \end{center} ## DHT11: Zoom \begin{center} \includegraphics[width=0.25\textwidth]{images/dht11-zoom.png} \end{center} Connect left pin to Vcc rail ($+$) and right pin to GND ($-$) rail. Second from left connects to Arduino pin #2 *and* a resistor to Vcc ## Pullup / Pulldown Resistors The DHT11 uses an *Open Collector* output that switches between *Not Connected* and *Connected to Ground*. When it's not connected, the pin is left *floating*. How do we make a floating pin look like a High signal? \vfill **Solution:** Connect the pin to Vcc so that it reads High; use a resistor to prevent short circuit (limit current). ## DHT11: Run example code We can test the sensor by running sample code from the library. Open the sample: File$\rightarrow$Examples$\rightarrow$DHT Sensor Library$\rightarrow$DHTtester Once the sample is loaded click the *upload* button (looks like right arrow) ## Arduino Serial Monitor Once the sample code is uploaded, you can open the *Serial Monitor* to view the output. It can be found under: Tools$\rightarrow$Serial Monitor ## DHTtester Output \begin{center} \includegraphics[width=0.98\textwidth]{images/monitor-dht.png} \end{center} ## TSL2561: Ambient Light Sensor \begin{center} \includegraphics[width=0.98\textwidth]{images/tsl2561-wide.png} \end{center} ## TSL2561: Zoom \begin{center} \includegraphics[width=0.3 \textwidth]{images/tsl-zoom.png} \end{center} Connect Vin to ($+$) rail, GND to ($-$) rail, SDA to Arduino pin A4 and SCL to A5 ## TSL2561: Run example code We can test the sensor by running sample code from the library. Open the sample: File$\rightarrow$Examples$\rightarrow$Adafruit TSL2561$\rightarrow$sensorapi Once the sample is loaded click the *upload* button (looks like right arrow) ## TSL2561 Sample Output \begin{center} \includegraphics[width=0.98\textwidth]{images/monitor-tsl.png} \end{center} ## CCS811: Gas Sensor \begin{center} \includegraphics[width=0.98\textwidth]{images/CCS811-wide.png} \end{center} ## CCS811: Zoom \begin{center} \includegraphics[width=0.3\textwidth]{images/ccs11-zoom.png} \end{center} *Vin* to ($+$) rail, GND *and* WAKE to ($-$) rail, SDA to TSL2561 SDA, SCL to TSL2561 SCL ## CCS811: Run example code We can test the sensor by running sample code from the library. Open the sample: File$\rightarrow$Examples$\rightarrow$Adafruit CCS811 Library$\rightarrow$CCS811_test Once the sample is loaded click the *upload* button (looks like right arrow) ## CCS811 Sample Output \begin{center} \includegraphics[width=0.98\textwidth]{images/monitor-ccs.png} \end{center} # Programming ## Part 1; Includes ~~~ C #include #include #include #include "Adafruit_CCS811.h" #include "DHT.h" ~~~ ## Part 2; Definitions ~~~ C #define DHTPIN 2 #define DHTTYPE DHT11 ~~~ ## Part 3; Objects ~~~ C Adafruit_CCS811 ccs; DHT dht(DHTPIN, DHTTYPE); Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); ~~~ ## Programming Note: Variables Declare a variable: ~~~ C int button_state = HIGH; ~~~ ` [= ];` (value optional) It's a name, like a preprocessor `#define`, but the value can change at *runtime* ## Part 4; Setup ~~~ C void setup() { Serial.begin(9600); Serial.println("Let's Talk Science!"); dht.begin(); ccs.begin(); tsl.begin(); while(!ccs.available()); float temp = ccs.calculateTemperature(); ccs.setTempOffset(temp - 25.0); tsl.enableAutoRange(true); tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); } ~~~ ## Part 4; Main Loop ~~~ C loop () { delay(500); ... } ~~~ ## Part 4a; Temperature / Humity Sensor Read ~~~ C loop () { ... float humidity = dht.readHumidity(); float temp = dht.readTemperature(); Serial.print(humidity); Serial.print(" "); Serial.print(temp); Serial.print(" "); ... } ~~~ ## Part 4b; Gas Sensor Read ~~~ C loop () { ... ccs.readData(); int co2 = ccs.geteCO2(); int tvoc = ccs.getTVOC(); Serial.print(co2/100); Serial.print(" "); Serial.print(tvoc/10); Serial.print(" "); } ~~~ ## Part 4c; Light Sensor Read ~~~ C loop () { ... sensors_event_t event; tsl.getEvent(&event); Serial.print(event.light/10); ... } ~~~ ## Part 4d; Ending the output ~~~ C loop () { ... Serial.println(""); } ~~~ ## Complete Sensor Output \begin{center} \includegraphics[width=0.98\textwidth]{images/monitor-all.png} \end{center} ## Arduino Serial Plotter Why such a terse, boring output format? Arduino has a feature where it will plot data in this specific format. You can see the plotted output by closing the Arduino Serial Monitor and Opening *Arduino Serial Plotter* (Tools$\rightarrow$Serial Plotter) ## Arduino Serial Plotter Output \begin{center} \includegraphics[width=0.98\textwidth]{images/plotter.png} \end{center} # Undiscussed Issues ## Obtaining the kit - Educators would need to order several different parts (SKUs). - Vendors keep very little stock (~4 dozen). - Educators may need to "google around" to find parts, most likely from abroad (US or China) ## Installing the Software - School machines may prevent software installation - School machines may not be conventional computers (iPads, Chromebooks) ## Troubleshooting the Build - Breadboards aren't rugged. - Sockets wear out quickly - Parts may need to be wiggled - Some parts may be DoA - Most troubleshooting requires a Multimeter (and training on how to use it) # Extra Credit ## 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{1}{\kilo\ohm}} = \SI{5}{\milli\ampere} $$ \footnotetext[1]{\tiny Actually, this calculation is inaccurate. LEDs will have a *forward voltage drop* of between \SI{1.8}{\volt} and \SI{3.3}{\volt} this should be subtracted from \si{\volt} above... but it's not critical.} ## Current Limits, Arduino - No single pin should source more that \SI{20}{\milli\ampere} (\SI{40}{\milli\ampere} is absolute max) - Pins are ganged together in groups of 8, no group should source more than \SI{150}{\milli\ampere} total - The whole board cannot source more than \SI{200}{\milli\ampere} total Practically speaking, this means that the Arduino cannot drive speakers, most motors, or anything normally mains powered. ## So\ldots no Arduino smart blender? You can control almost anything with an arduino, you just can't power it with the Arduino. There are various devices that let you switch highier powered devices: - Transistors - Relays - Solid State Relays - Triac ## HIGHs and LOWs Many different logic levels are in common use: \SI{1.2}{\volt}, \SI{1.8}{\volt}, \SI{2.5}{\volt}, \SI{3.3}{\volt}, and \SI{5}{\volt}. The voltage cited is the *nominal* Vcc of the system. A HIGH signal is generally any voltage $\geq \frac{2}{3}V_{cc}$. A LOW signal is generally any voltage $\leq \frac{1}{3}V_{cc}$. ## HIGHs and LOWs, pt. 2 In your travels, you're likely to see both \SI{5}{\volt} and \SI{3.3}{\volt} sensors and peripherals. Since $\SI{3.3}{\volt}\geq\frac{2}{3}V_cc$ your Arduino will accept input from a \SI{3.3}{\volt} peripheral without issue. If you drive an output to \SI{5}{\volt} while it's connected to a \SI{3.3}{\volt} peripheral with an Arduino **it will blow up your peripheral**.^[In the datasheet for the sensor, it'll have a section called *Absolute Maximums*. Generally \SI{3.3}{\volt} parts won't accept more that $\approx\SI{3.6}{\volt}$, but some will.] ## HIGHs and LOWs, pt. 3 Solutions: - Level Shifter: A dedicated chip that translates between voltages. Available as uni or bidirectional. - Buy a 3.3V Arduino Compatible. Arduinos are available that operate at the lower voltage.