Connecting CP2102 to ESP32 - Page 1
Forum section: electronics / beginners

author: Kaisha
author_rank: Contributor
timestamp: August 08, 2021, 05:29:34 pm
content: I want to connect a CP2102 USB->UART to an ESP32. Having read the datasheet and looked at a number of examples online, I still have a few questions.I plan on running the ESP32 mainly off of battery power, with the CP2102 only used occasionally (uploading programs, debugging, etc...), so I don't want to power the CP2102 if a usb cable isn't plugged in. I was planning to power the CP2102 directly off the usb (as the CP2102 has it's own internal linear regulator), but will any potential voltage difference between the ESP32 (which runs at 3.3V) and the CP2102 (which runs at 3.0V AFAIK) cause issues with communication? Could current (more than the tiny amounts used in signalling) potentially flow through the RX/TX/RTS/DTR pins? Is that something I need to worry about?When the CP2102 is unpowered (usb cable unplugged) but the ESP32 is running (battery power) is it possible for current to leak through the RX/TX/DTR/RTS lines? Will they be high impedance if the CP2102 is unpowered?I'm rather confused as to the use of the /RST pin (not the RTS UART out pin often used to reset the connected MCU, but the actual /RST pin used to reset the CP2102). The datasheet specifically indicates those pins you can leave floating, and this is not one of them. But it doesn't say what to do with it. It does say to optionally pull it high for noise immunity when used as an input, but not what to do if unused (I don't think I need to reset the CP2102). Most of the CP2102 examples I've seen online leave it floating, is this safe?Most of the examples I see online have 1k resistors in series on the RX/TX lines. I don't know what they're for or why they're needed. Given the very slow rate of UART, they can't be series terminating resistors, can they?

author: tooki
author_rank: Super Contributor
timestamp: August 08, 2021, 06:33:47 pm
content: The CP2102 datasheet answers the power questions, as this is the "configuration 3" described in section 10 of the datasheet.I can confirm that you can have the CP2102 running off USB and that nothing goes wrong with the ESP32 when USB is not connected. I am simply using the design from theESP32-DevKitC V4 board reference designunmodified, with the 3.3V regulator (and its input and output caps) unpopulated and instead feeding in 3.3V. I haven't measured whether any current flows (nor would it be easy to do so) in or out of the IO pins, but bear in mind that leaving USB unconnected and using external 3.3V power is an expressly supported configuration for those boards, so I don't think there's anything to worry about.It's probably safe to leave /RST floating, since they don't warn you to do so, but you can also just pull it up to 3.3V and call it a day. The ESP32 reference design above has it tied to 3.3V via a 2K resistor.Also a little heads-up, in case you're new to designing USB into your circuits: USB data lines are extremely picky. A fellow student of mine tried modifying the reference design, and he started by unrouting all the traces. He then drew them from scratch. Despite using the same schematic, those boards simply don't work: the USB traces on his boards are too circuitous and so the connection just fails. I then took a stab at it, by leaving the traces as-is and just adding another header for USB, so that I can attach a front-panel USB jack elsewhere. (Essentially, just an extension cord to the USB.) By using the trace length-matching feature in the layout program* to ensure the traces are of practically identical length, it worked.*in Altium, you set D+ and D- up as a differential pair and then use the appropriate layout function. I'm sure other programs work similarly.

author: Kaisha
author_rank: Contributor
timestamp: August 08, 2021, 08:40:50 pm
content: Quote from: tooki on August 08, 2021, 06:33:47 pmI appreciate the response, but I don't think that actually covers my use case.Maybe I wasn't clear about the situation. I want to only power the CP2102 when the USB is connected. But I still want to be able to power the ESP32 even when the USB is unconnected. The ESP32-DevKitC V4 schematic shows the CP2102 powered from their supplied LDO, but that doesn't cover the situation I was looking for.I've provided the schematic I'm playing around with in the attachments, so perhaps it will be more clear (or less... who knows, I'm still a beginner. The idea is the system runs off usb while it's plugged in, but can seamlessly switch to battery power when the usb cable is unplugged. The one big caveat is that the battery is a higher voltage than the usb.QuoteFair enough... but I kinda want to understand WHY.QuoteIs it even an issue with lower speeds?

author: tooki
author_rank: Super Contributor
timestamp: August 12, 2021, 03:40:04 pm
content: Quote from: Kaisha on August 08, 2021, 08:40:50 pmI gotcha (and actually think I can improve my own circuit!).Also, note that I overlooked and carried forward a probable small mistake: chances are, you actually mean the CP2102N, not the older, not-recommended-for-new-designs CP2102 (without an N). (They are almost identical, but there are some tiny differences, most notably that on the CP2102N, the voltage on the VBUS pin absolutely cannot be allowed to exceed certain limits — limits which are exceeded while the 3.3V regulator (whether internal or external) is starting up. Consequently, a voltage divider (as seen in the devkit schematic) between Vusb and the VBUS input is mandatory on it. Having the voltage divider is still recommended with the CP2102, so design your circuit with the voltage divider and you can populate either.) I should have been looking at the CP2102N datasheet, but my replies prior to this one are based on the CP2102. From here on, I'm expressly referring to the CP2102N datasheet, and assuming the CP2102N-GQFN28 (the 28-pin QFN version that is a drop-in replacement for the old version).Anyhow, what you want, then, is the bus-powered configuration (configuration in figure 2.1). But other than the capacitors, don't connect any load to its 3.3V output (for now — read on). This powers the CP2102N from USB, but not the rest of your circuit.Don't switch the Vusb line — i'm unsure whether the D+/D- inputs can tolerate input voltages when the chip is unpowered. Since the only load on it Vusb is the IC itself, which is a tiny load, you can ignore the power "wasted" when it's not needed.Now, as for the rest of the circuit: There are a lot of open questions, as the overall power architecture isn't fully explained here. (Like what the battery voltages are, whether they're rechargeable or primary cells, etc). How the rest of the circuit is powered kinda depends on that. Like if you are using rechargeables and want them to charge when on USB, that's one kettle of fish. If they charge elsewhere, a different kettle. If they're primary cells, another altogether.Quote from: Kaisha on August 08, 2021, 08:40:50 pmGeneral answer (and the one applicable to the old CP2102): it's never a good idea to leave inputs floating. Especially with modern CMOS electronics, whose inputs are MOSFET gates, the gate charge is so tiny that stray voltages (like from noise) can easily change an input's state. So a floating input is susceptible to instability. Now, the CP2102 doesn't expressly warn about it, so it could have a tiny internal pullup resistor. But it certainly does not hurt to "firmly" pull it to a known state.Specific answer: The CP2102N datasheet expressly recommends pulling /RST (now called "RSTb") to VDD via a 1K resistor. It reminds us that that pin is also used as an output, driven low when it resets, so the resistor is needed.Quote from: Kaisha on August 08, 2021, 08:40:50 pmYes,definitely. (Didn't I literally just give you an example of it not working duesolelyto trace layout??)These are USB Full-Speed (= 12Mbps) devices. The datasheet specs the rise/fall times of its USB ports at 4ns to 20ns. According to the standard bandwidth formula (0.35/Tr), the analog bandwidth needed is between 0.35/20ns =17.5MHzand 0.35/4ns =87.5MHz. That's nothing to sneeze at. Remember also that USB is packet-based, and the packets are sent between the USB host and the IC at the same speed, even if you're using a very slow baud rate on the UART. It'll just send packets less frequently.

author: Peabody
author_rank: Super Contributor
timestamp: August 12, 2021, 05:21:18 pm
content: My experience is also with the old CP2102. My memory is that both the 3.3V output pin and all the I/O pins (TX, RX, etc.) will sink current when the CP2102 is powered down. The current is significant - milliamps. I posted a Github repo on embedding the CP2102 in an MSP430 project, and went over the various options dealing with power. Here's the repo link:https://github.com/gbhug5a/MSP430-BSLDownload the PDF file from there. See "Circuit Design" beginning on page 11. As I say, that was all about the old chip, but some of it may be relevant.Also, the old CP2102 works fine with its reset pin NC.

author: Peabody
author_rank: Super Contributor
timestamp: August 12, 2021, 05:47:02 pm
content: Also, I'm confused by your drawing. You have three mosfets controlling the selection of Vbat and VUSB, but I think you only need one - the P-channel, but oriented "backwards", and a Schottky diode. The circuit below isn't exactly what you're doing, but it automatically switches between USB and battery power. The backwards orientation is needed to prevent the higher USB voltage from reaching back to the battery. The body diode blocks it in that orientation.

author: Kaisha
author_rank: Contributor
timestamp: August 16, 2021, 03:37:45 pm
content: I appreciate the replies, I'm sorry this is a bit late. I've been checking eevblog regularly but it didn't show new replies until today... weird.QuoteThe batteries are 2 lithium ion cells in series (I know it's a weird combination), which is why the 3 mosfets instead of just one. The battery will be higher voltage than the usb (so 9V to 6.6V range). The batteries will not be charged by the usb.QuoteI don't see a pdf file anywhere in that repo. Perhaps the link is wrong?

author: Peabody
author_rank: Super Contributor
timestamp: August 17, 2021, 03:33:05 am
content: The pdf is the second file from the bottom, just above the readme. Anyway, here's the direct link to that file, but you'll have to hit the download button to read it.https://github.com/gbhug5a/MSP430-BSL/blob/master/MSP430G-BSL-Using-Embedded-Adapter.pdf

author: Kaisha
author_rank: Contributor
timestamp: August 17, 2021, 12:05:38 pm
content: Thank-you. Not sure how I missed it. I will definitely take a look at it.

author: Peabody
author_rank: Super Contributor
timestamp: August 17, 2021, 01:14:15 pm
content: Back to the mosfets, why are you using two cells in series to back up an ESP32?

author: Kaisha
author_rank: Contributor
timestamp: August 17, 2021, 03:49:47 pm
content: The enclosure is already chosen. It has room for 2xAA sized batteries, but I'll be using 2x14500 sized lithium ion instead for the additional capacity.This means the usual single p-mosfet power switching doesn't work, since VUSB < VBAT. I think the 3 mosfet configuration should work, but I wouldn't mind a second opinion.

author: Peabody
author_rank: Super Contributor
timestamp: August 17, 2021, 10:26:38 pm
content: Putting the two batteries in parallel would give you about as much battery life as putting them in series, make the single mosfet work again, and let you charge from USB without needing a balanced charger. And then you could use a 3.3V linear regulator like the MCP1700 on the output.I haven't really studied your three-mosfet circuit, but just think stacking the batteries in series is making things more difficult than they need to be, with very little benefit, if any, in terms of battery life.

author: Kaisha
author_rank: Contributor
timestamp: August 18, 2021, 02:25:53 am
content: You are correct... But the enclosure is already chosen/fixed, I can't change the battery configuration.I also want to use a switching regulator for the added efficiency.

author: tooki
author_rank: Super Contributor
timestamp: August 18, 2021, 12:49:56 pm
content: Quote from: Kaisha on August 18, 2021, 02:25:53 amYou sure about that? You can probably fairly easily modify the contact configuration. Take a look at Keystone Electronics, they make gazillions of battery contacts.

author: Kaisha
author_rank: Contributor
timestamp: August 18, 2021, 07:05:46 pm
content: Modding it would not be easy, and I like it how it is. I don't think it's a big issue, the 3 mosfet setup should handle power switching between usb/vbat in the same way the 1 mosfet setup works (but with VBAT > VUSB). The buck regulator handles the voltage range with ease. Unless there's some error/issue I overlooked, that part of the project is pretty much finished.