Comunicando-por-puerto-serie

Let´s get programming. Hold on, why are we starting with communication via serial port? It doesn´t seem right. However, when a controller board is programmed, this is the best way of debugging the code. When we don´t know what´s going on or why the program isn´t working as we expected it to, what we need is to get some data during execution. When working with boards such as the ZUM BT 328, the easiest thing is to send this data via the serial port and monitor it, so let´s get going!

An empty sketch

All Arduino programs have the same bare minimum structure:

In the setup function, we can configure everything we subsequently need. The code is deployed in the loop function and then it runs continuously on the board. Don´t worry if you don´t understand anything right now. The aim of this lesson is simply being able to communicate with the computer via serial port, even though you might not understand exactly how it happens.

Go to Codebender and open a new project, the empty sketch will load automatically:

codebender_blank

Hello world – starting to communicate via serial port

Configuring the serial port has to be done using the setup function. To do this, we can use the Serial object included in the Arduino libraries.

All you need to do is tell it what speed you to want to communicate with the computer. The select the speed, it typically takes the values 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. The usual speed is 9600. This number indicates the number of bits of data transmitted per second via the serial port. Obviously, the greater this number is, the faster we can communicate. Right then, let´s get configuring:

In this command, the serial communication starts at 9600 bits per second.

Now we will send Hello world (Hola Mundo) via the serial port. As we only want to send it once, we will do it using the same setup function:

We will take a look at the entire programming process in the next video. Sorry but the only board I have to hand right now is a ZUM Mega, which is why the board selected is the Mega and not the ZUM BT, but it works the same way for both (and all Arduino boards).

All videos are recorded in HD 1080p, so you can select the best quality on YouTube.

Tick Tock

Now we´re going to make a wall clock. It will go tick tock every second, so we will program it to execute the loop function continuously:

Now´s let´s watch the entire process on video.

Echoooo

To finish off, we´re going to make things a bit more complicated. Up until now we have been writing via serial port. Now we´re going to receive some data and write what we have received:

This code is a little more mysterious, so we will run through it quickly. This lesson is only about debugging, but we´ll be taking a look at more advanced serial communication later on.

The available() function of the Serial object returns the number of bytes available in the port serial port (in order to be read). So when there is data available (Serial.available()>0), we read it (Serial.read()) and while we are doing so, we write it via serial port (Serial.write()). As we are not including the automatic change of line, the echo follows. Have a go yourself at seeing what happens when you us println. Do you know why?

Now we will watch the last video for today. In the next lesson we will (probably) look at how to declare and use variables.

You will find everything you need for making changes to videos, adding subtitles and adding voice-over in the download section. Unfortunately, we can´t share the music with you due to copyright (which we had to pay for).