Send data like it’s 1970
TUTORIAL
ost of the time you want a
microcontroller to be an
independent device operating on
its own. Give it power, and let it do
its processing. However, sometimes
we need to connect it to something
larger – a general-purpose computer. Perhaps you
need the processing power of a PC, or perhaps you
want to send data to the internet from a
microcontroller without network access.
Fortunately, there’s one protocol built into almost
every operating system and microcontroller: serial
(see box below). Back in the day, when trains ran on
steam and all this were fields, computers had a
D-shaped serial port that used the Universal
Asynchronous Receive and Transmit (UART) protocol,
but that has long since gone and now we have only
USB. While this is a different protocol, thanks to a bit
of software, and USB to UART bridges in modern
microcontroller boards, we can usually ignore the fact
that they are different and send data out of a UART,
via a USB connection, and receive it on a computer.
Let’s look at a very simple example (you should
find this in Examples > Communication > Graph in
the Arduino IDE).
M
Send data like it’s 1970
An old protocol that’s still got a lot to offer
Left
The plotter lets you view values
coming from sensors in real time
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(2);
}
As you can see, we have to first initialise the speed
of the UART port. You have to set this on both the
sending and receiving end (which we will look at
next). In general, lower speeds are likely to be more
stable than higher speeds. 9600 is about the lowest
we ever go, and we usually have no problem going
up to 115,000. Once it’s created, we can simply
push data out of the UART. In this case, it’s sending
the data from A0. If this is unconnected and floating,
you could get a range of values, but you could hook
this up to a sensor for more meaningful numbers.
If you upload this code to your board, you can then
view the data in the serial monitor (Tools > Serial
Monitor). Make sure that the speed is set to 9600
and it should come through. The Arduino IDE also
Technically, the word ‘serial’ refers to any
protocol where data is sent bit after bit on a
single wire. There could be one or more
support wires, but as long as the data is sent on
one wire, it’s serial. The alternative is parallel,
where data bits are sent down multiple
wires simultaneously.
However, in common usage, serial refers to
Universal Asynchronous Receive and Transmit
(UART), a protocol that’s been used since the
1970s to transfer data between devices.
SERIAL
Ben Everard
@ben_everard
Ben loves cutting stuff,
any stuff. There’s no
longer a shelf to store
these tools on (it’s now
two shelves), and the
door’s in danger.