This is an old revision of the document!

# Course 4: Sensors and peripherals

## Sensors

Sensors are devices that scan the environment and get data from it. Some examples of sensors are : thermistors , buttons (because they send a value, sensing the environment), photo resistors, infra-red sensors, distance sensors.

Types of sensors:

• Analog Sensors - send an analog value that needs to be processed by the ADC
• Digital Sensors - send a digital value

Basically, all the sensing parts are analog.

For the analog sensor the sensing part is directly connected to a micro controller. They are easy to interface, you only need an ADC to do some computing to find the real value.

Digital sensors are more complicated because the micro controller that processes the data is already in the box of the sensor and it sends the data out on a communication channel that implements some type of protocol. In order to get the data, the device needs to communicate with the sensor via the protocol.

For the analog sensors there are two types: with two or three pins.

An analog sensors will most probably be connected as a voltage divider. Such a sensor has a variable resistance, according to a specific environment element. Take a light sensor as an example: the more light there is, the lower the resistance. For the temperature sensor, the higher the temperature, the lower the resistance. You practically measure the voltage drop in the circuit. The 2 leg and 3 leg analog sensors are mentioned above. The sensors that have 3 pins usually have the voltage divider already integrated: one pin goes to VCC, one to the Ground and the third pin goes to the analog input. For the sensors withe 2 legs, you have to build the voltage devider yourself by adding a resistance to the circuit

If you switch VCC with ground, some sensors will burn.

Another particuparity of the sensors is that they will introduce some errors in the measurements, depending on the quality of the sensor. These parts usually increase in accuracy if they have the voltage devider inside as they bring some corrections. More to this, the sensors are not linear.

One simple way to connect errors in reading or the ones that occurred due to the fact that the circuit is not perfect is to average them (usually using 1000 samples). This works on micro controllers, but on computer boards, it might result in reading the same value over and over.

### Button

It is one of the simplest sensors that you can connect. It is in fact an analog sensor, but it can only report 2 values : either 1 or 0. If the button is released, the resistance is infinite, if it is pressed, the resistance becomes 0 and the input will be directly connected to the ground.

The button becomes a pull-up resistor because when the button is not pressed it pulls up the voltage up to 5V. You need a really small resistor so that when the button is in the air the voltage drop will be insignificantly low, so that you can still read 5 volts. The problem comes up when you press the button for a longer period of time. In conclusion, you need a resistor sufficiently high that you won't drain the source too fast, but sufficiently low to be able to read the value 1.

If R1 is replaced by S1 it will be called a pull-down resistor: it will pull down the voltage from that point to the ground the moment the button is not pressed. When the button is pressed it will stop the current from flowing too fast and having a short circuit. The input will be connected to VCC.

#### Button Debounce

This problem is really visible on micro controllers because they sample fast. Buttons are imperfect, so from the moment you start pressing the button, there is a short time when the button's connectors start approaching and the distance is really small resulting in an electric discharge. The resistance will be neither 0, nor infinite, so for a short period of time the resistance will vary.

With the microelectronics, you will see many values of 1 and 0 continuously changing. One way to debounce is to average the values and if the result is different from 0 or 1, then to discard the measurement because the button bounced. Another way is to use a trigger which, for an amount of time won't read any other value thus not detecting the edges. Expensive microcontrollers have debouncing circuits.

### Potentiometer

A potentiometer is a variable resistance, with 3 pins. Usually it is a full resistor and the pin is connected in the middle (it can float around the full resistor and split it into two resistors so it works as a voltage divider). You can connect it in two ways: either by making a voltage divider, choose R1 and connect two of the pins in the voltage divider (the middle one and one of the extremities), or by connecting one pin to VCC, one the Ground and the middle one to the input. To have a linear function as input from the sensor some computing is requested.

### Thermistor

Let's see an example of how an analog sensor can be connected to a board and how an application looks in Visual Programming for the thermistore, for instance. This sensor is still a variable resistor that changes its value with the temperature. You can compute the actual temperature using that value and a formula that takes into account the thermistors parameters, among which the resistance at 25 degrees. This kind of sensor has a low accuracy and is used to get a rough idea about the temperature.

### Temperature sensor

Note that there is a difference between the two sensors, consisting of the fact that the temperature sensor has a linear resistance-temperature characteristic, while the thermistor usually has the output voltage as a logarithmic or exponential function. Also the formulas to find the temperature in Celsius degrees are different.

### Light Sensor

This is also a resistance that changes its value due to the amount of photons it receives. Measuring the voltage drop on the resistance, you can get an idea about the light intensity. How does it actually work? If there is much light, there will be many photons in the photocell, thus less electrons to stop the electric current, which means the resistance of the sensor is lower.

### Gas Sensor

This sensor detects CO2. Pay attention that in order to detect gas it uses an exothermic chemical reaction therefore it heats up so dont touch it while it is connected to the board.
More importantly pay attention that this sensor IS NOT for safety-important applications.

### Distance Sensor

This sensor works like a sonar. It has 4 pins. Two of them are the power pins: GND and VCC, and the other two are the trigger and the echo pins. The trigger will send out an ultrasonic signal and wait for the response. The moment the signal is sent out, the echo pin is 1, when the response comes, this pin turns to 0. The distance is measured by multiplying the time needed for the response to come with the speed of sound.

Another type of sensor is an infrared one. It has an ADC inside so computing the distance is after a simple formula.

## Digital sensors

Digital sensors work with either of the two protocols: SPI or I2C.

#### SPI

This protocol involves several slaves and a master. The communication is always initiated by the master. The pins for such a sensor are SS(slave select}, MISO, MOSI, Clock, each with their role.
MOSI stands for master out, slave in, which means that a line is created to send data from the master to the slave. MOSI is the line that facilitates the communication in the opposite direction: the slave writes on the line and the master samples.
The master is the one who generated the clock. SS pin will be 0 when the slave is active and conversely 1 when the slave is inactive. In the first case, the slave waits for the clock to transfer data. One master has pins for each slave, but it can't work with more than one slave at a time, since the MISO and MOSI lines would get impossible to use.
Communication in this protocol is always an exchange.

#### I2C

This protocol works with one master and several slaves as well. This time there is no slave selection, but each slave has a fixed address.
It only uses two lines: SDA and SCL: serial data and serial clock line. This protocol is called a half duplex as it only uses one line for communication. The transfer of data goes as follows. The master sends the address. The slave identifies itself, sends out and acknowledgement message, then the master writes or reads the data, all this on the same line. In general microcontrollers can be masters or slaves, but the computers can only be masters.

### Accelerometer and Gyroscope

See below an example of wiring and programming for a digital sensor.

Using this sensor you can determine acceleration on all three axis and also the rotation angle. An important note is that it also measures gravitational acceleration so don`t be surprised to get values different from 0 when the sensor is not moving.

## Peripherals

### LED

An LED is a light emitting diode. A diode allows the current to pass only in one direction. Also, it has no resistance, which means it will be in a short circuit once it is wired to a circuit with no resistor. You will see when you look at the LED that it has two legs. One is longer, that one is usually the anode. This one has to be connected to the GPIO pin of the Edison. The shorter leg should to be connected to the resistor and then to the ground pin of the board.

Although the position of the resistor is not fixed, it can can either connect the ground to the cathode or the anode to the GPIO pin, the cathode should be connected to the ground to obtain the usually desired behavior. That means that we want the LED to light when the GPIO is set to HIGH and not to light when the GPIO is set to LOW. If you put the legs the other way around, the effect will be the opposite.

### 7 segment display

It is an electronic component consisting of 7 LEDs. These have either a common cathode or common anode. In the first case, the LEDs need the value 1 on the pins to be alight, but in the second case, the VCC will be common. The latter situation means that the LEDs are alight when the value on the pins is 0.

How do you connect it?

In visual programming, there are special blocks for the piece. All you have to do is to look up your model's datasheet AND insert the number of the pins for each segment.

### Shift register

It is a serial to parallel register. Being a register, it is actually a memory. The data stored inside goes from QA to QH. These are the GPIO pins. OE stands for output enable, it is used to disconnect the pins all at once. SER is the serioal input pin, RCLK latch clock, SCLK is the serial clock, SRCLR is the clear pin. The data is easy to read, paralleled, but written by shifting it inside. When the serial clock switches from 0 to 1 the register reads the value in the SER, puts the value from QH into QH, QG into QH and so on until it's QAs turn to go into QB and SER is now stored into QA. You would need 8 cycles to change all the values from a register. The bits can be read all at a time, but not written this way.
Two shift registers can be connected together.
A shift register offers 8 outputs using only 2 lines: the clock and the SER. When SRCLR is 1, everything is cleared out. The shift register changes the values in the output as others come in, so this can be visible. To avoid this problem, good quality shift registers have a parallel to parallel register. This means that only when a cycle is complete the RCLK will switch and the values are given as output. This is why the latch clock is needed.

### LCD

An LCD with 16 colons and 2 rows needs 16 pins to be controlled. It can use either a 4 pin protocol or an 8 pin protocol. The former will need 12 pins and the last, all the 16 pins. There are 4 power pins, meaning two groups of GND and VCC, one group being used by the backlights. Any LCD uses a potentiometer to set the contrast. The enable pin of an LCD can be connected to the ground or to a microcontroller. There are also I2C LCDs that work with only 4 pins, have an integrated potentiometer and can be connected in the same circuit only if they have different addresses.