Page 96 - Programming the Photon Getting Started With the Internet of Things
P. 96

5




                                                     Inputs











        In  this  chapter  we  will  program  some  input  devices  such  as  switches,  temperature

        sensors, and many more. Input devices are generally used to trigger some output or event.
        A simple switch can easily be used to turn something on or off, such as a light-emitting
        diode (LED). A sensor used as an input device can be used to monitor something like
        temperature or a certain type of gas, and when triggered, it can set a series of events in

        motion or just be collected as data and interpreted into something visual. This chapter will
        also look at some of the basic programming used in reading and detecting inputs, which
        you can use for your own projects in the future. We will look at both digital and analog
        inputs, where a digital system will return a value of either 1 or 0 and an analog input will

        return  a  value  from  0  to  4095  using  some  sort  of  analog-to-digital  conversion.  In  this
        chapter you will accomplish the following:


               Understand how to read digital and analog inputs

               Experiment with using different types of input devices
               Learn more about coding with the Particle programming language




        Digital Inputs


        When we used digitalWrite() in the previous chapter, there were two different states

        that the LED could be: either HIGH or LOW. When it comes to using digital inputs, the
        same states apply to the switch—it is either HIGH or LOW. When you read the state of a
        digital  input,  it  will  be  connected  to  either  3V3,  which  indicates  a  HIGH  state,  or  to

        ground, which indicates a LOW state. By connecting a simple push-button switch to your
        Photon, you can easily change the state of a digital input pin by pressing it.

             A simple push-button switch as shown in Figure 5.1 is great for experimenting with
        digital  inputs,  and  is  also  an  inexpensive  component—something  to  consider  when

        building your own circuits. You can see it snaps straight into the breadboard on either side
        of the gap. This switch is a common component you will become familiar with, as it is
        included in almost every electronic starter kit. When looking at the switch in Figure 5.1,
        the top two pins are connected together, as are the bottom two pins. When the push-button
        switch  is  pressed  down,  both  sets  of  pins  are  connected  in  a  circuit  and  it  becomes

        complete.
   91   92   93   94   95   96   97   98   99   100   101