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

map() returns a value on the scale of outFrom to outTo.

             You can easily modify the map() function by converting a value into a percentage from

        0 to 100—just change the output values.




        Variable Resistors

        Most analog sensors work like a potentiometer does, calculating the varying voltage as the

        resistor changes its value. These are called variable resistors that resist the flow electricity
        through a circuit. A good example of this is a simple light-dependant resistor or photocell
        like in Figure 5.7. This changes the resistance based on the amount of light that hits it.
        When the light increases, the resistance goes down; therefore, the voltage in the circuit

        goes up. Take the light away, and the resistance increases and the voltage is reduced.

































        Figure 5.7 A typical photocell, which acts as a variable resistor.


             In order to read sensors like these with the Photon, you will need to create a voltage
        divider circuit and connect it to the analog input pins on the Photon board.




        Voltage Divider Circuits


        When you are working with different sensors that offer a variable resistor feature, you
        need to create something called a voltage divider circuit. A voltage divider circuit converts
        the variable resistance into a variable voltage so we can read this value from the input pins

        on the Photon. In the schematic diagram in Figure 5.8 you can see a simple voltage divider
        circuit.
   104   105   106   107   108   109   110   111   112   113   114