Hardware Reference
In-Depth Information
USB: An Endless Source of Serial Ports
One of the great things about microcontrollers is that,
because they're cheap, you can use many of them. For
example, in a project with many sensors, you can either
write a complex program on the microcontroller to read
them all, or you can give each sensor its own microcon-
troller. If you're trying to get all the information from those
sensors into a personal computer, you might think it's easier
to use one microcontroller because you've got a limited
number of serial ports. Thanks to USB, however, that's not
the case.
In Windows, you'd see something like COM8 , COM9 , COM10 .
The Arduino boards come with their own USB-to-Serial
adapter on board, but other microcontrollers and devices
usually don't. You can buy a USB-to-Serial converter for about
$15 to $40—Keyspan ( www.keyspan.com ) and IOGear ( www.
iogear.com ) sell decent models. Most consumer models like
these are USB-to-RS-232, because RS-232 was the standard
serial connector for PCs before USB came along. USB-to-RS-
232 adapters won't work directly with TTL serial devices.
If your microcontroller speaks USB, or if you've got a USB-
to-Serial adapter for it, you can just plug it in and it will show
up in the operating system as another serial port.
FTDI makes a USB-to-TTL-Serial cable with a breadboard
connector that's handy for interfacing to TTL serial devices.
It's available from the Maker SHED, Spark Fun, Adafruit, and
many other vendors. It comes in 5-volt and 3.3-volt versions.
Arduino, Spark Fun, and Adafruit all make breakout boards
with the same cable pin connections. Spark Fun's FTDI Basic
Board has LEDs that flash when data is being transmitted.
Adafruit's version is called the FTDI Friend. The Arduino
USB-to-Serial adapter is based on a different chip but has
the same pin connections. Any of these can be used for
USB-to-Serial connections throughout this topic. You can
see them all at the beginning of this chapter. The pin con-
nections are shown in Figure 2-6.
For example, if you plug three Arduino modules into the
same computer through a USB hub, you'll get three new
serial ports, named something like this on Mac OS X:
/dev/tty.usbmodem241441
/dev/tty.usbmodem241461
/dev/tty.usbmodem241471
Data layer
This is the same as TTL—8 bits per byte with a start and
stop bit.
One more protocol: if you use a BASIC Stamp or another
microcontroller with a non-USB serial interface, you
probably have a 9-pin serial connector connecting your
microcontroller to your PC or to a USB-to-Serial adapter.
You can see it in Figure 2-4. This connector, called a DB-9
or D-sub-9 , is a standard connector for another serial
protocol, RS-232 . RS-232 was the main serial protocol for
computer serial connections before USB, and it's still seen
on some older computer peripheral devices:
So why is it possible to connect some microcontrollers,
like the BASIC Stamp or the BX-24, directly to RS-232
serial ports? It is because the voltage levels of TTL serial,
0 to 5 volts, are just barely enough to register in the higher
RS-232 levels, and because you can invert the bits when
sending or receiving from the microcontroller. RS-232
doesn't carry any of the addressing overhead of USB,
so it's an easier protocol to deal with. Unfortunately, it's
mostly obsolete, so USB-to-Serial converters are increas-
ingly common tools for microcontroller programmers.
Because the Arduino boards have an integrated USB-to-
Serial converter, you can just plug them into a
USB port.
Physical layer
A computer with an RS-232 serial port receives data on
pin 2, and sends it out on pin 3. Pin 5 is the ground pin.
Electrical layer
RS-232 sends data at two levels: 5 to 12 volts, and -5 to
-12 volts.
Logical layer
A 5- to 12- volt signal represents the value 0, and a -5 to
-12 volt signal represents the value 1. This is inverted logic .
When you're lucky, you never have to think about this kind
of protocol mixing, and you can just use converters to do
the job for you. You're not always lucky, though, so it's
worth knowing a little about what's happening behind the
Search WWH ::




Custom Search