News:

The Savage///Circuits website has been upgraded to a more efficient theme.

Main Menu

ESP32 as a USB host

Started by Jeff_T, Dec 10, 2025, 03:51 PM

Previous topic - Next topic

Jeff_T

I have been wanting to try this for myself for a while now, the videos I watched never really seemed to explain all the details but it turned out to be quite easy.

I posted a video of my example on YouTube, I hope it comes across as being an easy thing to do, if not I can expand a little more on how I did it.

Here is the link https://youtu.be/fQFY3lmTnSY

Chris Savage

Quote from: Jeff_T on Dec 10, 2025, 03:51 PMI posted a video of my example on YouTube, I hope it comes across as being an easy thing to do, if not I can expand a little more on how I did it.

Thanks for sharing. A lot of people have asked over the years how to do USB host mode. Many, I suspect, to make prank devices for computer users.  ;)

I only have examples of host mode for USB Drives, so I will watch your videos after work.

                    Bringing concepts to life through engineering.

Jeff_T

There are two things to mention that don't affect what's already been posted.

First I wanted to post the devkit boards that I used, I bought them during the Black Friday sale on Amazon for $4.70 a piece, a great price. They are still a good price at ~$6 a piece at this link https://www.amazon.com/dp/B0DG8L7MQ9

Secondly the example EspUsbHost code uses the Japanese keyboard code which did not align with my layout 100%, I think the library only has two options but I used setHIDLocal(HID_LOCAL_US)and it works fine.

JKnightandKARR

Quote from: Jeff_T on Dec 10, 2025, 03:51 PMI have been wanting to try this for myself for a while now, the videos I watched never really seemed to explain all the details but it turned out to be quite easy.

I posted a video of my example on YouTube, I hope it comes across as being an easy thing to do, if not I can expand a little more on how I did it.

Here is the link https://youtu.be/fQFY3lmTnSY
Haven't watched video yet, but, this can be used to make say a gamepad with?

Jeff_T

@JKnightandKARR , do you mean take input from a gamepad, I have not seen that with this particular library but there is another Arduino library that specifically mentions a USB gamepad, I will see if I can find the reference. If I owned a gamepad I would plug it in and try, every time I go to Goodwill I look to see if they have one someone may have donated but they go like hot cakes.

Do you have a project in mind? A wireless gamepad would be a cool build.


JKnightandKARR

#5
Quote from: Jeff_T on Dec 10, 2025, 08:17 PM@JKnightandKARR , do you mean take input from a gamepad, I have not seen that with this particular library but there is another Arduino library that specifically mentions a USB gamepad, I will see if I can find the reference. If I owned a gamepad I would plug it in and try, every time I go to Goodwill I look to see if they have one someone may have donated but they go like hot cakes.

Do you have a project in mind? A wireless gamepad would be a cool build.


Just an example, but if one wanted to make their own gamepad.  I have some arduino code todo that exact thing.

https://www.makershed.com/collections/make-kit/products/diy-arcade-joystick-kit
GHere is the kit I have, and the manual, has arduino code in it. Only mine was installed into a plastic storage container with a 3d printed template I created to hold the buttons, and joystick.

https://makershed.make.co/wp-content/uploads/sites/8/2023/05/Make_-DIY-Arcade-Controller-Arduino-Code.pdf
The code.

Chris Savage

Quote from: Jeff_T on Dec 10, 2025, 08:17 PMthere is another Arduino library that specifically mentions a USB gamepad, I will see if I can find the reference. If I owned a gamepad I would plug it in and try, every time I go to Goodwill I look to see if they have one someone may have donated but they go like hot cakes.

If you send me the link to one on Amazon and your address, I can ship one to you. It would be worth it to have some gamepad examples on here.

                    Bringing concepts to life through engineering.

Jeff_T

@JKnightandKARR great looking project so retro.

@Chris Savage let me look into what can be done with USB on the ESP, it seems there are a few recent examples on YouTube using various HID devices connected to some kind of Arduino set up as a host. If I find one related to gamepads we can surely post a similar version here.

Chris Savage

#8
Quote from: Jeff_T on Dec 11, 2025, 11:18 AMlet me look into what can be done with USB on the ESP, it seems there are a few recent examples on YouTube using various HID devices connected to some kind of Arduino set up as a host. If I find one related to gamepads we can surely post a similar version here.

Okay, either way, I am open to contribute to experimenting with the ESP32 by supplying the gamepad. I do have a Sega gamepad that is USB. Not sure how useful that would be to you. You can see a photo of it in this post.

                    Bringing concepts to life through engineering.

Jeff_T

The conversation about game controllers has really become interesting. I have not seen anything "out of the box" ready so I took a deep dive into the library I used in the video.

The USB protocol is complex and so is C++, producing a game controller host for the ESP32 is a fairly daunting task. I found some promising clues and I have decided to give it a shot with the hope of success being minimal.

On the positive side most of the hard work has been done in the EspUsbHost library, there is even a placeholder for a gamepad but unfortunately no C++ functions specifically for a gamepad. I think some of the existing code, in particular for a mouse, could be cloned and modified to suit. One of the things against us is that HID descriptors can vary even between the same type of devices so initially I am going to try an economical, simple and widely popular gamepad and see where that leads me. One other positive is that the EspUsbHost library has code, which is normally commented out, that will print the Report Descriptor to the Arduino serial monitor, this is a very useful tool.




Chris Savage

Quote from: Jeff_T on Dec 18, 2025, 07:33 PMI have not seen anything "out of the box" ready so I took a deep dive into the library I used in the video.

Jeff,

Whether it leads to anything or not, and with no expectations, if you're wanting to experiment, would having a game controller help? I could send you the Sega one I previously mentioned. There would be no cost and no expectations. If you think it would help in any way, I can send it to you, or if you find one on Amazon, I could have it drop-shipped.

                    Bringing concepts to life through engineering.

Jeff_T

Quote from: Chris Savage on Dec 19, 2025, 12:34 PM
Quote from: Jeff_T on Dec 18, 2025, 07:33 PMI have not seen anything "out of the box" ready so I took a deep dive into the library I used in the video.

Jeff,

Whether it leads to anything or not, and with no expectations, if you're wanting to experiment, would having a game controller help? I could send you the Sega one I previously mentioned. There would be no cost and no expectations. If you think it would help in any way, I can send it to you, or if you find one on Amazon, I could have it drop-shipped.

I know and appreciate that Chris, I can't promise anything but I feel quietly confident and I will try with a cheap USB SNES type pad as my test controller. If I can get that up and running then we can maybe talk about the SEGA pad.

For a mouse I have tried two different brands, a Logitech cordless and an IHome corded. Each mouse required a different code in the header file and I was pleased to have been able to figure that little puzzle out.

A PC is programmed to adjust to these variations between HID devices but the ESP32 host has to be done manually and I have read that some devices don't even expose their protocol. That's the reason I want to try what I consider might be the easiest to get working. I also need to end up with a type A or type C connector for plugging into the ESP controller but generally I figure we can make an adapter if needed.

I should have a $5 controller by this weekend so I can let you know how that goes.

Chris Savage

Quote from: Jeff_T on Dec 19, 2025, 04:28 PMI should have a $5 controller by this weekend so I can let you know how that goes.

I'm looking forward to your results.

                    Bringing concepts to life through engineering.

Jeff_T

The gamepad works ok, figuring out the data and data type wasn't too difficult. There are 8 buttons, action buttons A,B,X,Y , select and start and a left and right trigger buttons. The buttons were in two different data bytes and I masked them into a single byte so that I could enumerate them later. There was also a D-Pad with a range of 0-255 in both the x and y axis.

First I think I'll create a video as an example and then create a new thread here explaining what I did.

It involves 4 code files with the 4th being a simple Arduino example of usage, I just need to lay it all out in a simple way that is easy to understand.

I think changing controller type requires modifying code, I don't see a way around that but if the example is done right anyone should be able to figure how to make the modifications. Apart from that this SNES clone is a pretty decent controller for around $5.

Chris Savage

Quote from: Jeff_T on Dec 20, 2025, 09:50 PMThe gamepad works ok, figuring out the data and data type wasn't too difficult. There are 8 buttons, action buttons A,B,X,Y , select and start and a left and right trigger buttons. The buttons were in two different data bytes and I masked them into a single byte so that I could enumerate them later. There was also a D-Pad with a range of 0-255 in both the x and y axis. First I think I'll create a video as an example and then create a new thread here explaining what I did.

Great news. Looking forward to your video / post.

                    Bringing concepts to life through engineering.