Monday, March 27, 2017

Teensy 3.6 Basics: Sending Serial Data

Overview
There are a number of ways that the Teensy can communicate with a computer or other device. A serial connection can be used via USB, and provides an easy way to troubleshoot hardware and debug code, as the Teensy can send human-readable information to the computer for feedback. The Arduino IDE can open up a serial connection, and receive data from the Teensy via the Serial Monitor.

There are some downsides, too. Idiosyncrasies regarding serial data and connections may cause some confusion. For example, only one piece of computer software can actively communicate with a given serial-based connection at a time, as opposed to USB MIDI data - where multiple pieces of computer software can send and received data from the one device.

Data types also play a role in usability, as each data value in a serial message is 8-bit, and can represent either an ASCII code (and therefore an alphanumeric symbol) so as to be human-readable, or an integer from 0 - 255 so as to be treated as a data value. This will be expanded on with examples below.

Teensy 4.1 Note: These examples should work without issue wit Teensy 4.1






Hardware Setup
The initial hardware setup is simply the Teensy connected via USB to a computer.







Software Setup
A serial connection makes use of a series of Serial-based functions. A serial connection also has a pre-determined speed, which is set as part of the sketch as one of a number of possible baud rates, or data transfer speeds.

A suggested baud rate for USB connections is 57600, however it may be useful to go beyond this.

Possible rates include: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. All of these are given in bits per second.

To open a Serial connection, the function Serial.begin(57600); is required in the setup function. Serial.print(), Serial.println() and Serial.write() can all be used to send data from the Teensy to the computer.

Serial.print() will send the contents as ASCII character codes, so that the Arduino IDE can display them as human-readable numbers, letters and symbols. However, this will never add a line break.

Serial.println() will send the contents as ASCII character codes, so that the Arduino IDE can display them as human-readable numbers, letters and symbols and also add a line break immediately after the message.

Serial.write() will send the contents as a data value from 0 - 255, so that the data can be used directly in other software such as Max, PD or Processing.

To open the Serial Monitor in the Arduino IDE, select Serial Monitor under the Tools menu or use command shift m.







Example 1 - Printing to the Serial Monitor
In this example, the aim is to simply print the word "Hello" to the Serial Monitor from the Teensy every second. The serial port is opened with a rate of 57600. The Serial.println(); function is used to print the word "Hello" as a new line in the Serial Monitor.


Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_Serial_Data_Example_1/








Example 2 - Print Variable to the Serial Monitor
In this example, the aim is to print a variable to the Serial Monitor from the Teensy. In this case, a for loop is used to increase a value i by one every loop. This value can then be used. Note how this combines static text, the Serial.print() function, variables and the Serial.println function.



Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_Serial_Data_Example_2/







Example 3 - Print Value of Potentiometer to the Serial Monitor
In this example, the aim is to read the value of an analog input to which a potentiometer is connected. The value of the potentiometer is printed to the Serial Monitor.











Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_Serial_Data_Example_3/






Example 4 - Use Serial Write with Max
In this example, Serial.write() is used instead of Serial.print(). This means that values are sent from the Teensy in the data range 0 - 255. Max can then receive these values as integers that can be visually displayed using a multislider object.

Note the use of analogReadResolution(8) - setting the range to 8-bits - to keep the values within a range of 0 - 255. The serial object is used in Max to receive the data, and the data is timed using the metro object.







Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_Serial_Data_Example_4/









Summary
The serial connection can be used to help troubleshoot and test Arduino sketches on the Teensy. Depending on how the output data is to be used, Serial.print(), Serial.println() or Serial.write() may be used.

0 comments: