[tutorial] Lesson 2: IO Applications

lesson2

Here we are. The time has come to build our first projects. We’ll meet the components in our disposal, figure out how to connect them, and then all we have to do is code. Code. CodeCode something useful that can make our everyday lives easier, or perhaps something fun that can entertain us. There is no stopping you from building anything… from the silliest children’s toy to the next spaceship that will travel to Mars!

Arduino Code


A basic Arduino program has the structure shown above. It consists of two functions, setup and loop. `setup()` is executed once, when the operation of the uC starts. `loop()` is called after the setup function has finished and is repeated indefinitely. This means that the code inside the loop function repeats again and again until the power goes down or the uC is reset.

Normally, inside setup we make the initial configuration of the uC and any libraries we may be using. Then, in the loop function we write the main code we want being executed over and over again, checking conditions and taking actions.

`C++` is the language on which we will be writing code. I am going to assume that you have a basic familiarity with C or C++ programs. But even if you don’t, don’t be alarmed. There are tons of tutorials on the Internet. For reference, you can start from learn-c.org or learncpp.com. From my side, I will do my best to keep the examples simple, so you can follow along and figure out the details on the go.

Short Review
Include a library: `#include `
Declare a constant: `const int OUTPUT_PIN = 7;`
Declare a variable: `int data = 124;`
Declare an array: `char buffer[100];`
Add a comment: `// This is a comment`
If structure: `if ( data == 4 ) { … }`
While structure: `while ( idx >= 0 ) { … }`
For structure: `for ( int i=0; i<10; ++i ) { … }`
Define a function: `void myFuction(int value) { … }`
Call a function: `myFunction(data);`
automata

Breadboard


breadboard_

Before we move on with examining electronic components, we have to get introduced to the single most useful tool in our workbench that will be present in all of our projects. This is the breadboard. The breadboard will be the basis for our circuits. We will place the various components in the holes and then use extra wires to make the interconnections.

breadboard_inside_correct_

From inside, the breadboard has the structure shown above.

The inner holes are connected together vertically. So, a group (column) of 5 holes in the breadboard represents a node in the circuit. We place the leads of a component in different columns, and connect the leads of different components together with wires. The holes in the middle are not connected together. These holes have a special purpose. This is where we place ICs, so that two opposite pins are not short-circuited.

The outer holes are connected together horizontally. These interconnected holes form the power lines. This is where we bring the 5V and GND voltage references. From there we take those voltages with wires to the components. Attention must be payed, since some breadboards break these power lines in the middle (red parts shown in the figure). In that case, left and right sides are created on the power lines and are not connected together.

LEDs


leds_

The first component we will see is the Light Emitting Diode (`LED`). LEDs are devices that when you run a current through them, they emit light. They can emit light of different frequencies, so we can encounter LEDs of a number of colors. They come in different forms and sizes. Common LEDs being used widely are the 5 mm round ones (shown to the left). There is a wide array of applications with the LEDs. We can use them to give visual indications, light up a room, or even transfer information wirelessly (like with the IR LED in your TV remote).

`LEDs` have polarity. This means that LEDs have to be connected in a certain way, and not the reverse one. They have a positive side, called anode, and a negative side, called cathode. We can light them up by applying a positive voltage difference on the anode and cathode. Apply an excess voltage, and the LED will burn. Apply a negative voltage, and the LED won’t lit.

LEDs have a specific voltage (a.k.a. forward voltage) on which they like to be operated. On the forward voltage, they light up in full brightness. If we give them a smaller voltage, they will still work, but they will just be less bright. We can’t operate an LED directly on voltages bigger than the forward voltage. If we can’t supply the (forward) voltage needed, we can still make use of a higher voltage by adding a current limiting resistor.

led_draw

led

Different LEDs, in size and color, have different forward voltages. Though, we don’t necessarily have to look for the forward voltage of every LED we use. For the 5 mm (or 3 mm) round LEDs (of any color) and a 5V supply, a 470\Omega resistor will always be a safe choice.

More on Current Limiting Resistors
If we want to make sure that our LEDs will light up in full brightness, we have to pick an appropriate current limiting resistor for the specific forward voltage of the LEDs.

One more parameter of an LED is the current (a.k.a. forward current) it draws when it operates on the forward voltage. You can find both the forward voltage and current of your LED on its datasheet.

Let’s call the LEDs’ forward voltage, V_{f} , and the LEDs’ forward current, I_{f} . Then, we can calculate the resistor needed to operate our LEDs in full brightness, with the following equation

R = \frac{5V - V_{f}}{I_{f}} [\Omega] .

Let’s say we have some LEDs with a V_f = 3.1V and a I_f = 20mA. Then, the resistor needed is R = \frac{5V - 3.1V}{0.02A} = 95 \Omega . There is no 95\Omega resistor, so we choose the closest realizable higher valued resistor. That is 100\Omega. Now, we pick some 100\Omega resistors, we use one resistor for every LED, we apply a 5V voltage difference to the LED-resistor combos… and finally the LEDs will light up in full brightness.

If we connect the anode of an LED to an output pin of an Arduino, we can turn the LED on by writing HIGH to the pin. If we connect the cathode of an LED to an output pin of an Arduino, we can turn the LED on by writing LOW to the pin.

Let’s write some code now. We’ll start with the famous blink example. This example uses an LED and an output pin on an Arduino to control the LED. In the code, we turn the LED on for a second, then we turn it off for another second, and then we repeat.

Make the following connections with the LED, the resistor and the Arduino. Pay attention to the polarity of the LED. The cathode connects to the resistor and the anode to the output pin on the Arduino. Connect the Arduino to your computer with a USB cable, upload the code, and watch the LED flash every two seconds.

blink

blink

Buzzers


buzzer_promo_
The next component we will handle is the `buzzer` (a.k.a. piezo buzzer). I’m sure you heard a buzzer beeping before, like when you start your desktop computer and a short beep sounds to indicate normal hardware conditions, or when a truck has put on reverse to notify the people around it. Another common application of buzzers is for user input confirmation on keystrokes. If you want to know more about the operation of a buzzer, you can read the article at the [engineersgarage.com](http://www.engineersgarage.com/insight/how-piezo-buzzer-works).

A buzzer has polarity. The positive side always goes to a higher voltage than the negative side. You should normally use a buzzer with a 100\Omega resistor at 5V .

The example below is basic demonstration of the operation of a buzzer. It just plays a series of beeps, repeatedly.

If you want to do something more interesting with a buzzer, look at the [`tone()`](http://arduino.cc/en/Reference/tone#.UyJPyIUcKpe) and [`noTone()`](http://arduino.cc/en/Reference/NoTone#.UyJ31YUcKpc) functions that let you excite the buzzer at different frequencies, thus creating notes and being able to compose music. Check them out!

buzzer_sch
Make the following connections with the Arduino, the buzzer, and the resistor. Pay attention to the polarity of the buzzer. Positive (+) side goes to the output pin on the Arduino. Connect the Arduino to your computer and upload the code. You should hear two short beeps and one long beep repeating.

buzzer_sch_

Conclusion


In this tutorial you’ve learned about LEDs and buzzers. At this point, you know how to connect them and how to use them. Now clone the examples to your account, try out some variations in the code, and see how it all works out. We’ll get back next time with some new components!


The Schematics were based on fritzing.

Images are CC BY-NC-SA 3.0.

Stay tuned:

* we promise that we won´t spam you, never.