FORGE
has a Serial Plotter that converts numbers sent over
a serial connection into a graph.
The Serial.println() function can take a few
different data types, including floats (as shown here)
and strings.
This is a great way of seeing what’s going on in
your scripts, but it can also be used to send data to
programs running on your PC. Most programming
languages have a library or module for working with
serial connections, so you can send data directly
from your microcontroller to software running on
your PC.
BACK AND FORTH
So far we’ve seen data sent from the microcontroller
to the computer, but the serial protocol is
bidirectional (well, really, it’s two unidirectional
channels pointing in opposite directions, but let’s not
worry about that), which means we can also send
data from our computer to the microcontroller. The
following code (which you’ll find in your Arduino IDE
in Examples > Communication > Dimmer) takes data
in and uses it to control the brightness of an LED.
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
Serial.println(brightness);
analogWrite(ledPin, brightness);
}
}
While Serial.println() can take a wide variety of
formats, Serial.read() will always return a single
one-byte character. In this case, we’re using this to
control the brightness of the LED. However, by
default, the serial monitor will also send characters
for line end and carriage return, so we need to turn
these off. Change ‘Both NL and CR’ to ‘No Line
Endings’. Now, when you enter a character into the
box and hit Send, the LED will be adjusted based on
the ASCII value for that character. With letters, ‘A’ is
the lowest and ‘Z’ is the highest brightness.
Because of the one byte limitation, it’s easiest if
you can limit data sent from your computer to your
microcontroller to single bytes. Commands can be
single letters, etc. However, if this isn’t possible, you
can build up data by reading multiple bytes (i.e.
calling Serial.read() several times) and stitching the
data together in the way you need.
Using serial, you can quickly and easily create
custom USB gadgets for your computer, or send
data back and forward to control your makes.
Above
Serial can send data
in both directions
Here, we’ve looked at how to connect a computer to
a microcontroller and communicate via USB serial,
but there are loads of other devices that you can link
your microcontroller to via serial. One popular option
is Bluetooth serial links. These are hardware devices
that expose a Bluetooth connection as a serial port.
Any data you send down the wired serial link between
the microcontroller and Bluetooth module will be sent
over the Bluetooth link to whatever the module is
paired with. You can use this to send data back and
forth between your microcontroller and mobile phone
(there are several Bluetooth serial apps available), or
other Bluetooth-enabled devices.
NOT JUST COMPUTERS