Hardware Reference
In-Depth Information
You'll investigate the functionality of the serial interface more in later chapters.
For now, just be aware that the serial interface to the computer must be started in
the setup . Serial.begin() takes one argument that specifies the communication
speed, or baud rate. The baud rate specifies the number of bits being transferred
per second. Faster baud rates enable you to transmit more data in less time, but
can also introduce transmission errors in some communication systems. 9600
baud is a common value, and it's what you use throughout this topic.
In each iteration through the loop, the val variable is set to the present value
that the ADC reports from analog pin 0. The analogRead() command requires
the number of the ADC pin to be passed to it. In this case, it's 0 because that's
what you hooked the potentiometer up to. You can also pass A0 , though the
analogRead() function knows you must be passing it an analog pin number, so
you can pass 0 as shorthand. After the value has been read (a number between
0 and 1023 ), Serial.println() prints that value over serial to the computer's
serial terminal, followed by a “newline” that advances the cursor to the next
line. The loop then delays for half a second (so that the numbers don't scroll by
faster than you can read them), and the process repeats.
After loading this onto your Arduino, you'll notice that the TX LED on your
Arduino is blinking every 500ms (at least it should be). This LED indicates that
your Arduino is transmitting data via the USB connection to the serial terminal
on your computer. You can use a variety of terminal programs to see what your
Arduino is sending, but the Arduino IDE conveniently has one built right in!
Click the circled button shown in Figure 3-5 to launch the serial monitor.
Figure 3-5: Serial monitor button
Search WWH ::




Custom Search