Differences

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

Link to this comparison view

iot:labs:06 [2016/06/23 23:52]
ioana_maria.culic
iot:labs:06 [2017/09/11 09:39] (current)
alexandru.radovici created
Line 1: Line 1:
-====== Lab 3Light Blink ======+====== Lab 6Connect and use an LCD ======
  
 +In this project you are going to connect an LCD to your board and write a short message on it.
 ===== What you need ===== ===== What you need =====
-  ​* One Raspberry Pi connected to Wyliodrin STUDIO; + 
-  * One LED+  ​ One Raspberry Pi connected to Wyliodrin STUDIO; 
-  * One 220 Ohm resistor;+  *  One 16x2 LCD
 +  *  One breadboard;
   * Jumper wires;   * Jumper wires;
-  * Breadboard; 
- {{ :​iot2015:​labs:​leed.png?​80 |}} 
-{{ :​iot2015:​labs:​resist.png?​100 |}} 
- 
  
 ===== The Setup ===== ===== The Setup =====
-Connect the LED to the Raspberry Pi following the schematics in the figure. +{{ :iot:labs:lcd-expander.jpg?500 |}} 
- +The LCD in the kit is connected to a port expander (MCP23008). This allows ​the LCD to be controlled via the I2C protocolthus reducing ​the number ​of pins you need to connect ​the LCD to. In this caseyou just need to connect the expander ​to GND5V, SCL and SDA pins, according ​to the indicators on the expanderYou can find the appropriate pins in the **Pin Layout** tab. You just need to connect ​the SDA port on the port expander with the SDA pin on the board and so on.
-First of all, you need to distinguish the anode from the cathode. The LED has two legs: one is longer, that one is usually the anode and the shorter one is the cathode. Another way of identifying them is by looking inside the LED. There are two metal parts in it. The smaller one is connected to the anode and the bigger one is connected to the cathode.  +
-{{ :iot:labs:led-blink-schematics.png?300 |}} +
-To understand ​the anode and the cathode of the LED, imagine these two following situations:  +
-  -  The cathode ​is connected ​,through the resistor of course, ​to the ground. This means that the anode will be wired to the GPIO pinwhich has to be //High// in order to make the LED light up. +
-  - The anode is connected to the VCC pin of the board. Which means that, should ​you want to light up the LED, the cathode has to be connected to the GPIO setted to //Low //Don't forget ​this kind of a circuit also requires a resistor.  +
- +
-To sum it upalthough the position of the resistor is not fixed, it can can either ​connect the ground ​to the cathode or the anode to the GPIO pinthe cathode should be connected ​to the ground to obtain ​the usually desired behaviourMeaning you 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 +
 ===== The Code ===== ===== The Code =====
-Let's see the same application in three different programming languages.  +{{ :iot:labs:lcd-simple-example.png?600 |}} 
- +You go to the Wyliodrin Applications page and create ​a new applicationYou name it and select Streams as programming languageOnce created, you click on the new application'​s ​name to open it.
-==== Visual Programming ==== +
-First, you can try it out in Visual Programming.  +
-Here's an example:  +
-{{ :iot2015:labs:blink-application.png?300 |}} +
-The first two blocks will simply print on the screen the messages written inside them.  +
-The // repeat while true// blocks will create ​an endless loopInside ​it, there will be the blocks that turn on and off the LED on the chosen pinThere is also a // delay// blockwhich will insert here a pause of 0.5 seconds between the // on //  state of the LED and the // off // one. +
- +
-==== Python ==== +
- +
-An important feature of Visual Programming in Wyliodrin is that you can easily get the Python code for your projects, just by clicking the // Show code// button. ​  +
-<code python>​ +
-from wyliodrin import * +
-from time import * +
- +
-pinMode (0, 1) +
- +
-print('​Led ​on pin 0 should blink'​) +
-print('​Press the Stop button to stop'​) +
-while True: +
-  digitalWrite (0, 1) +
-  sleep ((500)/​1000.0) +
-  digitalWrite (0, 0) +
-  sleep ((500)/​1000.0) +
-</​code>​ +
- +
-First of all, you need to import the //​wyliodrin//​ and //time// modules that will allow you to use some simple functions. +
- +
-Firstly, you have to declare the pins you are going to use and their purpose. In this case, pin 0 is used as //OUTPUT//. This is what //​pinMode(0,​1)//​ does (1=OUTPUT, 0=INPUT).  +
- +
-Next, there are the //print// functions that simply write the text in the shell. +
- +
-Another useful function is //​digitalWrite//​. The function receives as parameter the pin and the value. As you are doing a digital write, the value can be either 0 or 1. If the value is 0, the pin will have no current. If the value is 1, there is current. +
- +
-The //sleep// function makes the program wait for the desired number of seconds. +
- +
-All this code is enclosed within the //while True// loop that makes it to be run forever, until the application ​is stopped. +
- +
-==== Streams Programming ==== +
- +
-Now that you've seen both Visual and python, let'​s ​move on to Streams.  +
- +
-Streams is an inbuilt programming language used in Wyliodrin to create many complex IoT projects+
  
-It is quite different from  usual programming languages, and it can sometimes contain visual blocks as functions.+First of all, you need to initialise the LCD. For this, you drag the **run** node and select **Fire once at start** so you make sure that once you run the application a first message ​is sent. This is only required to trigger the **init LCD** nodeso you can assure there is no other message being sent again. For this, drag the **trigger** node and double click on it.
  
-This programming language works not as an imperative one, but is based on data flow. The nodes will send messages to one another. You can look at these messages as JSON objects. Any message has two important fieldsone will be the payload and a second one the topic+{{ :iot:labs:trigger-block.png?400 |}}
  
-You go to the Wyliodrin Applications page and create a new application. You name it and select ​the Streams programming languagethen, the example //Led Blink - Streams//. This will create an application that makes an LED connected to the pin 2 of the Edison blink.+You need to set this node to **wait until reset**. This will make the node to send the first message ​and then wait until it receives a message containing ​the property **reset**which will never happen.  
 +Next, the **init LCD** node initializes ​the LCD.
  
-Once created, you click on the new application'​s name to open itA new window will open to display ​it. +First of all, you need to require ​the **wyliodrin** module, which exposes some simple functions in order to control the LCDOtherwise you would need to implement the protocol in order to control ​it. 
-{{ :​iot:​labs:​ledblinkstreams.png?​400 |}}+
  
-Any action in Streams starts with the **run** node. As you can see in its descriptionthis node will set a payload at a certain interval. More specifically,​ you can choose ​in its settings ​to send the message at certain time interval or at a specific time.+Once you have the **wyliodrin** variablecall the **LiquidCrystal** constructor ​in order to obtain ​**LiquidCrystal** object. The constructer receives 0 as parameter.
  
-For this project, you need to specify ​the node to send the number of **times** it has been blinkingrather than the timestamp. To do that double click the node and select **times** for the payload. This way the node will send a payload with value 0 when it first firesthen 1followed by 2 and so on.+Why is the value 0 the parameter of the contructor? This type of LCD communicates with the Raspberry Pi board via that I2C protocol. Taking into account how the I2C protocol behaves, you would need to mention ​the bus specific ​to the LCD, that usually is 32. However, in order to be able to connect multiple LCDs at once, they have three jumpers which can be connected in order to represent values from 0 to 7, thus being able to identify eight different LCDs. This is the value you need to pass to the constructorin this caseno jumper is connected ​so we passed the value 0.
  
-In this example, the **run** node will send a message every 1 second to the **function** node. Double click on the latter to control its behaviour. The **function** block allows you to write JavaScript code in order to analyze the received message and send some message further on.+{{ :​iot:​labs:​init-lcd.png?400 |}}
  
-What the code has to do is to take the payload ​from the message sent by the first node (msg.payload), divide it by 2 and store the result in the message payloadThis means the new message will be either 1 or 0. The function returns just one of these valuesdepending on the case+What is new at the code presented above is the **context.global.lcd** structure. You need to use this because you want the **lcd** variable to be available ​from other nodes also (Display on LCD node) and **context.global** is a variable accessible to all the nodesso you just set the lcd property and you will be able to use it from any other node.
  
-{{ :iot:labs:blink_function.png?300 |Blink function}}+{{ :iot:labs:display-lcd-simple.png?400 |}}
  
-The last node is simply the digital write function. It will ask for the pin number as parameterso double click on it and specify pin 0, the one the LED is connected toThe node will write the value received (0 or 1) on the selected pin. +The **display LCD** node is another ​function ​node which first clears ​the LCDthen prints a string ​on it. Notice ​that the global **lcd** variable ​is used.
-===== Tips & Tricks ===== +
-Usually the longer leg is the anode and the shorted one is the cathode. However, this is not standard, to make sure which is which, you should know that the cathode leg is always connected to the bigger part inside the LED.+
  
 ===== Exercises===== ===== Exercises=====
-  - Use one LED to write SOS using the international morse code. {{ :​iot2015:​labs:​international_morse_code.png?300 |}}+  - Use a temperature sensor and display ​the temperature on the LCD.
iot/labs/06.1466715154.txt.gz · Last modified: 2016/06/23 23:52 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0