
Chapter 2. Working with Sensors
A sensor is a device that can detect changes or events and provide a corresponding output. The output is usually an electronic signal, for example, a light dependent resistor (LDR) outputs a voltage, which depends on the level of light cast on the sensors. When working with electronics, sensors are often divided into analog and digital sensors. Digital sensors can only detect two states, either on or off. The digital sensor can only distinguish if there is voltage going into the sensor or not. In code, this transfers into a 1
for voltage coming in and 0
if there is no voltage present. This is why they are called digital sensors, since they only operate in 0s and 1s. This means that these sensors only have two states, either on or off. A button, for example, is a digital sensor, which can only sense two states, if the button is pushed or not.
Analog sensors, however, can sense a range of values. The LDR, for example, is an analog sensor that changes the output voltage depending on the light level cast on the sensor surface. The problem with microprocessors is that they are digital by nature, and don't know how to handle analog information by default. This is why there are analog pins on almost all Arduino boards, which have an analog to digital conversion built in. These pins can read values ranging from 0 to 1023. In this chapter, we will have a look at some different sensors that may be useful for wearable projects and introduce them to readers that are not too familiar with programming yet.
In this chapter, we will take a look at a collection of analog sensors, which can be used to track movement and light. In the first two examples, we will focus on a stand-alone sensor component, which will involve building circuits using a breadboard. The remaining examples will use sensors that include prebuilt circuits on a PCB board. In this chapter, we will also take a look at different ways to communicate with our prototyping board and how to send data back to the computer.
Tip
If you run out of digital pins, you can always use the analog pins even for reading digital sensors. Just remember that the output will be read in the range of 0 to 1023 and not HIGH and LOW if you are reading them as analog pins. The analog pins can be used as digital pins as well. Analog pin A1 is the same as digital pin 14, and so on.