This shows you the differences between two versions of the page.
iot:labs:03 [2015/07/21 15:10] madalina.tanea [Add a button] |
iot:labs:03 [2017/09/11 09:34] (current) alexandru.radovici |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Lab. 03 - Blink an LED ====== | + | ====== Lab 3: Light Blink ====== |
- | ==== What you need ==== | + | ===== What you need ===== |
- | * One Edison connected to Wyliodrin; | + | * One Raspberry Pi connected to Wyliodrin STUDIO; |
* One LED; | * One LED; | ||
- | * One 200 Ohm resistor; | + | * One 220 Ohm resistor; |
- | * Two male-male jumper wires. | + | * Jumper wires; |
- | {{ :iot:labs:leed.png?80 |}} | + | * Breadboard; |
- | {{ :iot:labs:resist.png?100 |}} | + | {{ :iot2015:labs:leed.png?80 |}} |
+ | {{ :iot2015:labs:resist.png?100 |}} | ||
- | ==== The Setup ==== | + | ===== The Setup ===== |
- | Connect the LED to the Edison following the schematics in the figure. | + | Connect the LED to the Raspberry Pi following the schematics in the figure. |
- | You will see, when you look at the LED, that it has two legs. One is longer, that one is usually the anode. The latter has to be plugged into 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. | + | 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:ledblink.png?500 |}} | + | {{ :iot:labs:led-blink-schematics.png?300 |}} |
To understand the anode and the cathode of the LED, imagine these two following situations: | 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 pin, which has to be //High// in order to make the LED light up. | - The cathode is connected ,through the resistor of course, to the ground. This means that the anode will be wired to the GPIO pin, which has to be //High// in order to make the LED light up. | ||
Line 20: | Line 21: | ||
To sum it up, 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 behaviour. Meaning 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. | To sum it up, 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 behaviour. Meaning 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. | ||
- | ==== Streams programming language ==== | ||
- | 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. | + | ===== The Code ===== |
+ | Let's see the same application in three different programming languages. | ||
- | 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 fields: one will be the payload and a second one the topic. | + | ==== Visual Programming ==== |
- | Any action in Streams starts with the //run// node. As you can see in its description, this node will set a payload at a certain interval. More specifically, you can choose in its settings to send the message at a certain time interval or at a specific time. | + | First, you can try it out in Visual Programming. |
- | ==== The Code ==== | + | Here's an example: |
- | You go to the Wyliodrin Applications page and create a new application. You name it and select the Streams programming language, then, the example //Led Blink - Streams//. This will create an application that makes an LED connected to the pin 2 of the Edison blink. | + | {{ :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 loop. Inside it, there will be the blocks that turn on and off the LED on the chosen pin. There is also a // delay// block, which will insert here a pause of 0.5 seconds between the // on // state of the LED and the // off // one. | ||
- | Once created, you click on the new application's name to open it. A new window will open to display it. | + | ==== Python ==== |
- | {{ :iot:labs:blink2.png?400 |}} | + | |
- | In this example, the //run// node will send a message every 1 second to the node that says //blink//. Look in its settings. The //payload// field will be a number, incremented by one. The //blink// node is the one which holds the visual blocks. Double click on the latter to see the function inside. It will be in Visual Programming. | + | |
- | {{ :iot:labs:blinkblocks.png?600 |}} | + | |
- | What it does is to set a variable //new message// with the value of the data sent from this node to the next one to come. Now, the very message is composed like this: take the payload sent from the first node and divide it by 2. Which means the new message will be either 1 or 0. The function returns just one of these values, depending on the case. | + | |
- | The last node is simply the digital write function. It will ask as a parameter the pin number, so double click on it and specify the pin you are going to use. On this pin, it will write the message that was previously composed. | + | |
- | ==== Tips & Tricks ==== | + | |
- | We said that 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. | + | |
- | ==== Add a button ==== | + | |
- | You can easily add a button to turn on and off the LED. | + | |
- | === What you need === | + | |
- | * One Edison connected to \Wyliodrin; | + | 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. |
- | * Two 200 Ohm resistor; | + | <code python> |
- | * Six male-male jumper wires; | + | from wyliodrin import * |
- | * One button; | + | from time import * |
- | * One LED. | + | |
- | === Schematics === | + | |
- | To connect it, just think about the button as a voltage divider, the same as for the thermistor. Look in the introduction to electronics to properly understand the concept. {{ :iot:labs:buton.png?400 |}} | + | |
- | === The code === | + | |
- | You won't need to use more than 3 blocks. | + | |
- | {{ :iot:labs:button.png?500 |}} | + | |
- | * one is the usual //run// | + | |
- | * read the digital pin to which the button is connected | + | |
- | * write its value on the pin where the LED is wired | + | |
- | ==== Exercises==== | + | pinMode (0, 1) |
- | - Connect two LEDs and one button that will light them up one by one. | + | |
- | - Use one LED to write SOS using the international morse code. {{ :iot:labs:international_morse_code.png?300 |}} | + | |
+ | 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. | ||
+ | |||
+ | ==== Useful Functions ==== | ||
+ | You can use the following functions both in Python and JavaScript: | ||
+ | * **wyliodrin.pinMode (pin, mode)** - configures the specified pin to behave either as an input or an output; | ||
+ | * **wyliodrin.digitalWrite (pin, value)** - writes a HIGH or a LOW value to a digital pin; | ||
+ | * **wyliodrin.delay()** - pauses the program for the amount of time (in miliseconds) specified as parameter. | ||
+ | |||
+ | In order to use these function, import the **wyliodrin** module: | ||
+ | * **Python:** from wyliodrin import wyliodrin | ||
+ | * **JavaScript:** var wyliodrin = require ('wyliodrin'); | ||
+ | ===== 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===== | ||
+ | - Use one LED to write SOS using the international morse code using Python or JavaScript.{{ :iot2015:labs:international_morse_code.png?300 |}} | ||
+ | - Create a traffic lights system that has lights for cars and for pedestrians. | ||
+ | - Use [[http://openweathermap.org/api|Open Weather Map]] to get the temperature in Bucharest and light up one of the green, yellow or red LEDs depending on the temperature. **Note:**Use the **requests** module. |