I have a Nano ESP32 set up to use MIDI over both USB and the BLE connections to Android devices. I want to be able to read and write to my sketch at the same time that they are connected to these two devices so I thought that I could use the Serial0 connected to my PC as outlined in the cheat-sheet:
where it says:
My USB adapter has pins labelled GND, ID, D+, D- and VBUS.
I assume that I must connect D+ and D- to Rx and Tx but I am not sure which way round these connections should be.
According to the cheat sheet you posted, use wires to connect whatever 3.3V UART serial device you want to RX and TX (D0 and D1) on the ESP32 Nano. GND must also be shared.
To connect to a PC via USB for UART serial data transfer, you need to use a USB RS232 serial adapter with 3.3V voltage levels. FTDI makes them, for example.
I want to be able to view Serial0 I/O in a terminal window so that I can debug my MIDI USB sketch when the Nano ESP32 USB port is connected to an Android device.
So I think that your last paragraph applies to my situation:
To connect to a PC via USB for UART serial data transfer, you need to use a USB RS232 serial adapter with 3.3V voltage levels. FTDI makes them, for example.
I will order a USB RS232 serial adapter with 3.3V voltage levels.
@dtone1
out of curiosity, what's your setup to use MIDI on the Nano ESP32?
MIDIUSB won't compile for this architecture.
Also, you should be able to use the same USB connection for both MIDI and Serial at the same time, but unless I have your Sketch or configuration I can't be of any help
Yes, I can do that when the USB cable is plugged into my PC but not when it is plugged into an Arduino device.
I am using the TinyUSB MIDI library - which in turn uses the Forty Seven Effects library - for my MIDI over USB. I also use the Forty Seven Effects library for MIDI over BLE. Both seem to be working well on my Nano ESP32. At the moment I am trying to figure out if there is any significant latency between these two. My initial feeling is that the latency is less than 20ms.
Here is the sketch that I am working on. I would like to replace the Serial calls with Serial0 calls so that I can have the Nano's USB port connected to an Android device and still be able to adjust the latency settings from the PC. I have ordered a USB RS232 serial adapter to connect to the Nano Esp32's Rx and Tx pins but I have to wait for that to arrive.
The sketch does demonstrate USB MIDI on a Nano ESP32.
/* 04 Oct 2023 Settable_latency_test
This test sounds notes from two different sources, USB and BLE. It is assumed that the
BLE source will have some amount of delay (latency) over the USB source so there is an adjustable
latency that can be added to the USB source. Once the two sounds occur at the same time then the
amount of latency added will be equal to additional latency that the BLE source has over the USB
source.
The sketch simply sounds two one second long notes separated by a latency delay that is entered into
the Serial monitor input area. This latency amount is in ms. and can be changed at any time.
The two notes are repeated every 5 seconds.
Latency has to be >=0...
*/
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <BLEMIDI_Transport.h>
#include <hardware/BLEMIDI_ESP32.h>
unsigned long currentMillis = 0;
int latency = 0;
// USB MIDI object
Adafruit_USBD_MIDI usb_midi;
// Create a new instance of the Arduino MIDI Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, USB_MIDI);
// Create a new instance of the BLE MIDI Library
BLEMIDI_CREATE_INSTANCE("MIDI_BLE", BLE_MIDI)
void setup() {
USB_MIDI.begin();
BLE_MIDI.begin();
// Open serial ports, sets data rate to 115200 bps
Serial.begin(115200);
delay(2000);
Serial.println("Starting Serial");
} // end of setup()
void loop() {
currentMillis = millis(); // capture the latest value of millis()
getNewLatency();
USBNote();
BLENote();
} // end of loop()
void getNewLatency() {
const unsigned int maxInputChars = 50; // how much serial data we expect before a newline
static char inputLine [maxInputChars];
static unsigned int inputPos = 0;
byte inByte;
// if serial data available, process it
if (Serial.available () == 0) { // Nothing to see here
return;
} else { // There is data to read
inByte = Serial.read ();
switch (inByte) {
case '\n': // end of text
inputLine [inputPos] = 0; // terminating null byte
latency = atoi(inputLine); // terminator reached! inputLine is the new latency
Serial.print("latency set to "); Serial.println(latency);
inputPos = 0; // reset buffer for next time
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (inputPos < (maxInputChars - 1))
inputLine [inputPos++] = inByte;
break;
} // end of switch
}
return;
} // end of getNewLatency()
void BLENote() {
static bool BLENoteOn = false;
static unsigned long BLENoteOnTime = 0;
static unsigned long BLENoteOffTime = 0;
if (BLENoteOn && (currentMillis - BLENoteOnTime <= 1000) ) return; // leave note playing
if (!BLENoteOn && (currentMillis - BLENoteOffTime <= 5000) ) return; // leave note turned off
if (!BLENoteOn) { // Time to turn it on
BLENoteOn = true;
BLENoteOnTime = currentMillis;
BLE_MIDI.sendNoteOn(69, 127, 1); // A4 = 440 hz
} else { // Time to turn it off
BLENoteOn = false;
BLENoteOffTime = currentMillis;
BLE_MIDI.sendNoteOff(69, 127, 1);
}
} // end of BLENote()
void USBNote() {
static bool USBNoteOn = false;
static unsigned long USBNoteOnTime = 0;
static unsigned long USBNoteOffTime = 0;
if (USBNoteOn && (currentMillis - USBNoteOnTime <= (1000 + latency) ) ) return; // leave note playing
if (!USBNoteOn && (currentMillis - USBNoteOffTime <= (5000 + latency) ) ) return; // leave note turned off
if (!USBNoteOn) { // Time to turn it on
USBNoteOn = true;
USBNoteOnTime = currentMillis - latency; // To prevent latency being added to USBNoteOntime every time around the main loop
USB_MIDI.sendNoteOn(73, 127, 1); // C#5
} else { // Time to turn it off
USBNoteOn = false;
USBNoteOffTime = currentMillis - latency; // To prevent latency being added to USBNoteOfftime every time around the main loop
USB_MIDI.sendNoteOff(73, 127, 1); // C#5
}
} // end of USBNote()
Correct - RS232 is a serial port with much higher voltages and may damage GPIOs on the board if connected directly.
I agree the right wording for what OP is looking for should have been "a 3.3V USB serial adapter", but it's easy to confuse UART with RS232 when even FTDI called the USB/UART chip FT232 , which lead to other confusing names down the road.
As long as "3.3V" is in the product description, you will (probably) be fine!
I ordered a " 3.3V USB to TTL Serial Adapter Debug Cable TX RX Signal 4 Pin Female Socket PL2303 Prolific Chip". I think that this is OK?
Anyway, lburelli, maybe the Nano ESP32 Cheatsheet should be updated to mention that in order to use Serial0 as a terminal then some sort of 3.3V USB to TTL adapter must be used to connect to Rx and Tx..
I may have already fried an ESP32 by connecting D+ and D- to Rx and Tx and plugging those into a USB socket on my PC.