# Differences

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

iot:labs:05 [2015/07/20 17:27]
madalina.tanea [See how the light changes in a room]
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 5: Sensors 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 Pi, please note that  you will need to use an Arduino as well.  +==== What you need ====
-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.  +  * One Raspberry Pi board connected to Wyliodrin STUDIO;
-==== Build a virtual thermometer ​====+  * One Arduino board;
+  * One USB cable
+==== The setup ====
+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 connectivity. So you will use the board to gather data from the environment and then pass it on to the Raspberry Pi.  ​

-=== What you need ===+The Raspberry Pi is a computer that is capable of processing data and communicating with other smart devices. For instance, ​you could visualise the temperature on your smart phone.

-  ​One temperature sensor;  +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 a certain action or information and the Arduino will respond accordingly.
-  ​Three jumper wires; +
-  ​One Edison connected to Wyliodrin. +
- ​=== ​The setup === +
-{{ :​iot:​labs:​tempstreams.png?300 |}}+

-In this figure you can see a 3-leg temperature sensor.  +In order to implement ​the protocolyou need to flash the Arduino with the **StandardFirmata** firmware.
-There is an alternative ​to this one, the thermistor. The 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 pinThis sensor consists of a thermistor and several other electronic pieces. The leg situated in the middle outputs a different voltage depending ​on the temperature.+In the Wyliodrin STUDIO interface you have two tabs referring ​to **SOFTWARE** and **FIRMWARE**. So far, you used only the **SOFTWARE** tabas you wrote applications for the Raspberry Pi solelyNow, 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.

-=== The Code === +{{ :​iot:​labs:​select-firmata.png?300 |}}
-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. ​ \\ +Hit the **Show examples** button and select **Arduino**. Then **use** the **Firmata/StandardFirmata** exampleNow that you have the software to run on the Arduino, once 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.
-**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?​300 |}} +
-The first mandatory one, which is //run//. The next one will read the value that comes from the analog pin, in 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 aboveThe resulting value is stored in the //​temperature//​ variable+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 a new Raspberry Pi applicationIf you are confident that the Arduino is running **StandardFirmata**,​ you can skip this step.
+{{ :​iot:​labs:​flash-arduino.png?300 |}}

-To write the formula you can use either a visual node or a formula node. Choose the latter and the code inside should look like the one in the next figure. +===== Street Lightning =====
-{{ :​iot:​labs:​tempfct.png?​300 |}}+

-As promised, you will now display ​the temperature ​on a thermometer using the //send signal// node+In this application, you will build a system to monitor ​the light level and if there is the case, you will turn on the street lights brighter or dimmer, depending ​on the amount of light.

-In dashboard choose ​the thermometer and assign it a signal with the same name as use the nodes +==== What you need =====
-{{ :​iot:​labs:​dashtermo.png?300 |}}+  * 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

-The //send signal// node stores the value it receives from the previous block and sends it further to the dashboard, where a graph with the exact same name as the node's signal is searched. Once found, the connection is established. The node will receive the signal the data, store it and send it to the graph via a signal.  ​+==== The Setup ====

-=== Use a thermistor === +{{ :​iot:​labs:​light_sensor_schematics.png?600 |}}
-As mentioned at the beginning, the same project can be easily built with a thermistor+

- Note that there is difference between ​the two sensorsconsisting of the fact that the temperature sensor previously used has linear resistance-temperature characteristicwhile the other one usually has the output ​voltage as a logarithmic or exponential function+The photocell works just like resistor with variable resistance. Depending on the amount of light it receivesits resistance gets higher or lower. As resultyou can connect it in a voltage ​divider, ​as well.

-The wiring ​is quite simple, just follow ​the schematics of a 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
-{{ :​iot:​labs:​thermistored.png?​300 |}} +
-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 valueyou just have to apply the following formulas and you will obtain ​the temperature in Celsius degrees. +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.
-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?​300 |}} +
-In Visual Programming,​ the same function would look like this: +
-{{ :​iot:​labs:​termivisual.png?​300 |}} +
-==== See how the light changes in a room ==== +
-You that now have a thermometer can you find something extra to make it more interesting?​ Try a light sensor.  +
-  *  One photocell;​ +
-  *  One 220 $\Omega$ resistor; +
-  *  Three jumper wires. +
- === The Setup === +
-{{ :​iot:​labs:​light.png?300 |}}+

-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 boardThis 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 a pull up or pull down voltage divider+In addition, the photocell ​has resistance varying from hundred of ohms to mega ohms and in order for the voltage divider ​to workyou 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 visibleThis is why you need a 10 k Ohms resistor.
-=== 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.  +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 3That pin has a 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 pins0 being the equivalent ​of digital 0 and 255 the equivalent of digital 1.
-{{ :iot:labs:lightstreams.png?300 |}}+==== The Code ===
+
+{{ :​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 LED. For 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=====
+  - Display the values coming from the light sensor in the dashboard using the **gauge** graph.
+  - 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 sensor, which works in a different way.
+  - Search the temperature sensor'​s datasheet online (TMP36) and transform the read values to celsius degrees.

-==== Exercises====
-  - Ordered List ItemConnect 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.
-  -   * 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.  ​
-  - 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 board, you will need its Board ID. To find it, go to the main IOT page and in the boards settings, choose BoardID.
-  -   - 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. ​