Changes between Version 2 and Version 3 of ControlSystems/Electrical/Training/Arduino/Lesson3


Ignore:
Timestamp:
Sep 28, 2015, 1:07:44 AM (10 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/Electrical/Training/Arduino/Lesson3

    v2 v3  
    11= Touch and Feel =
     2
     3Robots need to be able to sense their environment.  One of the most basic sensing devices is the mechanical switch.  A switch is any device that opens or closes a circuit mechanically.  You can use a switch to detect when a robot has bumped into something or to stop a moving part (like an arm) from exceeding its range of motion (this is known as a **Limit Switch**). 
     4
     5You have a couple of switches in your kit. [[Image(http://ecx.images-amazon.com/images/I/41BuqbQZt8L._AC_UL115_.jpg, align=right)]].  Each switch has two leads on the bottom and a button on top.  When you push the button, the two leads are connected electrically.
     6**Exercise**: Use a multimeter to measure the continuity or resistance of the switch in the open (button not pressed) and closed (button pressed) positions.
     7
     8* **Exercise**: [http://playground.arduino.cc/Main/FlightGearInputPushbutton read the position of a switch]: connect a switch to your arduino using the protoshield and breadboard. 
     91. Put the leads of the switch on opposite sides of the valley in the middle of the breadboard.  NOTE: if you put both switch leads in the same row of holes on the same side of the valley, then they are already connected together by the breadboard and pushing the button will do nothing.
     102. Use a jumper wire to connect one of the switch pins to +5v
     113. Use a jumper wire to connect the other switch pin to an Arduino digital pin
     124. Connect a 10K resistor to the switch pin that is connected to the Arduino digital pin
     135. Connect the other end of the 10K resistor to Ground
     146. Write software to change the state (on or off) of an LED each time the switch is pushed.
     15 
     16
     17==  Why the 10K resistor? ==
     18The 10K resistor is called a pull-down resistor.  It is used to keep the Arduino digital input in a known state when the switch is open.  Without it, the digital input (which is very sensitive) could change its state due to electrical noise (e.g. from fluorescent lights).  The 10K resistor keeps the input pin at the ground level maintaining a 'low' at the digital input until the button is pushed.  When the button is pushed, the input pin is connected directly to +5v through the switch.  Because the switch has extremely low resistance, the +5V overwhelms the weak pull-down resistor and the input pin will read high.
     19
     20It's important to understand pull-up and pull-down resistors as they are required in many circuits.  They are such a common requirement that many modern microcontrollers (like the arduino) have them built in where they can be turned on and off under software control see [https://www.arduino.cc/en/Tutorial/InputPullupSerial here].
     21
     22== Debouncing ==
     23You may notice that your LED sometimes changes to the wrong state when you push the button.  This is due to **switch bounce**; please see [http://www.labbookpages.co.uk/electronics/debounce.html this tutorial].  Switch bounce is particularly problematic when you are using a switch to count events.  For example, to count rotations of a robots wheel to see how far you've gone.  There are a number of ways to eliminate switch bounce using hardware solutions, but the least expensive solutions use software.  The most common approach is to note the time when the switch first closed and then not count any more closures until a debounce period has elapsed (usually a few tens or hundreds of milliseconds).
     24**Exercise**: write software to debounce your switch so it will reliably toggle the LED state.
     25
     26== Libraries ==
     27Arduino has a huge collection of libraries containing software routines you can incorporate into your programs to solve all sorts of problems.  Naturally there is an already written library for switch debouncing: [http://playground.arduino.cc/Code/Bounce Bounce library]. 
     28**Exercise**: Add a library to your sktch (see [https://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-libraries here] for details.
     29
    230* DEVICES: Mechanical switches
    331    * SPST, SPDT, DPDT terminology (w/demo)
    4     * Switch bounce
    5     * Hardware de-bouncing - pull-up resistors, capacitors
    6     * Software de-bouncing
    732    * Potentiometer - pencil/graphite resistor, multimeter, analog input
    833* THEORY: Digital vs. Analog input
    934* MISC: Survey of other mechanical inputs/uses (e.g. magnetic reed switch, limit switches)
    10 * HANDS-ON: read a switch with a digital input and an analog value from the potentiometer with an analog input.
     35* HANDS-ON: ead a switch with a digital input and an analog value from the potentiometer with an analog input.
    1136
    12 == Experiments ==
     37== More Experiments ==
    1338* [https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-5-push-buttons Push Buttons]