Read your sensors: Serial.print();

Today we start to play with analog sensors. As you may remember from this previous tutorial, an analog sensor returns a range of values and they are different from the digital ones, that return only two values.

To see what the sensor is reading, we’re going to introduce a new function that is called

Serial.println();


What it does, is writing something on a serial port, like the USB. From the Arduino software, we can see what’s going on in the serial port where the Arduino is conncected, by clicking in the top-right corner of the interface where there’s that small button with a magnifier. This will open a new window called “Serial Monitor



The serial function must be initialized in the void setup(), using this syntax:

Serial.begin(9600);


the value 9600 is the speed of the communication, it is expressed in Baud and it’s a fixed value, if not for particular cases, just use 9600


Then inside the loop, every time we want to print something we just have to write

Serial.println("Hello!");
delay(1000);


With this inside the loop, our program keeps printing "Hello!" on the serial port every second. Try to upload it into the Arduino, here’s the full code:

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println("Hello!");
  delay(1000);
}


After loading it into the board, open the Serial Monitor, you should see something like this:


If you write something inside the println brackets, you can do it in quotes like we did with the "Hello!", and it prints exactly what we write, but we can also print a value or a variable.

What’s a variable?

Variables are a way to store values that may change over time. To create a new variable we have to assign them a name and a type. There are different types of variables, in our basic TinkerKit! sketches we are using mainly integers that are shortened as int.

They must be declared before the setup, in the globals section, for example if we want to create a new variable of type integer, we write

int i = 0;


this creates a new variable called i that for now is equal to 0. we can try to increment this value in the loop function by writing

i = i + 1;


So that every loop our variable increases by one every second. Let’s verify this with the Serial print function that we’ve seen before, in the loop we write Serial.println(i), the one second delay and our sketch is now something like:

int i = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  i = i + 1;
  Serial.println(i);
  delay(1000);
}

if we open the serial monitor after loading this sketch, what we see is a value that keeps increasing every second:

Now, what does the serial print have to do with the analog sensor? Well we can print on the serial monitor the value that the sensor is reading.

How can we write it in the Arduino/TinkerKit! language? Try to guess it before reading further.

The solution is

Serial.println(sensor.read());


The full sketch is

#include <TinkerKit.h>
TKLightSensor sensor(I0);

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(sensor.read());
  delay(1000);
}


Now that we know variables, we can use them to make our code more readable and easier to modify; in fact we can store the sensor.read() value in a variable:

#include <TinkerKit.h>
TKLightSensor sensor(I0);
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = sensor.read();
  Serial.println(val);
  delay(1000);
}


If we use print instead of println we can write on the serial port without creating a new line. It’s useful if we want to add details on the printed value or to print multiple variables on the same line, just remember to add println to the last element.

Serial.print("The sensor value is: ");
Serial.println(val);

In the next tutorial we’ll finally connect inputs and outputs. Do you have an idea on how to do it? Try by yourself, create an LED that reacts at the amount of light in the room.

Leave a comment