Serial Communication

The Arduino has a USB connection that is used by the IDE to upload code into the microcontroller. The good news is that after a sketch is uploaded and is running, the sketch can use this same connection to send messages to or receive messages from from your computer. The way we do this from a sketch is to use the serial object.

In the Arduino programming language, anobject is a collection of related capabilities bundled together for convenience, and the serial object allows us to communicate over the USB connection. You can think of the serial object as a way to get a message to or from the Arduino, one character at a time. Of course, the serial object contains lots of complicated stuff that we don’t need to worry about. We just need to learn how to use the serial object.

In this example you’ll take the last circuit we built with the photoresistor, but instead of controlling the brightness of an LED, you’ll send the values that are read from analogRead() back to the computer. Type the code in Example 5-5 into a new sketch. You can also download.

Example 5-5. Send the computer the values read from analogue input 0
const int SENSOR = 0;  // select the input pin for the
                       // sensor resistor

int val = 0; // variable to store the value coming
             // from the sensor

void setup() {

 Serial.begin(9600); // open the serial port to send
                     // data back to the computer at
                     // 9600 bits per second
}

void loop() {

  val = analogRead(SENSOR); // read the value from
                            // the sensor

  Serial.println(val); // print the value to
                       // the serial port

  delay(100); // wait 100ms between
              // each send
}

After you’ve uploaded the code to your Arduino, you might think that nothing interesting happens. Actually, your Arduino is working fine: it is busy reading the light sensor and sending the information to your computer. The problem is that nothing on your computer is showing you the information that is coming from your Arduino.

What you need is a software function called the serial monitor, and it’s built in to the Arduino IDE.

The Serial Monitor button is near the top-right corner of the Arduino IDE. It looks a bit like a magnifying glass, as if you were spying on the communication from the Arduino to your computer.

Click the Serial Monitor button to open the monitor, and you’ll see the numbers rolling past in the bottom part of the window. Cover up the photoresistor to make it darker, and see how the numbers change. Notice that the numbers never go below zero, and never go above 1023, as this is the range of numbers that analogRead() can produce.

This serial communication channel between Arduino and your computer opens up a whole new world of possibilities. There are many programming languages that let you write programs for your computer that can talk to the serial port, and through the serial port, those programs can talk to your Arduino.

A particularly good complement to Arduino is the Processing language), because the languages and IDEs are so similar. You’ll learn more about Processing in “Coding”, and your Arduino IDE includes some examples, such as File→Examples→04.Communication→Dimmer and File→Examples→04.Communication→Graph. You can also find many examples on the Internet.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *