February, 2016

In this second tutorial we are going to look at different forms of input and output. We will examine analog input, and serial output (how to sent text back to your computer), as well as controlling a servo motor. We will also look at functions and how to use them.

Serial Output

Sometimes we want to see what a program is doing while it runs in the Arduino, or perhaps we may even to interact with it by writing small texts, or we may want programs running in the computer to communicate with programs running in the Arduino. We can accomplish many of these things using serial communication through Arduino’s and the IDE’s serial capabilities. These are explained in the reference for Serial, together with the many functions to convert to and from ASCII characters, a standard encodings of text used in serial communication, to types like int, float or double, used in the Arduino. Serial is implemented as a C++ object, which makes the syntax a bit strange. We won’t go into details of what this means in detail, but this is the explanation for the weird ‘.’ that we use when we call Serial functions. To test the serial communication, just copy the following code into a new sketch:

After you have uploaded the code into your Arduino, go to Tools>Serial Monitor, to open the serial monitor. make sure that the baudrate (on the right-bottom corner of the window) is set to the same we used in Serial.begin(). This baud rate indicates how fast will be the communication, and both the Arduino and the Serial monitor have to have matching rates. (Change it and you will see what it happens!). Serial output is very useful to check and see what is actually happening when we run a program. One can for example printout variables, states (of pins) etc. It can also be used to get input from the computer, using Serial.read(). We won’t provide any example here, you can test it yourself if you feel like it, using the code in the reference. Another thing to notice is that we use Serial.println() and Serial.print(). The difference between them is that println() makes a new line, and print() doesn’t (see what happens if you change them!). As you see also, we are using text, which needs to be between double quotes “”. These are called strings. Strings are arrays of characters, or variables of type ‘char‘. We won’t look at arrays at this point, if you need to have to use them in the future we will look at them in detail. Sufficient to say is that arrays are a type of variable, but rather than holding only one value, they can hold a fixed number of values of the same type. An array has a name (as any variable) and to access the different values it holds, we use an index. Again, check the syntax if you are interested in the reference: array.

Analog Input

Now that we have the serial working, it is easier to test things in the Arduino. In the previous tutorial we looked at how to use digital input, that is how to read if a pin is getting any current or not (for example using a switch). There are many things in the world that can be sensed and described as on or off, but there are also many other things that are quantities, and that can be sensed (by electronic devices and sensors) and described as different amounts of voltage. We are going to build any of the 2 circuits in the Arduino analog input tutorial (you can choose any of them, or try out some other yourself…) . You can also try the code on the example, but to use the serial communication we are going to use the following code:

You can see how we are using the analogRead() function. The result we get from analogRead() is a value between 0 and 1023, which corresponds to a voltage between 0 an 5 volts. (Never connect anything with more voltage than that directly to the Arduino or you may fry it!) .

We won’t test analog output in these tutorials, but the logic is similar to that of the analog input: instead of having on or off (HIGH or LOW) , as we have been using with the LED in pin 13, one can use analogWrite(), which would have the effect of dimming the LED. analogWrite() uses a technique called Pulse Width Modulation (PWM), which consist of turning on and off a pin really fast. Depending of how long a pin is or on off, it will give a higher or lower voltage respectively. You don’t need to know all the details, besides the values used (between 0 and 255) and the fact that you can only use PWM in the pins marked with a tilde ‘~’ (pins 3, 5,6,9,10 and 11, in the Arduino Uno). This technique can be used to control the speed of motors or for dimming LEDs, for example.

Functions

We said in the previous tutorial that we were going to look at what functions are and how you could create your own. Functions are very useful to make code readable, as you can “pack” all the functionality you need in different parts of your code. For this we are just going to use a completely useless change to the code above, to make it look like like this:

As you can see besides the setup() and loop() functions we have two other functions defined in the text: sillyReadAnalog() and blinkOnce(). These are functions that we have created, and while they don’t really do anything interesting they illustrate how we declare functions. The int before sillyReadAnalog() and the void before blinkOnce() they indicate that sillyReadAnalog() returns an int and that blinkOnce() does not return anything. sillyReadAnalog() simply calls the analogRead() function, copies its value into a local variable (a variable that only exists within the scope of the function) and sends back the value in that variable using a return statement. blinkOnce() does not return anything, so it does not need a return statement. sillyReadAnalog() takes one variable, and blinkOnce() two; a function can take from 0 to any number of variables. What this means is that when we call the function (which needs to have exactly the same number of parameters as in the declaration) the parameters we pass will be copied into the variables of the declaration. When we use loop(), setup(), pinMode()… we are just using functions that have been defined this way by the people that has written Arduino, and you can the same way invent your own functions.

Servo Motors

While all the functions we have been using, besides conditional statements and other features that are part of the C language, are part of the Arduino core definitions, we often may need to use functionality that it is not part of the Arduino core. We can do this by using libraries, which are collections of functions that are not included by default when you write a sketch. This is what we have to do to control RC servo motors. Servo motors are quite neat to use, because they allow to control the position of something (a robot arm, for example) with minimal circuitry. While one can download and install 3rd party libraries, usually consisting of the code related to a particular piece of hardware like a LED matrix, there are a number of libraries that come with the Arduino IDE and are very easy to use. You can see how to include any library by opening a sketch and going to Sketch>Include Library and then selecting one of the options. The Arduino IDE will generate some code, consisting of one or more #include directives. The ‘#’ means that this is a thing for the compiler to do, not really any instructions to execute by the program. After the include word comes the name of the file which has the declarations of the functions we are going to use. Sometimes we may need to include more than one file. All the libraries that come with Arduino also have examples, which can be opened through File>Examples. We are going to use one of those examples, which is in:

File>Examples>Servo>Sweep

Each RC Servo has three cables. To connect the servo you only need to connect the power (usually red) to 5V, the ground (usually black) to the ground in the Arduino, and the other cable (often white) to the pin you are going to use ( the example uses pin 9). And that’s it…

As you can see the servo example also has a funny ‘.’ syntax, typical of C++. Without going in too much details, and if you look at how the code works, Servo is used a bit like a variable that has functions. It is declared with its type (which is Servo) and then we use the ‘.’ to call some functions associated with it. Servo is in this case what is called a class. Classes are a C++ feature (they are common in “object oriented” languages, such as Java). As said, we won’t go into the details of Object Oriented Programming, but it is worth to mention the syntax, that you may find quite often in Arduino.

Some of the things we have not looked at in these tutorials:

We have got a basic understanding of basic programming concepts such as variables, scope, functions, or control flow. These concepts constitute a good basis which we can use to learn further how to program in the future, with Arduino or in some other environment or programming language. There are however a few important points we have not covered. We have previously mentioned arrays, but in the examples above we have also only used if and if/else statements. During the following exercises is very likely that you will find loops in existing code, or that you may need to use them, either using while or for statements. We will figure these ones out while we do some hands-on work. There are of course quite a few other things that we have not talked about, but the the above tutorials give a basic knowledge of what programming is.

Final exercise:

Connect Servo motor to analog input.

Rest of the week:

Do something funny or interesting…explore the possibilities of Arduino, for example by connecting some input to some output in an interesting way. Next week we will start developing projects in relation to Arduino.