Project 2.00 Serial Printing
In this project you will be introduced to Serial. Serial is a great way to see what is happening while your projects are running. Serial allows you to print strings of text, numbers and more out into a “Serial Port”. To open the Serial Port, click the magnifying glass in the top right of the Arduino IDE, or press ctrl + shift + m.
Here is what the magnifying glass will look like:
When you open the Serial port, it will appear in a window at the bottom of the screen. This is where the things you print will be displayed. Make sure to change this to “Newline” if it’s not already.
Project Code:
///////////////////////////////////////////////// // 2.00 - Serial Printing void setup() { Serial.begin(9600); } void loop() { Serial.print("My name is: "); Serial.println("YourNameHere"); delay(1000); } /////////////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
This line initializes the Serial port for communication. The 9600 value passed is the “Baud rate”. Baud rate is the rate of bits transmitted per second. Because our microcontroller has a built-in USB interface, the number we pass here does not matter.
Serial.begin(9600);
There are a few common ways to print information to this screen. We can use the Serial.print and/or Serial.println methods.
The difference is that the Serial.println method will print a new line after it prints what was passed to it. It is important to note that if the data being printed is a string of characters, they need to be enclosed in quotations. If it is a single character, it can be enclosed in quotations or apostrophes. Otherwise, numbers and variables do not need to be enclosed in anything special.
Serial.print("My name is: "); Serial.println("YourNameHere");
Change out the “YourNameHere” string with your name (enclosed in quotation marks).
Try making them both just Serial.print and see what happens!
A delay is needed to keep our MC Trainer from printing thousands of lines of text in a few seconds!
delay(1000);
Make sure and open the Serial port to see what is being printed!