Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Arduino 5 - Inputs: Mr. Mcbrien Tej2O

This document discusses inputs to Arduino programs including from users, sensors, and other devices. It provides examples of using pushbuttons, sensors, internet connections as inputs. Code examples are shown for reading digital and analog pin values to detect button presses and sensor resistance levels. Students are assigned to build circuits using buttons and LEDs, and an optional extension to make an electronic thermometer. Key concepts are that Arduino pins can read digital on/off signals or analog voltage values as program inputs.

Uploaded by

mike simson
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Arduino 5 - Inputs: Mr. Mcbrien Tej2O

This document discusses inputs to Arduino programs including from users, sensors, and other devices. It provides examples of using pushbuttons, sensors, internet connections as inputs. Code examples are shown for reading digital and analog pin values to detect button presses and sensor resistance levels. Students are assigned to build circuits using buttons and LEDs, and an optional extension to make an electronic thermometer. Key concepts are that Arduino pins can read digital on/off signals or analog voltage values as program inputs.

Uploaded by

mike simson
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Arduino 5 – Inputs

Mr. McBrien
TEJ2O
Yesterday

 PWM
Today

 Sensors and pushbuttons


Curriculum
 Students will:
 B5. demonstrate an understanding of fundamental programming
concepts, and develop a program that interacts with an external
device.

 B5.1 use constants, variables, expressions, and assignment


statements correctly;

 B5.2 use input statements, output statements, selection


structures, and repetition structures in a program.
“The only thing that is constant is
change.” - Heraclitus
 Almost no device can function independently of its
environment

 Examples of independent devices?

 Examples of “adaptive” devices?


A Circuit that Makes Choices

 We can construct loops to monitor given pins

 Our sketch can change its behaviour based on the value from a given pin.

This change in behaviour could be as simple as going to standby mode, or as


complex as we can conceive.
A Simple Interactive System

Monitor
Switch

Pressed? No

Yes
Start
Function
Arduino Input Sources

 Users
 Internet
 Sensors
 Other devices
User Inputs to the Arduino

 Connecting hardware to the Arduino makes it possible for a user to change its
behavior:
 Switches
 Dials
 Keypads
 Touchscreens
 More !
Examples

 A user presses a switch, and power is provided to a circuit


until the switch is pressed again.
 A user types a passcode into a keypad and a security
system is disabled.
Sensors

 There are many kinds of sensors that Arduinos can monitor much like a
switch.
 Sensors change their electrical behavior in response to the environment
 Sensors can detect:
 Light (or lack of light)
 Infrared
 Moisture
 Heat (or its lack)
 Much, much, more.
Examples

 A “nightlight” turns on an LED if there is no light hitting a


light-dependent resistor (LDR)

 An IR sensor detects a pattern of pulses and provides


power to a device.
Internet Input
 Available Arduino shields include WiFi and
Ethernet.

 Arduinoscan be controlled by apps, or


even monitor websites like Twitter.

 Programs can be run on PCs that write to a


network. The Arduino can monitor a
network location and act accordingly.
Internet Examples

 An iPhone app sends commands to an WiFi-enabled


Arduino controlling a thermostat

 The Arduino accesses a website and sounds a siren when


the Leafs score.
Other Devices

 Other devices can connect to the Arduino if they have appropriate output:
 Virtually any type of electrical output
 Bluetooth
 Optical/IR remotes

The necessity of a shield depends on the situation…


Other Examples

 A romantic geek connects a lockbox to an Arduino


with a GPS shield

 He gives the lockbox to a woman with a series of


clues

 When she gets to the location described in the clues


the lockbox opens to reveal the engagement ring…
How Does the Arduino Detect/Use
Inputs?
 This depends on the type (duh!)

 Usually, incorporate a status check in your loops

 Each check is coded differently…


Today’s Key Command(s)
Core Skill!
 val = analogRead(pin#);
 PushButtonState = digitalRead(pin#);

This command reads the potential at pin# and assigns it to


a variable.
Digital vs. Analog

 Reminder: Digital = on/off; analog = a value.


 A switch is a classic on/off; sensors tend to give
values.
 Some sensors are digital!!!
The Numbers

 When the Arduino monitors pins, it measures the voltage


between 0 and 5V
 The value is from 0 to 1023 (how many bits???)
 The resistance between the source and the input pin will
determine the value

As long as a sensor gives a predictable electrical response to our stimuli,


we can use it to make a detector.
An Example – Determining the
Resistance of a Circuit
 We’ll consider a simple nightlight implementation
 The resistance of an LDR changes with light hitting it
 The Arduino needs to access this…
Resistance Determines Voltage
 Arduinos can’t measure resistance directly – we’ll do it
indirectly.
 We run 5V across the LDR plus another resistor
 Remember voltage drop?
 The current across the LDR will create a voltage drop
between it and the resistor
 We measure the voltage between the device and the fixed
resistance.
The Circuit

We measure the potential


difference between these
points
The Decision

 Based on the potential at A0 we calculate the light


level

 Basedon the light level, we decide when to turn on


the LED
Leveraging the Serial Monitor

 We need to know what voltage represents


lit/unlit

 We can then use the value in any calculations.


Activity 1 – Pushbutton LED

 Breadboard LEDs, connected to Arduino output pins.


 Monitor a “button” to see if it’s pushed. If it is, turn
on the first LED.
 Continue to monitor the button, and turn off the LED
if it’s pushed again.

Don’t forget to insert a delay between monitoring steps!


Check and Wait
Expert Tip!
 Checking status on a switch too often can introduce
problems
 One positive result can look like two (or 20!)
 A delay between checking operations will avoid errors due
to slow execution in the physical world.
A new tool: if/else/else if
 (see references) Core Skill!

You may not have a pushbutton handy – don’t forget that you can get
the same result from touching two jumpers together.
Activity 2 – Use a Button to Change LEDs

 Obtain several more LEDs.


 Monitor your switch, and each time the switch is
flicked, move to the next LED.

Don’t forget to make sure every LED is protected by your


resistor!
Extension Activity – Electronic
Thermometer
 We’ll configure the Arduino to monitor a temperature sensor and change its
behavior based on the result.
 We’ll report the temperature to the Arduino Serial Monitor
 Math reference:
 TMP35 Datasheet
Extension – A Quick-Read Thermometer
(20 XPs)
 Our Arduino can make more elaborate decisions based on the output of the
thermometer
 Please configure the Arduino to report the result via LEDs:
 Red >27˚C
 Yellow ~24˚C
 Green <21˚C
Key Points
 Arduino input pins monitor potential difference – voltage.
 Values can be digital : on/off, or
 Values can be analog : 0-1023.
 With these inputs, we can monitor for button pushes, or get
values from sensors.
Homework

 Complete your input circuits activity.


Arduino References

 If/then/else
 Digital Read
 Analog Read

You might also like