PROGRAMMING: sERIAL COMMUNICATION
If you’ve read this far, you have already seen us use the Serial object in our code examples. The Serial object allows us to communicate between an Arduino and a desktop or laptop PC. We use some of its methods to send information from the PC to the Arno board and back to the PC.
A little background: computers used to have a special connector called a serial port. The serial port was used to communicate with other devices using a protocol called RS-232. USB has replaced RS-232 in PCs. The microcontroller on some Arduino boards (like the UNO or Duemilanove) still speak RS-232. Another chip on the board or in a special cable translates between USB and RS-232. The Arno speaks USB natively (without needing the help of another chip) but we still use some of the language of RS-232.
If you’re going to use serial communication, you need to include this command in the setup() block.
Serial.begin(9600);
If we were using RS-232, the command would set the communication speed to 9600 baud. With the Arno, we are just telling the microcontroller to create an instance of the Serial object.
To write something from the Arno to the PC we use the command:
Serial.print(“Hello World!”);
or
Serial.println(“Hello World!);
When we use Serial.print, we send the message from the Arno to the PC. The next message will start right at the end of the first without starting a new line. Serial.println adds a special character telling the PC to start a new line.
How do we see the message once it’s sent to the PC? We need a serial monitor. Luckily, the Arduino IDE has one built-in. When we plug the Arno into a PC, it should create a COM port (a virtual serial port). To connect to the COM port, we go to Tools > Serial Port in the IDE menu bar. You should see something like COM4 in the dropdown menu. Select it. We discuss connecting with the Arno more in the Getting Started section.
Once you have selected the serial port, you can open the serial monitor. There’s an option in the Tools menu or you can click on the magnifying glass icon on the upper right side of the IDE window. A third option is to press Ctrl-Shift-M.
Here’s what the serial monitor looks like. You can send messages to the Arno by typing in the top box and hitting Enter or the Send button. Messages from the Arno to the PC appear in the lower box.
A little background: computers used to have a special connector called a serial port. The serial port was used to communicate with other devices using a protocol called RS-232. USB has replaced RS-232 in PCs. The microcontroller on some Arduino boards (like the UNO or Duemilanove) still speak RS-232. Another chip on the board or in a special cable translates between USB and RS-232. The Arno speaks USB natively (without needing the help of another chip) but we still use some of the language of RS-232.
If you’re going to use serial communication, you need to include this command in the setup() block.
Serial.begin(9600);
If we were using RS-232, the command would set the communication speed to 9600 baud. With the Arno, we are just telling the microcontroller to create an instance of the Serial object.
To write something from the Arno to the PC we use the command:
Serial.print(“Hello World!”);
or
Serial.println(“Hello World!);
When we use Serial.print, we send the message from the Arno to the PC. The next message will start right at the end of the first without starting a new line. Serial.println adds a special character telling the PC to start a new line.
How do we see the message once it’s sent to the PC? We need a serial monitor. Luckily, the Arduino IDE has one built-in. When we plug the Arno into a PC, it should create a COM port (a virtual serial port). To connect to the COM port, we go to Tools > Serial Port in the IDE menu bar. You should see something like COM4 in the dropdown menu. Select it. We discuss connecting with the Arno more in the Getting Started section.
Once you have selected the serial port, you can open the serial monitor. There’s an option in the Tools menu or you can click on the magnifying glass icon on the upper right side of the IDE window. A third option is to press Ctrl-Shift-M.
Here’s what the serial monitor looks like. You can send messages to the Arno by typing in the top box and hitting Enter or the Send button. Messages from the Arno to the PC appear in the lower box.
So far we’ve only covered how to send messages to the PC. What happens when we type a message in the serial monitor and send it to the Arno? The Arno holds the incoming message in a special part of its memory called a buffer. Each character occupies one byte of memory. There are several commands to handle messages once they’re in the buffer
//How many bytes are in the buffer?
int howMany = Serial.available();
//read a byte
char aByte = Serial.read();
//read all bytes and send them back to the PC
char aByte;
while(Serial.available() > 0){
aByte = Serial.read();
Serial.print(aByte);
}
Serial.println(“All done!”);
Serial communication between the Arno and PC is an incredibly useful tool. Besides providing a method to collect input from the user, the Serial object provides a window into what’s going on inside the Arno. We often use it to figure out why a sketch isn’t working the way we expect. This process is called debugging. You can use the Serial object to send the values of variables to the serial monitor or to let you know when the Arno reaches a certain line in the sketch or enters a function. It would be very difficult to figure out why something isn’t working without this feedback.
//How many bytes are in the buffer?
int howMany = Serial.available();
//read a byte
char aByte = Serial.read();
//read all bytes and send them back to the PC
char aByte;
while(Serial.available() > 0){
aByte = Serial.read();
Serial.print(aByte);
}
Serial.println(“All done!”);
Serial communication between the Arno and PC is an incredibly useful tool. Besides providing a method to collect input from the user, the Serial object provides a window into what’s going on inside the Arno. We often use it to figure out why a sketch isn’t working the way we expect. This process is called debugging. You can use the Serial object to send the values of variables to the serial monitor or to let you know when the Arno reaches a certain line in the sketch or enters a function. It would be very difficult to figure out why something isn’t working without this feedback.