Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
public:crocs:arduino [2014-10-24 23:47] – [Arduino code basics] lnemec | public:openlab:autumn2014:arduino [2016-12-01 14:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Arduino ====== | + | ====== |
+ | |||
+ | * Date: 31. 10. 2014 && 11.3.2016 | ||
+ | * Workshop by: Lukáš Němec | ||
+ | * Cake: we will see... | ||
+ | |||
+ | ===== What to expect ===== | ||
+ | |||
+ | Introduction to microcontroller programming focused mainly on Arduino platform. We will start with basics, what to expect from Arduino and what is Arduino capable of with some examples. Then theoretic part, how to connect everything together and mainly how to write programs. Thus everything from using basic C skills to Arduino-specific functions. | ||
+ | |||
+ | As we learn enough of theoretic background, you will get your hands on Arduino boards and you will have a chance to program your own blinking LED or something similar, depending on your time, skill and enthusiasm. | ||
===== What to prepare ===== | ===== What to prepare ===== | ||
Line 9: | Line 19: | ||
- Google and own imagination ;-) | - Google and own imagination ;-) | ||
| | ||
- | There will be supply of leds, resistors, some sensors (light, temperature, | + | There will be a supply of LEDs, resistors, some sensors (light, temperature, |
+ | |||
+ | ===== Arduino Theory ===== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ==== Arduino Ecosystem ==== | ||
+ | |||
+ | Arduino is a phenomenon of last few years, quickly gaining popularity in many fields. It originated somewhere in DIY community as easily manageable microcontroller with Open-Source design, that was accessible to everyone. With this huge success, a 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, a little 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 the latest development, | ||
+ | |||
+ | Also open source design of Arduino is by many viewed as the perfect point to start with own modifications, | ||
+ | |||
+ | Concerning accessories, | ||
+ | |||
+ | ==== How to connect everything together ==== | ||
+ | |||
+ | Every Arduino has pins, usually with other 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) enabled 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. The output from these pins can be anything between 0 to 1023 and we use these pins for reading values from sensors, e.g. humidity, light, distance etc. | ||
+ | |||
+ | === Power and special pins === | ||
+ | |||
+ | The 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 a case of smaller or older versions of Arduino for actual programming (USB interface is not present). | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | ==== How to upload code ==== | ||
+ | Recommended 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 the 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 25: | Line 82: | ||
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. | ||
- | Now for some code, that actually does something | + | === Blinking LED === |
+ | |||
+ | Now for some code, that actually does something | ||
<code C> | <code C> | ||
//global constant for more comprehensible code, you can use just number, | //global constant for more comprehensible code, you can use just number, | ||
- | //this way it is just more user friendly when handling multiple pins | + | //this way it is just more user-friendly when handling multiple pins |
int ledPin = 13; | int ledPin = 13; | ||
Line 46: | Line 105: | ||
</ | </ | ||
- | This code also requires some basic assembly, LED needs to be connected to arduino, it is recomended | + | This code also requires some basic assembly, LED needs to be connected to Arduino, it is recommended |
+ | |||
+ | <note important> | ||
+ | <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 possibilities what to use as a source of input, you can choose from whatever is available, rotary potentiometer, | ||
+ | |||
+ | Every sensor (save the 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 initialized 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, the 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 a 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. | ||
+ | |||
+ | |||
- | <note important> | ||
- | <note warning> | ||
- | {{ : | ||