HomeSTEM HQ Getting Started with Arduino Arduino Lesson 2

 

Lesson 2: Getting Serial - Writing To and From Our Board

Arduino Reference page

     
           
     

Communication - in Both Directions!

The serial port is the simplest way to communicate with your board, and to receive feedback from your sketch. The standard serial port for the Arduino is the USB port, and the Arduino IDE offers a "serial monitor" for watching what is happening. Initialising the serial port is easy - just add Serial.begin() to your setup routine, and give a baud speed (9600 or 115200 seem fairly standard - the former value is required for Bluetooth Low Energy (BLE) communication, to be dealt with a little later).

Let's add serial support to our LED sketch.

#define LED LED_BUILTIN

void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Welcome to the world of Arduino!");
}

void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}

Back to Top

Try changing the baud rate from 9600 to 115200: what do you notice? (You will need to change the dropdown menu at the bottom right of the serial monitor window to the correct rate). The response time should be faster, but otherwise all still works.

Change Serial.println to just Serial.print - "println" forces a new line for each statement. "print" runs the strings together.

So in this way, we can use the Serial port to give us a running commentary on our sketch - very useful when debugging and trying new things!

 
 

Writing TO the board

Now things can really start to get interesting: controlling the board with commands is a key element in all future work. The Arduino IDE Serial Monitor comes with an entry line at the top - here you can type text which will be sent to the connected board.

The fun happens in the loop: first define a character variable (here called "recvChar") - serial communication works by sending a series of string characters. Next, we apply a test - if Serial is available, then read the serial port into the recvChar variable, and print it to the serial monitor so that you can see when this happens.

Now an important new feature: "switch". Similar to an "if .. then .. else" loop in other coding languages, study this structure and see how it works. Each "case" elicits an action. So typing "0" or "1" and pressing enter sends a 0 or 1 string to the board. Easy! Then just adding a few Serial.println statements to watch what is happening, and we have established simple two-way communication between our board and the serial monitor.

Back to Top

 
 

#define LED LED_BUILTIN

void setup()
{

pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.print("Type 1 and enter to turn your LED ON,");
Serial.println(" and 0 to turn it OFF.");
}

void loop()
{

char recvChar;

if (Serial.available())
{

recvChar = Serial.read();
Serial.println(recvChar);
}

switch(recvChar)
{

case '0':

Serial.println("LED Off");
digitalWrite(LED, LOW);
break;

case '1':

Serial.println("LED On");
digitalWrite(LED, HIGH);
break;

}

}

Back to Top

 
 

Challenge

How might you change the code to have the LED come ON initially?

We would need to use the command digitalWrite(LED, HIGH) of course - but where should we place this in our sketch?

Try putting it at the start of the loop() - what do you notice?

What about at the end of setup()...

Back to Top

 
 

HomeSTEM HQ Getting Started with Arduino Arduino Lesson 2