This is an old revision of the document!


OpenLab 2014/07 | Hands on Arduino

  • Date: 31. 10. 2014
  • Workshop by: Lukáš Němec
  • Cake: we will see…
  • Cake by: Mirek Jaroš

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.

When we will 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.

  1. Arduino tutorials (also can be found in IDE) http://arduino.cc/en/Tutorial/HomePage
  2. Google and own imagination 😉

There will be supply of leds, resistors, some sensors (light, temperature, IR …) and bunch of other arduino related stuff.

Arduinos come in many different flavours, from basic UNO to MEGA, or specialized one's, for example arduino robot. Also 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.

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>Reset pin is usually present also in form of a button on board, this can be used for reinitializing state of your code back to setup function.</note>

Recomended steps:

  1. Connect arduino to PC, usually USB cable type B, or micro USB with new boards.
  2. Start Arduino IDE
  3. Select Tools - Board Type (mostly Arduino UNO)
  4. Select Tools - Serial Port (there will be usually only one, when only one arduino board is connected)
  5. Write your code or open some example sketches (or load some saved sketches)
  6. Do all the wiring (can be done as first step or as last)
  7. Press Ctrl + R to compile
  8. Press Ctrl + U to upload code to your arduino
  9. Optionally repeat while fixing bugs or adding features (two names for the same thing)

Programming for arduino is basically C language with some specific commands and libraries.

Introduction

Smallest code, that sucessfully compiles looks like this:

void setup() {
}
 
void loop() {  
}

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):

//global constant for more comprehensible code, you can use just number, 
//this way it is just more user friendly when handling multiple pins
int ledPin = 13; 
 
void setup() {
  // initialize digital pin 13 as an output, LED will be connected here  
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  //digitalWrite(pin, value) command is used to set voltageon pin to certain value
  //most usefull are constants HIGH and LOW, you don't need to use numbers
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second, led is turned on
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second, led is turned off
}

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>When connecting LED, remember that LEDs are like all other diodes, so electricity flows in one direction only. The longer lead is positive (anode) needs to be connected to power and shorter leg (cathode) to ground.</note> <note warning>LEDs are not capable of handling arduino's 5V power, so resistor is required. Usually one which has 220Ω is used, but in this case it is not necessary to be exact, so you can use 200Ω or 300Ω as well and it will slightly affect brightness of LED, but nothing more.</note>