# Differences

This shows you the differences between two versions of the page.

iot:labs:05 [2015/07/24 18:46]
iot:labs:05 [2017/09/11 09:35] (current)
Line 1: Line 1:
-====== Lab 05. Connecting Analog ​Sensors ​to the board and adding charts======+====== Lab 5Sensors and Dashboard======

-At the end of this project ​you will know how to get data from different ​sensors ​you can place around your home and display the gathered data on graphs.  +For the following applications, ​you will read data coming ​from sensors, ​which means that you will also need to use the Arduino board. ​As the Raspberry Pi has no ADCs, you cannot read values coming from analog sensors.
- +===== Arduino Setup =====
-In case you want to use a Raspberry Piplease note that  you will need to use an Arduino as well.  +
-No analog pin dependent piece can be used with your Raspberry Pi only. You will need the help of an Arduino board. +
- +
-Let's begin with the temperature sensor.  +
-===== Build a virtual thermometer ​=====+

==== What you need ==== ==== What you need ====

-  * One temperature sensor;  +  * One Raspberry Pi board connected to Wyliodrin STUDIO
-  * Three jumper wires+  * One Arduino board
-  * One Edison connected to Wyliodrin.+  * One USB cable.
==== The setup ==== ==== The setup ====
-{{ :​iot:​labs:​tempstreams.png?450 |}}+As previously stated, the classical Arduino board is basically a micro-controller,​ which is capable of running one piece of software at once and that has little processing power and no network connectivitySo you will use the board to gather data from the environment and then pass it on to the Raspberry Pi.  ​

-In this figure you can see 3-leg temperature sensor.  +The Raspberry Pi is computer that is capable of processing data and communicating with other smart devicesFor instanceyou could visualise ​the temperature on your smart phone.
-There is an alternative to this one, the thermistorThe latter has two legs and it should be connected as a voltage dividerwhich is explained in the electronics chapter+

-In this case, the sensor needs to be connected to the GND pin, the VCC and an analog pin. This sensor consists of a thermistor and several other electronic pieces. The leg situated in the middle outputs ​different voltage depending on the temperature.+The two boards need to be connected ​in order to send data between them. You can connect them via the USB cable and a serial connection will be established between ​the two. Once this is done, they can exchange data and the Arduino board can be controlled via the Raspberry Pi. This is done by the **firmata** protocol. The protocol allows ​the Raspberry Pi to send the Arduino messages in which it requests for certain action or information and the Arduino will respond accordingly.

-==== The Code ==== +In order to implement ​the protocol, ​you need to flash the Arduino with the **StandardFirmata** firmware.
-In order to get the values from the the temperature sensor ​you will just have to use the usual //Analog read// blocks and read the analog pin. The values that you obtain are in a range of 0 to 1023. However, this doesn'​t look at all like the values you would expect from a thermometer+

-To transform these values into Celsius degrees, ​you are going to use this formula. ​ \\ +In the Wyliodrin STUDIO interface ​you have two tabs referring ​to **SOFTWARE** and **FIRMWARE**. So faryou used only the **SOFTWARE** tabas you wrote applications for the Raspberry Pi solely. Now, select ​the **FIRMARE** tab and there is where you can write code which will be run on the Arduino. In this case, you will import an existing project.
-**temperature= (value*5/​1024-0.500)*100**\\ +
-How does it look in Streams programming language? You need no more than 3 nodes. +
-{{ :​iot:​labs:​tempsenz.png?​400 |}} +
-The first mandatory onewhich is //run//. The next one will read the value that comes from the analog pinin this case A0. Now the payload of the message sent will be this very value, between 0 and 1023. Now, you have to convert it to Celsius degrees.+

-To get from this value to Celsius degrees you have to follow the formula above. The resulting value is stored in the //​temperature//​ variable+{{ :​iot:​labs:​select-firmata.png?300 |}}

-To write the formula you can use either a visual node or a formula nodeChoose ​the latter and the code inside should look like the one in the next figure. +Hit the **Show examples** button and select **Arduino**Then **use** ​the **Firmata/​StandardFirmata** exampleNow that you have the software to run on the Arduinoonce you hit the **run** button, ​you will be asked the tyype of the Arduino board and if you want to flash it or not. Select ​the board you are using and **RUN AND FLASH**The **StandardFirmata** firmware ​will be deployed on the Arduino.
-{{ :​iot:​labs:​tempfct.png?​400 |}} +
-In this JavaScipt function ​you create an object called //​new_message//​ and a variable with the value of the message from the payload. Next, you apply the formula from which you obtain ​the temperature in degrees ​and make it the message to be sent forwardYour function ​will return this very message+

-As promised, ​you will now display ​the temperature ​on a thermometer using the //send signal// node+As you know, any micro-controller,​ including ​the Arduino, once flashed, runs the same firmware until another one is uploaded ​on the board. Thus, you don't have to flash the board each time you run new Raspberry Pi application. If you are confident that the Arduino is running **StandardFirmata**,​ you can skip this step.
+{{ :​iot:​labs:​flash-arduino.png?300 |}}

-In dashboard choose the thermometer and assign it a signal with the same name as use the nodes +===== Street Lightning =====
-{{ :​iot:​labs:​dashtermo.png?​400 |}}+

-The //send signal// node stores the value it receives from the previous block and sends it further to the dashboardwhere graph with the exact same name as the node's signal is searched. Once foundthe connection is established. The node will receive ​the signal the datastore it and send it to the graph via a signal +In this applicationyou will build system to monitor ​the light level and if there is the caseyou will turn on the street lights brighter or dimmerdepending on the amount of light.

-==== Use a thermistor ​==== +==== What you need =====
-As mentioned at the beginning, the same project can be easily built with a thermistor+  * One Raspberry Pi connect to \Wyliodrin;​
+  * One Arduino connected to the Raspberry Pi;
+  * One photocell;​
+  * One LED;
+  * One 220 $\Omega$ resistor;
+  * One 10 k$\Omega$ resistor;
+  * Jumper wires

- Note that there is a difference between the two sensors, consisting of the fact that the temperature sensor previously used has a linear resistance-temperature characteristic,​ while the other one usually has the output voltage as a logarithmic or exponential function. ​+==== The Setup ====

-The wiring is quite simple, just follow the schematics of a voltage divider.  +{{ :iot:labs:light_sensor_schematics.png?600 |}}
-{{ :iot:labs:thermistored.png?400 |}} +
-For the code you will need to know that this time the formulas you use are different to convert the values into Centigrade.\\  +
-**V_m = analogRead(pin) * 5/​1023**\\ +
-**R_t = 5/V_m * 10000**\\ +
-**Ratio = 1/B* ln(R_t/​10000)**\\ +
-**temperature = 1/(1/298 + Ratio)**\\ +
-B = 4050, is one of the constants that comes with the sensor'​s specifications.+

-Vm is the voltage that you get in your analog input while Rt is the resistance of the thermistor. By having this value, you just have to apply the following formulas and you will obtain the temperature in Celsius degrees. +The photocell works just like resistor with a variable ​resistanceDepending on the amount ​of light it receivesits resistance gets higher or lowerAs result, you can connect ​it in voltage divider, as well.
-You only have to calculate what the formulas above will give and put it in a variable. ​Send the value of this variable to the thermometer in the Dashboard.  +
-The only difference will be in the function that you use to do the conversion. +
-{{ :​iot:​labs:​termifct.png?​450 |}} +
-In Visual Programmingthe same function would look like this: +
-{{ :​iot:​labs:​termivisual.png?450 |}} +
-===== See how the light changes in room ===== +
-You that now have a thermometer ​can you find something extra to make it more interesting?​ Try light sensor.  +
-  *  One photocell;​ +
-  *  One 220 $\Omega$ resistor; +
-  *  Three jumper wires. +
-==== The Setup ==== +
-{{ :​iot:​labs:​light.png?400 |}}+

-The photocell works just like a voltage divider, as well (check ​the //​Introduction ​to Electronics//​ chapter for more details)You need to connect it to the Gnd and Vcc and from between ​the photocell and the resistor ​to the analog ​pin of the board. ​This light sensor has a resistance ​that varies ​with the light, the more light there is, the lower the resistance of the photocell. Depending on how you place the sensor you can obtain ​pull up or pull down voltage divider.+The sensor is connected to the 5V via the 10 K Ohms resistor and to the GND pin on the other sideIn order to read the sensor'​s value, ​the yellow jumper wire is connected ​to the A0 pin of the board. ​The A0-A5 pins can be used for reading digital values, in this case values ranging from 0 to 1024.
+
+For the photocell, the resistance ​decreases proportionally ​with the amount of light, so for this schematics, the brighter the environment, the higher is the read value.
+
+In addition, the photocell has a resistance ​varying from hundred ​of ohms to mega ohms and in order for the voltage divider to work, you should use the sensor ​as a pull-down resistor, while the actual resistor needs to have a resistance comparable with the sensor'​s,​ otherwise its effect is not visible. This is why you need a 10 k Ohms resistor.
+
+The other part of the setup, the LED is connected similar to the previous schematics except that you can notice that its behaviour is controller by pin 3. That pin has tilde next to it. That means that it is a PWM pin so you can control if the LED should light up brighter ​or dimmer. You can write values ranging from 0 to 255 on these pins, 0 being the equivalent of digital 0 and 255 the equivalent of digital 1
==== The Code ==== ==== The Code ====
-The new nodes are much simplerYou just add a block to read the analog pin that you use and send it to the chart.  +
-{{ :iot:labs:lightstreams.png?400 |}}+{{ :​iot:​labs:​street_lights_code.png?​400 |}}
+
+First of all, you need to use an **Arduino in** node in order to read the values from the sensor. This node gets triggered each time the value on the pin it reads from changes. To actually configure the node's behavior you need to set its properties.
+
+{{ :​iot:​labs:​arduino_in_properties.png?​400 |}}
+
+The first property allows you to set the Raspberry Pi connection to the Arduino board so the Raspberry Pi knows the port through which it should send the messagesUsually, the port is **/​dev/​ttyACM0**. However, in order to make sure, go to the **Shell** tab and type **dmesg**. There you should see messages stating that an FTDI cable has been connected to the board and the port it is connected to. Concatenate that value to **/dev/** and place the value as the first property of the node.
+
+Further on, you need to specify which pin you want to read from. In this case, the pin is 0 and is an analog pin. Afterwards, ​you will use a **range** node in order to map the read values to values that should light up the LEDFor this, you need to adapt the system to your environment. Depending on the regular amount of light, usually you will read a value of around 400. So in this case, the range scales values going from 400 to 1024 to 0 to 255. However, if the regular value is different, you will need to scale a different range.
+
+{{ :iot:labs:range-light.png?400 |}}
+
+The last node to use is **Arduino out**. This node acts just like the **digital write** node used in the previous chapters. However, you still need to select the port for the Arduino board and you can choose from analog or digital write. As you want to make the LED light up brighter or dimmer, you will set the type to **analog** and the **pin** property has to be 3.
+
+<​note>​
+If you use more than one Arduino node in the application,​ add a new Raspberry Pi board only for one node. For the rest of the nodes, choose the previously added board from the drop-down.
+</​note>​
+
+Now you can run the application and watch how the LED gets brighter as you cover up the sensor. ​

===== Exercises===== ===== Exercises=====
-  - Connect a photoresistor. Send the values ​to a chart. Set a maximum value. In case the sensor ​detects a value higher that the maximum, an LED will turn on for a few seconds. +  - Display ​the values ​coming from the light sensor in the dashboard ​using the **gauge** graph
-  -  Do the same, but set the limit value from a slider, ​in the dashboard. ​This slider gives values between 0 and a maximum of your choice.  ​ +  - Connect all the sensors in the kit to the Arduino ​and display the values ​you read in the dashboard. **Note:** Do not connect the Hall sensorwhich works in a different way
-  - For this exercise you will have to make a chart using the values of a photoresistor as well, only that this time, the team next to you will have the sensor ​and they will send a signal with its values. You will receive this values and send them to the dashboard. ​In short, make a line chart using the values of a photoresistor connected to the neighbour'​s board. \\ {{ :​iot:​labs:​boardid.png?​300 |}} \\  ​** Hint** In order to get a message from another boardyou will need its Board ID. To find it, go to the main IOT page and in the boards settings, choose BoardID+  - Search ​the temperature sensor'​s ​datasheet online (TMP36) and transform the read values ​to celsius ​degrees.
-  -  Use a thermistor. You need to find online ​the value // B // and it'​s ​resistance at 25 degrees. Put its values ​in degrees ​in a thermometer chart. ​ \\ **Hint** You will wire it as a voltage divider with a similar resistance+