The Serial Monitor

Information
Step 1
Serial communication is where data is sent one bit at a time between two devices.

The Arduino and computer communicate via Serial through the USB cable.

Temp1491870570

In this lesson we will learn to use the Serial Monitor to display this data to our computer.

Temp1491870630
Step 2
Click on this icon to open the Serial Monitor
Temp1491610466

We haven't printed any data to it yet so it's blank right now.

Step 3
First use Serial.begin() to initialize serial communication

We do this once in the setup() function.

Temp1491864667

We need to specify a rate at which data is transferred, and 9600 is the default rate for the serial monitor so we use that.

Step 4
Now we can print data to the serial monitor

We can do this with Serial.print()and Serial.println(). (That's println with an L).

Both functions do the same thing, but println() (print line) creates a new line after sending, so we will use that one.

Temp1491865013
Check Your Work
Step 5
Upload your code and watch it through the Serial Monitor
Temp1491865216 Temp1491865192

You should see this:

Temp1491865435
Step 6
We can remove these lines now, they will only get in the way
Temp1491866610
Planning
Step 7
We're going to now create a variable and use the Serial monitor to watch it change

Unlike in JavaScript where we would define any variable using var, in C++ we must also declare what type of data the variable holds.

Temp1491865945

Here are some example data types:

int: a number without a decimal place.
float: a number with decimal places.
char: a character such as 'a'.
Servo: a Servo is a data type too.
Step 8
We first declare our variables at the top of our program

We must first declare our variables even exist before we can start using them.

Temp1491866173

Declaring a variable outside of a function puts it in the global scope. Meaning any function can use it.

Step 9
We use the setup() function to initialize our variables

We need to give our variable a value to start at.

Temp1491866651
Step 10
In the loop() function, increment the value of myNumber by 1

We use the symbol += to increment a variable by an amount

Temp1491866729

Remember that += is short-hand. We could have said

myNumber = myNumber + 1;

//equivalent to
myNumber += 1;
Step 11
Now we want to print this value to the Serial Monitor

Since we want it to show us the number every time it updates, we will put our println() inside the loop function.

Temp1491866899

Also we will put in a delay, just so it doesn't update too fast.

Check Your Work
Step 12
Upload your code and open the Serial Monitor.

You should see the value of the variable myNumber as it increases by 1 every 100ms.

Temp1491867147
Planning
Step 13
Let's use the Serial monitor to read the position of a dial

We can use the Serial Monitor to view information we don't already know such as the position of this potentiometer

Temp1491867736

We can use the position of the dial as INPUT for our CONTROLLER

Challenge
Step 14
Use this schematic to set up your potentiometer
Temp1491868753

Use your breadboard to help you set this up.

Use analog pin 0. Just so we're all on the same page.

Temp1491868872

HINT: These here are your analog pins!

Step 15
Set the value of myNumber to the value being INPUT in to pin A0

We will use the function analogRead() which will return a number between 0-1023 based on the amount of current going in to the pin

Temp1491869033

Also, remove the increment on myNumber.

Check Your Work
Step 16
Now upload your code and read the value of the potentiometer

We now have INPUT data! In the next lesson we will use this to CONTROL our OUTPUTS.

The value of myNumber as I turn the knob:

Temp1492119428
Challenge
Step 17
Use this INPUT to CONTROL the speed of a servo
Temp1498331034

Hook up a servo, and use writeMicroseconds() to set its speed.

You'll need to modify your INPUT data a little.

  • Your data will range from 0-1000
  • The servo needs values from 1000-2000

How much do you need to add to your input data so that you can use it to control your servo?

Check Your Work
Step 18
You should now be able to change the speed of your servo by twisting the potentiometer
Planning
Step 19
Many different components can be used as input

For example a distance sensor or a temperature sensor can supply INPUT to our Arduino.

with INPUT our CONTROLLER can make decisions and produce them as OUTPUT.

Temp1491870289

We will explore some of these components in future lessons.

The Serial Monitor Info

Account

MVCode Clubs

Created By

Newprofile jamie

Course:

Introduction to Robotics: Remote Control Car

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach