# Trinket Fake USB Serial

## Overview

Warning: 

The Trinket has a USB port that is used for bootloading. But the Trinket can only become a low-speed USB device because of its limited hardware. USB standards prevents low speed USB devices to truly act as virtual serial ports, which is why we cannot use a serial terminal to communicate with the Trinket directly.  
  
_However_ **,** there's a work-around for this problem. In Windows, you can emulate a fake serial port bridge using a utility named **com0com**. In this tutorial, you'll see how we can write a middle-man program that communicates with the Trinket and **com0com** , so that a serial terminal (such as the one built into Arduino IDE) can talk with the other end of **com0com.**

Danger: 

Danger: 

There are two pieces of code involved here:

- Arduino library named **TrinketFakeUsbSerial**
- A PC app named **TrinketFakeUsbSerialHostSW**

The other pieces of software involved  

- **LibUsbDotNet** , which makes it easy for me to use libusb-win32 to write TrinketFakeUsbSerialHostSW for Windows
- **com0com** , which emulates the two fake serial ports (only required on Windows)
- Some sort of serial terminal (Arduino IDE, Hyperterminal, Teraterm, RealTerm, Putty, etc)

Below is a diagram showing how data flows between the different components: ![](https://cdn-learn.adafruit.com/assets/assets/000/011/618/medium800/trinket_flowchart.png?1381725137)

Below is a demonstration video (the code sketch is in the Usage Demo section of this tutorial, the video should be viewed in 720p and full screen if you want to read the text on screen)

http://www.youtube.com/watch?v=Tt38wUjvH1k

# Trinket Fake USB Serial

## How it works

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/011/619/medium800/trinket_flowchart.png?1381725589)

Since virtual serial port protocol isn't allowed for low speed devices. We have to find another way to communicate.  
  
First, understand that all communication on a USB bus is always initiated by the host (the computer).  
  
The most basic useful data transaction is a control transfer, which carries two parts: a setup packet and a data packet. Basically, the setup packet says how many bytes are in the data packet, and which way the data packet goes. We use control transfers for data going from the computer to the Trinket.  
  
To get data going from the Trinket to the computer, we open an interrupt-in endpoint to send it. The computer will automatically ask "is there anything in the endpoint" once every 2 ms, and if Trinket answers "yes", then the transfer begins.  
  
The middle-man software I wrote (named **TrinketFakeUsbSerialHostSW** ) will communicate with the Trinket using LibUsbDotNet, which is a libusb-win32 wrapper for C#. The data is relayed to a fake serial port created by com0com.

# Trinket Fake USB Serial

## Install for Windows

Warning: 

Everything you need to download is on github, click the button to download a zip

[Download the Trinket Fake USB Files](https://github.com/adafruit/Adafruit-Trinket-USB/blob/master/TrinketFakeUsbSerial_allfiles_20131014d.zip?raw=true)
(note: all the source code is available: [https://github.com/adafruit/Adafruit-Trinket-USB/](https://github.com/adafruit/Adafruit-Trinket-USB/) )  
  
[Install the Arduino library](http://arduino.cc/en/Guide/Libraries "Link: http://arduino.cc/en/Guide/Libraries") as if it was any other Arduino library, read the readme.txt file, the source code, and the example sketch to see how to use it.  
  
Install [com0com](http://com0com.sourceforge.net/), then use it to setup a pair of fake serial ports (see screenshot to see my configuration, you can use any port number you want)

![](https://cdn-learn.adafruit.com/assets/assets/000/011/620/medium800/trinket_com0com_screenshot.png?1381727646)

This is the point where you write your own sketches to use the TrinketFakeUsbSerial library in any way you'd like. Get creative!  
  
Please read the source code of the library, the readme file, and the example sketch. It will help you understand how to use the library. It works mostly like HardwareSerial, so you can use print and println, along with other inherited functions.

When you try to connect a Trinket loaded with a sketch containing the TrinketFakeUsbSerial library, Windows will fail to find a device driver. The zip file you downloaded contains the device driver you need, so you have to tell Windows where they are. The driver is unsigned so there might be a security warning, install it anyways.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/621/medium800/trinket_win_driver_install.png?1381728224)

You've also downloaded the executable named TrinketFakeUsbSerialHostSW.exe , when you need to use the serial terminal with Trinket, this executable must be running in the background.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/623/medium800/trinket_app_screenshot.png?1381730267)

![](https://cdn-learn.adafruit.com/assets/assets/000/011/622/medium800/trinket_systray_screenshot.png?1381729368)

Remember, if you use com0com to setup COM12 and COM13, then use TrinketFakeUsbSerialHostSW to open COM12, and use your serial terminal to open COM13. The com0comsoftware will create a data-pipe between the two different # ports.  
  
Don't try to open COM12 using both, it won't work. Don't try to open COM13 using both, it won't work.  
  
(You don't have to use 12 or 13, you can really use any unused number)

# Trinket Fake USB Serial

## Usage Demo

Warning: 

http://www.youtube.com/watch?v=Tt38wUjvH1k

The video above should be viewed in 720p and full screen if you want to read the text on screen  
  
The sketch used in the demo is posted below

```
#include "TrinketFakeUsbSerial.h"

void setup()
{
  TFUSerial.begin();
}

void loop()
{
  TFUSerial.task(); // this should be called at least once every 10 ms

  if (TFUSerial.available()) {
    char c = TFUSerial.read();
    if (c == 'a')
    {
      TFUSerial.println("hello world");
    }
    else if (c == 'b')
    {
      TFUSerial.println(millis(), DEC);
    }
  }
}
```

# Trinket Fake USB Serial

## Install for Linux / Mac

Warning: 

I managed to write a Python script that does the same thing as the Windows application, except it's not a system tray application. It needs to be launched through the command line or using a script.  
  
Since it's a Python script that uses cross platform modules, the whole script is also cross platform and thus it works on Linux and Mac OS X.

Danger: 

First you need these items installed:

- [Python 2.7](http://www.python.org/download/releases/2.7.5/) (Python 3 will not work)
- [PyUSB](http://sourceforge.net/apps/trac/pyusb/)
- [pySerial](http://pyserial.sourceforge.net/)

(note: you can install the two modules using [easy\_install](http://pythonhosted.org/distribute/easy_install.html) , if you are having trouble installing these, please check Google to find some Python tutorials on installing modules!)  
  
Second, to replicate the role of com0com, use this command:  
```
socat PTY,link=COM8 PTY,link=COM9
```

to create COM8 and COM9, use any number you want  
  
Finally, to run the Python script...  
  
The download package is located: [https://github.com/adafruit/Adafruit-Trinket-USB/blob/master/TrinketFakeUsbSerial\_allfiles\_20131014...](https://github.com/adafruit/Adafruit-Trinket-USB/blob/master/TrinketFakeUsbSerial_allfiles_20131014d.zip?raw=true "Link: https://github.com/adafruit/Adafruit-Trinket-USB/blob/master/TrinketFakeUsbSerial\_allfiles\_20131014d.zip?raw=true") , the py file is inside the zip archive.  
  
Using your command line terminal, navigate to where the py file is located, and then run this command:

```
python TrinketFakeUsbSerialHostSW.py -p &lt;portname&gt;
```

The script should then run forever. It should gracefully handle connections and disconnections for you, but errors will show up on the screen.

To view more command line options (options for silencing errors, or show more detailed connection status) for the script, run this command:

```
python TrinketFakeUsbSerialHostSW.py -h
```

# Trinket Fake USB Serial

## USB HID Terminal Alternative

Warning: 

Another way of communicating with the Trinket uses HID to pass raw USB messages back and forth. This technique was created by Ray's Hobby. The key difference between Ray's method and the method discussed above is:

- Ray's method uses a HID profile, thus no driver installations are required at all
- Compared to the Fake USB method uses a custom profile with a libusb driver
- Ray's method uses a customized serial terminal that he wrote himself
- The Fake USB method works with most generic serial terminal programs, but requires running a background application to act as a "bridge"

For a lot more information check out [http://rayshobby.net/?p=7363](http://rayshobby.net/?p=7363) where the technique is talked about in detail  
Ray's code is not specifically designed for Trinket, so to make it work with Trinket, I have forked Ray's github for this project and made modifications to his code so that it also works with the Trinket.

[Visit the forked and modified github repo](https://github.com/adafruit/rayshobby-hid-serial-trinket)
You can download all the files from the github repo above.  
  
The only changes are:

- changed usbconfig.h to include different pin assignments
- added the oscillator calibration function required for ATtiny
- adjusted clock speed for Trinket

I did not modify Ray's host software at all. It works with his software without any modifications. His software is written using [Processing.org](http://processing.org/), and while the executables are also included, you should have [Java](http://www.oracle.com/technetwork/java/javase/downloads/index.html) installed in order to use them. The host software should work on any platform that can run Processing.org applications (or any platform that can run Java, meaning Windows, Linux, and Mac should all work).  
  
When you have downloaded all the files, [install the Arduino library as usual](http://arduino.cc/en/Guide/Libraries). You can then try compiling your own sketches using the library, or try running one of the provided example sketches.  
  
Then to use the serial terminal, either run the host software that you've already downloaded. You may use Processing.org to open the host software, or use one of the provided precompiled executables specific to your operating system.  
Danger: 


## Featured Products

### Adafruit Trinket - Mini Microcontroller - 5V Logic

[Adafruit Trinket - Mini Microcontroller - 5V Logic](https://www.adafruit.com/product/1501)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1501)
[Related Guides to the Product](https://learn.adafruit.com/products/1501/guides)
### Trinket 6-Pack - 3 x 3.3V and 3 x 5V Trinkets

[Trinket 6-Pack - 3 x 3.3V and 3 x 5V Trinkets](https://www.adafruit.com/product/1509)
**[We're no longer selling this pack of Trinket 3V+5V, we recommend our Trinket M0 which has better USB support, way faster processor, a ton more memory and CircuitPython support!](https://www.adafruit.com/product/3500)**

&nbsp;

Be the life of your...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1509)
[Related Guides to the Product](https://learn.adafruit.com/products/1509/guides)

## Related Guides

- [LED Trinket Tree Topper](https://learn.adafruit.com/neopixel-led-trinket-tree-topper.md)
- [Lie Ren's Stormflower Gun Blade](https://learn.adafruit.com/stormflower-gun-blade.md)
- [Adding Third Party Boards to the Arduino v1.6.4+ IDE](https://learn.adafruit.com/add-boards-arduino-v164.md)
- [Trinket USB Keyboard](https://learn.adafruit.com/trinket-usb-keyboard.md)
- [Trinket Sound-Reactive LED Color Organ](https://learn.adafruit.com/trinket-sound-reactive-led-color-organ.md)
- [Portable Mini Timelapse Camera](https://learn.adafruit.com/portable-mini-timelapse-camera.md)
- [Really Simple Animatronic Tail](https://learn.adafruit.com/really-simple-animatronic-tail.md)
- [3D Printed Daft Punk Helmet with Bluetooth ](https://learn.adafruit.com/3d-printed-daft-punk-helmet-with-bluetooth.md)
- [NeoPixel Cyber Falls Wig](https://learn.adafruit.com/neopixel-cyber-falls.md)
- [Bluetooth-Controlled NeoPixel Goggles](https://learn.adafruit.com/bluetooth-neopixel-goggles.md)
- [3D-Printed Bionic Eye](https://learn.adafruit.com/3d-printed-bionic-eye.md)
- [2014 Halloween 3D Printed Projects Roundup](https://learn.adafruit.com/2014-halloween-3d-printed-projects-roundup.md)
- [Guardian Robot – Zelda BOTW](https://learn.adafruit.com/guardian-robot-zelda-botw.md)
- [Trinket “Question Block” Sound Jewelry](https://learn.adafruit.com/trinket-question-block-sound-jewelry.md)
- [Halo Energy Sword by Mattel NeoPixel Upgrade](https://learn.adafruit.com/halo-energy-sword-by-mattel-neopixel-upgrade.md)
