The USB feature of the new ESP32 chips is
handy. Unfortunately, it is not easy to use because
the information is hidden. This video shows how everything works and
how you must set up the Arduino IDE to exploit the USB functionality. I promise it will save you a lot of frustration
and time. And it is the basis for a future video about
simple debugging with the new Arduino IDE 2.0. Grüezi YouTubers. Here is the guy with the Swiss accent. With a new episode and fresh ideas around
sensors and microcontrollers. Remember: If you subscribe, you will always
sit in the first row. The Universal Serial bus or USB can emulate
keyboards or create a USB stick. It also can save a few parts like the USB-Serial
chip on your PCB. This is why it is one of the significant improvements
of the ESP32-S2 and -S3 and the ESP32-C3. If you compare the parameters in the Arduino
IDE of the “old” ESP32 with those in the ESP32-S3, you see that many new ones starting
with “USB” appeared. We must understand the USB architecture used
in the Espressif chips to use them. And finally, you will get ready-made settings
with all parameters for your projects. In one of my last videos, I introduced the
ESP32-S3 as the next generation of ESP32 chips. I will concentrate on this because it has
the combined USB features of the -C3 and the -S2. If we look at its block diagram, we find the
word “USB” in two blocks: - USB Serial/JTAG and
- USB OTG And a bit later, we find that the -S3 offers:
- 1 × full-speed USB OTG - 1 × USB Serial/JTAG controller
So it offers two independent USB controllers, not only one, as suggested by the pinout where
we only find one USB interface. The -S2 chip only contains an OTG, and the
-C3 a Serial/JTAG controller. Another essential interface to understand
the matter is UART0 with its RX and TX pins. To eliminate confusion, we define four cases:
1. Legacy mode: As with the “old” ESP32,
we use RX/TX and a USB-to-serial chip to program and for Serial.print()
2. CDC mode: We use the CDC mode for programming
and Serial.print. This mode works in the -C3 and the -S3
3. OTG mode: We enable the second USB controller
to emulate keyboards, for example, or offer the ESP32 as a disk drive like a USB stick. This mode works in the -S2 and the -S3
4. UART and OTG mode, where we use two controllers
We start with the legacy mode because we all know it. It works exactly like with the old ESP32. We need a USB-to-serial converter chip and
must keep GPIO0 low during reset to bring the chip into upload mode. This can easily be checked in the Serial monitor
on 115200 baud: The chip writes: “Waiting for download”, which means “upload”
in Arduino terminology. It also says that it expects the download
on USB/UART0. As always, we have two possibilities to handle
GPIO0: Automatically with the famous two-transistor setup or using two buttons. For our “legacy” use case, the best is
to use the automated two-transistor setup. For all others, a design with two buttons
makes much more sense. Why? The reset button is obvious. I always want a reset button on my boards. GPIO0 acts more like an “emergency button”
with the new chips. It sets the chip into upload mode when everything
else fails. With Serial.print(), the output shows up on
our serial monitor. Everything as expected. Nothing new. Please select these parameters in the Arduino
IDE for use case 1. We will later discuss the function of the
new parameters. Let's continue with use case 2: The CDC mode. First, what does CDC mean? It is a USB device class like the USB Human
interface device class or the USB Mass storage class covered later. It offers serial communication over USB. Exactly what we need if we want to omit the
external USB-to-serial chip. This use case works with the first interface
of the ESP32-S3 that is restricted to CDC and JTAG. To use this mode, we must connect a USB connector
directly to the chip’s D+ and D- pins. We no longer need the USB to serial chip and
the two-transistor setup. If you build your own PCB, you may protect
the USB pins against overvoltage with such a chip. Let’s look at the needed parameters to enable
this mode in Arduino IDE. The USB mode has to be “CDC and JTAG”. JTAG means “Joint Test Action Group’s”
standard to connect debuggers, a topic of a later video. USB CDC On Boot obviously has to be enabled
to use it. Otherwise, the chip would not listen to the
D+ and D- pins. The following parameter is “MSC on boot”. MSC means “Mass storage class”, a USB
function mentioned before that uses the second USB controller of the ESP32-S3. It can create a USB disk drive. For the moment, we do not need it. Next is DFU, Device Firmware Upgrade. We do not need it; it can be disabled for
all use cases. Next is “Upload Mode”. As in use case 1, we chose “UART0/Hardware
CDC”. The rest of the parameters are well-known
from the “old” ESP times and will not be discussed in this video. With these parameters, the ESP32-S3 presents
itself differently in the ports menu. It adds its name to the com port number. Very handy! If you reset the chip, it loses the USB connection
because the reset button also stops its internal USB controller. When you leave the reset button, you hear
the sound of a connecting USB device. Sometimes, your computer assigns a different
com port than before. This happens if the uploaded sketch changes
some USB parameters inside the ESP32-S3. In this case, you have to select the port
again. In most cases, you do not need to use the
GPOI0 button to start the upload. Serial.print() works as usual, this time via
our USB port. All in all, the USB-to-Serial chip is neatly
replaced. Remember that UART0 with RX/TX does not get
the Serial.print statements because its function is transferred to the USB connection. If you want to use the “standard” UART
pins, you must use Serial0.print() statements. You can use both interfaces in parallel. For example, to connect to a GPS chip with
Serial0. Nice! I assume this will be the most common use
case for the ESP32-S3. It fully replaces the USB-to-Serial chip,
including the two transistors. And if you use an ESP32-S3 on a module, you
do not need a lot of external components for a fully functioning board. If we need additional USB functionality, we
must switch gears and enable the OTG controller in use case 3. This means we enable the chip's full-fledged
USB controller. It can emulate keyboards or mice, a gamepad,
or create a USB disk drive. You find many example files in the Arduino
IDE. Here, for example, it creates a disk drive
and adds some text. Or here, it emulates a keyboard when you press
the GPIO0 button. This is only possible if we enable the OTG
controller using these parameters. USB host mode is currently not supported in
the Arduino IDE. According to Espressif, this should change
soon. That will, of course, increase the useability
of those chips considerably. Last but not least, we can enable the UART
and the OTG USB controllers. Then we can use the RX/TX pins for uploading
and printing and the OTG controller for all other scenarios. Printing to Serial0 goes to RX/TX, and Serial.print()
goes to the USB com port. Here are a few tips for working with the OTG
mode: First, I would like to return to a remark
made before The USB configuration changes depending on the sketch running on the board. This differs from the legacy modes, meaning
the PC discovers other USB devices before or after flashing. Whenever the PC finds a changed device, it
creates a different COM port. So always check the actual COM port before
and after uploading if you work in OTG mode. It might have changed. Second: If you do not need USB, you can use
pins 19 and 20 as regular pins. Of course, this only works in legacy mode. And third: Remember the “emergency” button
on GPIO0 if uploading does not work. You can always put the chip in the defined
upload position with the standard sequence. So, summarized:
- The ESP32-S3 offers two USB controllers. A simple one that only provides CDC and JTAG
serial connections and a full-fledged USB controller for all sorts of USB services
- We can use the ESP32-S3 in four modes: o The Legacy, where we do not need to enable
any USB controllers on the chip and just use a USB-to-serial controller for uploading and
printing o The CDC mode replaces the external USB-to-Serial
controller with the internal CDC/JTAG controller. That reduces the external part count
o The OTG mode offers all the native USB modes of the USB controller
o In OTG mode, you can also upload via UART. Just adapt the parameters and add an external
USB-to-Serial adapter - OTA uploading works with USB
- The ESP32-S2 also has a USB OTG controller - The ESP32-C3 only has a CDC controller
- If something doesn't work, use the emergency button
This was all for today. As always, you find all the relevant links
in the description. I hope this video was useful or at least interesting
for you. If true, please consider supporting the channel
to secure its future existence. Thank you! Bye