It is standard nowadays to use an Arduino (or any other compatible) board USB port as an emulated Serial port. This is very useful for debugging, sending and receiving data from our lovable boards.
After noticing this in the datasheet, I thought it would be quite useful sometimes to attach a hardware serial device directly the USB port rather than by attaching flying wires on a breadboard or solder it directly to the board.
Thus, here it is a quick tutorial showing you how to set your board such that you can use its USB as a hardware Serial port.
In this specific tutorial, the serial device connected is a Bluetooth HC-06 serial adapter. However, you can adapt the code to any other serial device, as long as you solder a USB cable adapter to the serial device.
Bill of materials:
uChip x 1
micro-USB to USB/A adapter x 1 (link)
HC-06 BT module x 1
Recycled USB cable x 1
Battery (3V3 < VBAT < 5) x 1
Step 1: Create Serial Device With USB Connector
Peel off the USB cable and solder its wires to the Serial device as shown in the schematic and written below.
- USB cable black -> GND
- USB cable red -> VCC (Power)
- USB cable (D-) white -> RX
- USB cable (D+) green -> TX
Step 2: Program UChip
Connect uChip to your computer and load the sketch “HWSerialUSB.ino” into the board. Then, disconnect uChip to continue with the next steps.
TIP: How does the code work? Why is my USB port different now?
Here is described in summary the trick that I am doing in the code.
Basically, I am creating a new “SerialUSB_HW” instance using the GPIO that are currently assigned to work as D- and D+.
In the Setup() I enable the SERCOM functionality for the USB pins, using the function “pinPeripherial()” given into the “wiring_private.h” header included at the beginning of the code.
Now, I can use “SerialUSB_HW” instance likewise the standard Serial or SerialUSB, receiving and sending data to my HC-06 serial device.
Step 3: Assemble - Connect - Experiment
Connect the battery to uChip
- pin_8 -> VBAT--
- pin_16 -> VBAT+
Insert the OTG adapter and then the Serial device with its brand new USB port and...that’s all, the hardware is ready!
After pairing with the HC-06 module (standard password is 1234), connect to the BT device using your phone or BT interface. You should now receive the status of the on-board LED.
Send the char ‘o’ to turn on the LED, or any other char to turn it off.
Experiment and try other serial devices. Now you know how to use the USB port as an Hardware Serial port!
TIP: There is a #define in the code, which allows you to switch between using the USB port as Emulated serial or as Hardware serial. Experiment and verify that the Serial device attached (HC-06) does not communicate unless we force the USB to work as Hardware serial!