Looking for cheap ICs to use as a USB bridge to interface with atmega328p (Atmega16u2 is $$$)

Hi all,
I know almost everything is backorder these days but might as well ask

I am wondering if anyone had already built PCBs with USB functionality?
I never built a usb interface myself, but I would like to do it and start off with a simple USB game controller. something simple: buttons --> atmega328p --> usb-bridge --> PC

I inherited from a full spool of smd Atmega328p's so I want to do all of my projects with this chip. The Atmega32u4 are expensive and too anyway...

So, I am looking for a cheap and easy to use chip that could be used as a usb bridge?
I have a atmel ICE, and I use platformIO, so ideally something that could be programmed with these tools would be great. SMD too preferably.

Thanks a lot!

For simple, low speed applications like yours, V-USB works with many different AVR chips, even some ATTiny processors.

To expand on V-USB, it is a fun play.
String numeric output with V-USB HID - Community / Exhibition / Gallery - Arduino Forum

Do NOT go cheap on the zener diodes!

Understanding you prefer SMD but understanding the old "bird in the hand" saying, these are cheap:

Amazon.com: HiLetgo 5pcs USB to Serial USB to TTL CH340 Module with STC Microcontroller Download Adapter : Electronics

Amazon.com: Ximimark 4Pcs USB 2.0 to TTL UART 5PIN Module Serial Converter CP2102 STC PRGMR Free Cable : Electronics

CH340 is usually the cheapest to get, especially if you import directly from China. There are many versions of this chip; one of the notable differences is that some require an external 12MHz ceystal while others don't. This can obvipusly impact total costs and pcb size. They also come in different footprints.
I've used CH340G mostly with an external 12MHz crystal; works fine if you follow the (scarce) pointers in the datasheet.
CP2102 (and family members) is popular as well, but more costly and not necessarily better IME.

I've done several designs using Microchip's MCP2221. It's very easy to use and requires no drivers for I2C communication and configuration on the PC since it's a HID device.

CDC driver is available and automatically installed for Windows. Mainline support in Linux.

Out of stock everywhere though.

https://www.microchip.com/en-us/product/MCP2221

CH340 is usually the cheapest to get
CP2102 (and family members) is popular as well

Note that CH340 and CP2102 are (AFAIK) exclusively USB/UART bridge ICs, probably unsuitable for the OP's intended "game controller."

1 Like

Good catch!

Thank you! I tried working with V-USB in the past few days, but I could not achieve anything.

I followed this tutorial for the hardware AVR ATtiny USB Tutorial Part 1 | Code and Life and for setting up the vusb folder and usbconfig file. To my understanding, for a HID device, I do not need to setup a CLI software on the host side, I should only need the firmware on the atmega328p and this should be enough to operate as a keyboard?

However, I did not want to mess with makefiles and c programming so I basically just dumped the v-usb library in a platformIO Arduino project and added it to the library paths.

I simply copy pasted the HIDKeys example in my main.cpp V-USB - A Firmware-Only USB Driver for Atmel AVR Microcontrollers

I did have to change main() for setup() and loop() however. and I also changed void pointer castings into struct castings

  usbRequest_t *rq = (void*)data;

into

  usbRequest_t *rq = (usbRequest_t *)data;

Since the former would cause an error in C++.

Now, everything compiles and the hardware isn't different. But it doesn't work, I get usb error 43 when I plug the usb jack in the PC.

I wonder if the whole attempt of using platformIO with Arduino core is what causes issues, or maybe its the way I typecast the usbRequest_t struct that creates padding errors, or maybe my understanding of HID devices is wrong and I need to install drivers or to run some kind of software on the host side?

Anyway, thanks a lot for your help

Correct, but depends on the game. It wasn't specified. Might have been a custom/DIY game that could read input from a serial port.

I'm not sure that v-usb is compatible with being "dump into" an Arduino sketch.

There are also reports that V-usb is losing compatibility with modern desktop USB controllers and drivers.

I did a test last week with a V-USB analog logger from 10 years back! On my Surface Pro 4 Win 11 machine, there was no issue. Who knows about other machines, but as long as the USB controller conforms to USB 1.1 specs (old keyboard or very old mouse could be a compatibility test) things should work, IMO. The real issue with V-USB is that the zener diodes are very critical, I ordered quality Vishay part.

I would recommend going back to a 2012 Arduino IDE version.

Sometimes I forget that the Adafruit Trinket/TrinketPro is essentially V-USB (while Frank Zhao worked for Adafruit; Frank designed the USBSnoobie board.) So much for the history lesson.

I pulled up a Atmega328P sketch for a trinketPro and it compiles under IDE 1.8.18 on Win-11 with no errors.

The Zip and required library are attached for anyone wishing to take a trip back in time (TrinketPro OR homebrew USB-V req.)

// *******************************
// HID Serial Example
// Print analog sensor values
// RAYSHOBBY.net
//
// This program opens an HID serial channel and shows how to send and receive messages from a host
// computer. To use it, you need to run the HIDSerialMonitor from a host computer. The program
// repeatedly prints out the value on a specific analog pin. Typing 'a?' (where ? is a number between
// 0 to 5) in the serial monitor changes the selected pin.
// *******************************

#include <HIDSerial.h>
#include <Streaming.h>

HIDSerial serial;

unsigned long t = 0;
unsigned char buffer[32];
unsigned char adcCh = 0;

void setup() {
  serial.begin();
  serial << "HID Serial For Trinket" << endl;
}

void loop() {

    int value;
    if (millis() > t + 2000)
    {
        t = millis();
        serial << "A" << adcCh << " =  " << analogRead(adcCh) << endl;
        // serial.write('A');  
        // serial.print(adcCh);
        // serial.print(" = ");
        // serial.println(analogRead(adcCh));
    }
    
    if(serial.available()) {
        int size = serial.read(buffer);
        if (size!=0) {
            //serial.write((const uint8_t*)buffer, size);
            if(buffer[0]=='a' || buffer[0]=='A') {
                if(buffer[1]>='0'&&buffer[1]<='5') {
                    adcCh = buffer[1]-'0';
                    serial.write('A');
                    serial.print(adcCh);
                    serial.println(" selected.");
                }
            }
        }
    }
    serial.poll();
}

HIDSerial.zip (111.6 KB)

For history buffs:
usbsnookie frank zhao - Google Search

image

1 Like

That's the thing, though. As I understand it, V-USB doesn't quite conform to the specification...

First bullet on the ObDev site:

  • Fully USB 1.1 compliant low-speed device, except handling of communication errors and electrical specifications.

But, I concede that I have only experimental experience and that was mostly 10 years ago; however, every one of my many designed worked (at least one still does under Linux Mint and Windows 11 on HP Elitebook and Microsoft Surface.)

I have (personally) never considered it prime-time commercial technology but apparently the folks at Objective Development think so (or did) as commercial licenses are (were) available.

So apart from the IDE, is there anything wrong with my approach? Its just that I feel like Im working with a black box and I find it hard to determine where to start debugging. It doesn't work and I cant tell what is wrong. Ill check on the scope to see if st least something comes out of the data lines, but otherwise, the data could be jibberish and I wouldn't know.

I didnt mention, The tutorial used a 3v3 reg, so that is what I am using. This voltage is out of spec for the 16mhz crystal i am using, but I read it was mostly reliable and I tested with other programs. But it may not work with usb, timing may be more critical. I will try at 5v with the zeners

That is all I can think of.

NO!
The zeners are 3.6 Volt as USB is 3.3V even if the USB-PC cable provides 5.0 Volt.

3.3 and 5.0 circuits are discussed under hardware:
V-USB - A Firmware-Only USB Driver for Atmel AVR Microcontrollers (obdev.at)

. If you need to run the AVR at 5 V, add 3.6 V zener diodes at D+ and D- to limit the voltage.

I meant vcc at 5 volts, not 5v zeners.

The signal is literally 0v flat when I power on with the scope. (I just plugged the scope on the USB D+)

I also wasn't able to test the zener circuit, I do not have any zener with zener voltage under 4v...

But I was thinking I could try the RT232, I have a TTL to USB in my possession. But I have a hard time understanding how I could use these chips to create a keyboard or game ctrler... If I check the device class, its undefined (0xFF)


Since its not a HID device, how the hell am I supposed to work with this?
This thread says I just can't Can i use PL2303 USB to TTL Serial Converter Module send keystrokes using Arduino nano? - Arduino Stack Exchange

But the CH340 modules you were referring to are clearly the same type of device.. What to think of this!?

With a USB to serial converter you would need some software running on the host that converts serial commands to keystrokes (or whatever you're gonna need). I personally wouldn't bother with v-usb, either get a microcontroller with a USB peripheral or stick to the serial based approach.

1 Like