Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
public:crocs:arduino [2014-10-24 21:47] – [Arduino code basics] lnemec | public:openlab:autumn2014:arduino [2015-01-05 09:40] – Page moved from public:openlab:arduino to public:openlab:autumn2014:arduino mukrop | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Arduino ====== | + | ====== |
+ | |||
+ | * Date: 31. 10. 2014 | ||
+ | * Workshop by: Lukáš Němec | ||
+ | * Cake: we will see... | ||
+ | * Cake by: Mirek Jaroš | ||
+ | * Discussion on A403 wallpainting | ||
+ | |||
+ | ===== What to expect ===== | ||
+ | |||
+ | Introduction to microcontroller programming, | ||
+ | |||
+ | As we learn enough of theoretic background, you will get your hands on Arduino boards and you will have chance to program your own blinking LED or something similar, depending on your time, skill and enthusiasm. | ||
===== What to prepare ===== | ===== What to prepare ===== | ||
Line 10: | Line 22: | ||
| | ||
There will be supply of leds, resistors, some sensors (light, temperature, | There will be supply of leds, resistors, some sensors (light, temperature, | ||
+ | |||
+ | ===== Arduino Theory ===== | ||
+ | {{ : | ||
+ | |||
+ | |||
+ | ==== Arduino Ecosystem ==== | ||
+ | |||
+ | Arduino is phenomenon of last few years, quickly gaining popularity in many fileds. It originated somewhere in DIY community as easily managable microcontroller with Open-Source design, that was accesible to everyone. With this huge succes, family of official Arduino board quickly grew, as well as all other products, that were somehow Arduino related. | ||
+ | |||
+ | Today There is huge selection of boards, starting from basic UNO, litle bit more advanced Leonardo, up to Arduino MEGA. On the other side, if you are interested in smaller sizes, there is Arduino micro, mini and nano, some that small, that there is no space for usb connectivity. Also, if you want to keep up with latest developement, | ||
+ | |||
+ | Also open source design of Arduino is by many viewed as perfect point to start with own modifications, | ||
+ | |||
+ | Concerning accesoris, there are many shields, which can add specific functionality to your arduino, for example GSM of WIFI shields. These become usefull, when you are building a diet control machine, that will publish tweet every time you open door to your fridge. Also there is wide range of sensors, so you can measure almost everything, from light and humidity to methane concentration. | ||
+ | |||
+ | ==== How to connect everything together ==== | ||
+ | |||
+ | Every arduino has pins, usually with sothered sockets, but you can also buy just bare PCBs and make sothering yourself, if you ever need to do so. There are three basic categories of pins: analog, digital and power + some special purpose ones. Pins are usually numbered or labeled, and you can see these directly on the board. | ||
+ | |||
+ | ===Digital pins=== | ||
+ | Use digital signal, thus input and output has only two possible values. These are 1 and 0, or in practical view, 5V or nothing. Digital pins can be also used to read data, but again only two states, so ideal choice for switches. To make things little bit more tricky, some digital pins have pulse-width modulation (PWM) enabeled and these are usually labelled with ∼. These pins can be used for analog write commands, in other way, output can be set anywhere between 0 and 255. These can be used for a dimmer or precise positioning of servo motors. | ||
+ | |||
+ | ===Analog pins=== | ||
+ | Analog pins are used for analog read, when we need more precise value than just binary 0 or 1. Output from these pins can be anything between 0 to 1023 and we use these pins for reading values from sensors, e.g. humidity, ligth, distance etc. | ||
+ | |||
+ | === Power and special pins === | ||
+ | |||
+ | Last category of pins are power pins, these are ground pins, 5V and 3.5V pins. Connect these to power breadboard power lines or components directly, as needed. | ||
+ | |||
+ | All other pins are used for special purposes, like reset pin or in case of smaller or older versions of arduino for actual programming (USB interface is not present). | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | ==== How to upload code ==== | ||
+ | Recomended steps: | ||
+ | |||
+ | - Connect arduino to PC, usually USB cable type B, or micro USB with new boards. | ||
+ | - Start Arduino IDE | ||
+ | - Select Tools - Board Type (mostly Arduino UNO) | ||
+ | - Select Tools - Serial Port (there will be usually only one, when only one arduino board is connected) | ||
+ | - Write your code or open some example sketches (or load some saved sketches) | ||
+ | - Do all the wiring (can be done as first step or as last) | ||
+ | - Press Ctrl + R to compile | ||
+ | - Press Ctrl + U to upload code to your arduino | ||
+ | - Optionally repeat while fixing bugs or adding features (two names for the same thing) | ||
===== Arduino code basics ===== | ===== Arduino code basics ===== | ||
Programming for arduino is basically C language with some specific commands and libraries. | Programming for arduino is basically C language with some specific commands and libraries. | ||
+ | |||
+ | === Introduction === | ||
Smallest code, that sucessfully compiles looks like this: | Smallest code, that sucessfully compiles looks like this: | ||
Line 24: | Line 83: | ||
</ | </ | ||
Here you have two functions, one for initial setup of variables, pins etc, and loop function that runs in infinite loop after setup as it's name suggests. | Here you have two functions, one for initial setup of variables, pins etc, and loop function that runs in infinite loop after setup as it's name suggests. | ||
+ | |||
+ | === Blinking LED === | ||
Now for some code, that actually does something usefull (blinking LED): | Now for some code, that actually does something usefull (blinking LED): | ||
Line 48: | Line 109: | ||
This code also requires some basic assembly, LED needs to be connected to arduino, it is recomended to use breadboard and some jumper wires, exact way is illustrated on image below. | This code also requires some basic assembly, LED needs to be connected to arduino, it is recomended to use breadboard and some jumper wires, exact way is illustrated on image below. | ||
- | <note important> | + | <note important> |
- | <note warning> | + | <note warning> |
{{ : | {{ : | ||
+ | |||
+ | === Sensors, Analog input and Serial monitor === | ||
+ | |||
+ | Next example will show, how to read values from sensors and how to obtain debugging information from Arduino board. | ||
+ | |||
+ | In order to receive value from sensor, we have to use analog input pins (Arduino UNO has range from A0 to A5) and read values with analogRead function. There are many posibilities what to use as a source of input, you can choose from whatever is avaiable, rotary potentiometer, | ||
+ | |||
+ | Every sensor (save he more complicated ones) has basically three connections, | ||
+ | |||
+ | Next is serial output, which can be used either for debugging or logging values. Output needs to be inicialized in setup phase with following '' | ||
+ | |||
+ | <code C> | ||
+ | int sensorPin = A0; // select the input pin for the potentiometer | ||
+ | int sensorValue = 0; // variable to store the value | ||
+ | |||
+ | void setup() { | ||
+ | // | ||
+ | pinMode(sensorPin, | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // read the value | ||
+ | sensorValue = analogRead(sensorPin); | ||
+ | //print value on serial | ||
+ | Serial.println(sensorValue); | ||
+ | //wait | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | ===== How to continue ===== | ||
+ | |||
+ | ==== Ideas, where to get them ==== | ||
+ | As stated previously, idea is begining of everything, thus you need ideas what to do with Arduino. Hopefully you can come up with many of your own, but if you need some inspiration boost, following might help: | ||
+ | * Arduino Started Kit (book of 15 tutorials together with Arduino board and parts you will need) | ||
+ | * Other official Arduino Examples and already finished projects http:// | ||
+ | * Makezine videos, or paper issue: http:// | ||
+ | * many other, just search for " | ||
+ | |||
+ | ==== What to buy ==== | ||
+ | You will need something to start with, either you already have some electronics, | ||
+ | |||
+ | === Essential === | ||
+ | |||
+ | * Arduino Board | ||
+ | * some basic electronic parts (LEDs, resistors) | ||
+ | * breadboard | ||
+ | * jumper wires | ||
+ | |||
+ | === Nice to have === | ||
+ | |||
+ | * variety of sensors | ||
+ | * All other usefull parts (depending on project this can be servo motors of Liquid crystal display etc.) | ||
+ | * multimeter | ||
+ | * sothering iron (when you want to make project permanent) | ||
+ | * more Arduino boards (more projects at once becomes easier) | ||
+ | |||
+ | |||
+ | ==== Where to buy ==== | ||
+ | * Official Arduino strore - arduino.cc, based in Italy, sells Arduino boards, shields, sensors | ||
+ | * GM electronics - gme.cz, overpriced Arduinos, but nice for small parts like LEDs, based in Brno, so ideal if you are missing just one part to complete project | ||
+ | * dealExreme.com - dx.com, china based, cheap, not original Arduinos, but Funduinos etc. (so called Arduino compatible parts), takes about 4 weeks to arrive, ideal for stocking up on parts of budget buys of boards | ||
+ | |||
+ | ===== JeeNodes ===== | ||
+ | |||
+ | Jeenodes are special purpose boards (Arduino based) for WSN networks. Because of these facts, there is limited number of pins, and some other limitations, | ||
+ | |||
+ | First connections, | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | |||
+ | And now the other part, small change in source code: | ||
+ | |||
+ | <code C> | ||
+ | //pin placement is changed to 4 | ||
+ | int ledPin = 4; | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | digitalWrite(ledPin, | ||
+ | delay(1000); | ||
+ | digitalWrite(ledPin, | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Other projects are changed acordingly. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||