USB host for Arduino - Page 1
Forum section: electronics / projects

author: Miti
author_rank: Super Contributor
timestamp: October 19, 2021, 09:26:42 am
content: I’m trying to connect an USB bar code scanner to Arduino for a stand alone functional tester. Do you have any experience with this and is there any USB host to serial adapter that you can recommend? I have Google, I want experience, preferably easily obtainable in NA. I know I could use PS2 but I need USB.Cheers,Miti

author: rs20
author_rank: Super Contributor
timestamp: October 19, 2021, 09:50:49 am
content: Just to clarify requirements, what kind of USB device does the barcode scanner appear as? IIRC, USB barcode scanners normally appear as USB keyboards, right? (I.e. they just "type out" whatever barcode they see?)If that's the case, then I assume one promising lead could be a USB-keyboard to PS/2 adapter (the more unusual USB-device to PS/2-host variant), assuming you can get the arduino to speak PS/2.

author: capt bullshot
author_rank: Super Contributor
timestamp: October 19, 2021, 09:54:00 am
content: If you need USB host capabilities, a Raspberry Pi would be the better choice over Arduino.

author: rs20
author_rank: Super Contributor
timestamp: October 19, 2021, 09:59:32 am
content: I agree that an RPi is definitely worth exploring.Can also consider:https://www.hobbytronics.co.uk/usb-host-dip(better alternatives may exist?)

author: newbrain
author_rank: Super Contributor
timestamp: October 19, 2021, 12:37:50 pm
content: Quote from: capt bullshot on October 19, 2021, 09:54:00 amDepending on the needs, also a Raspberry Pi Pico might be a cheap solution for a HID USB host.

author: voltsandjolts
author_rank: Unknown
timestamp: October 19, 2021, 12:48:58 pm
content: I've used these as host interface for FTDI devices, works well.This one is USB keyboard host which apparently works with barcode readers.https://www.hobbytronics.co.uk/usb-host-keyboardQuoteEdit: just noticed rs20 pointed to same, beat me to it.

author: Ian.M
author_rank: Super Contributor
timestamp: October 19, 2021, 01:22:01 pm
content: Although its probably not cost-effective vs the previous suggestions, Arduino did used to do a USB Host Shield, using the SPI interface MAX3421E USB controller which is USB 2.0 full and low speed host capable. See:https://docs.arduino.cc/retired/shields/arduino-usb-host-shieldClones of the USB host shield are still widely available, which would allow you to read keystrokes from a HID keyboard (or other device that emulates one) on anything from an Arduino Uno upwards. The easiest way of finding the correct library to install is to use the library manager so search forMAX3421E, which gets you Oleg Mazurov's USB Host Shield Library 2.0, which has plenty of exampes.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: October 19, 2021, 03:53:11 pm
content: There are a number of boards in the official "Arduino" family. If by "Arduino", you mean the very popular UNO, then some of the previous replies will apply.If instead you just want to stay in the Arduino programming world, you could consider a board with USB host capabilities, such as theArduino Zero. There are also other third party Arduino compatible boards based on the same SAMD21 microprocessor family, such as theSparkFun SAMD21 Mini BreakoutandAdafruit Feather M0.Here is a library that can talk, as a USB host, to various USB devices, including a keyboard, which is what a bar code reader likely emulates.https://github.com/gdsports/USB_Host_Library_SAMD

author: Nominal Animal
author_rank: Super Contributor
timestamp: October 19, 2021, 04:09:09 pm
content: I'd suggest aTeensy 4.1(about $27 USD), plus theUSB Host connector(about $3 USD, only need to solder standard 4-pin 0.1" spacing pin header). I also like to get theEthernet kit($4 USD) just in case I want to use the hardware 10/100Mbit wired Ethernet connection also.Note that you can connect the main USB connector to your development host while the barcode reader is connected to the Teensy USB Host connector, as long as the barcode reader doesn't require too much current (keeping the maximum current from the development host via the USB bus under 500mA). If using external power, note that the board does not have diodes, so you'll need to cut the VUSB trace (as described in the Teensy card) to make it externally powered only; or use an USB cable with the +5V line disconnected to connect the Teensy to your development machine while externally powered.Of course, you'd need to port your Arduino project over toTeensyduino, the add-on for Arduino for Teensy support. Biggest differences are in fast I/O port access (instead of PORTD etc., you usedigitalWriteFast(); like in standard Arduino,digitalWrite()works too), interrupts, and timers: Teensyduino has easier interfaces for using all those.Teensyduino developer, Paul Stoffregen, is a frequent contributor to Arduino (authored many common libraries et cetera), and I've found theTeensy forumsuseful.Oh, forgot. Teensy 4.x has 1,048,576 bytes of RAM, and can run at up to 600 MHz. (It's a really powerful Cortex-M7 microcontroller from NXP.) With onePSRAM chip($2 USD) soldered to SMD pads on the board, you can add 8 MiB of RAM; and you can use up to two of those PSRAM chips, if you find you need lots of RAM. This RAM appears as ordinary memory from address 0x70000000 upwards, so you can use it for your own data without worrying about bank switching or that kind of nonsense.(I'm not affiliated, just a very satisfied and happy customer. I also occasionally help out on the forum there, too.)

author: tooki
author_rank: Super Contributor
timestamp: October 19, 2021, 04:20:28 pm
content: Quote from: Miti on October 19, 2021, 09:26:42 amThe majority of commercial-grade bar code scanners are multi-interface, typically supporting at least RS-232, USB, and PS/2 "keyboard wedge" (i.e. PS/2 with passthrough for an actual keyboard) modes. You just connect a different cable to it.So if you're not committed to a particular USB-only scanner, I'd look for a model with RS-232 (easily converted to UART/TTL signals with a MAX232 or equivalent), or maybe even PS/2, since there are PS/2 keyboard input libraries for Arduino.There are also bar code (1D/2D, even) scanner modules with UART interfaces that are easily controlled by Arduino.

author: Miti
author_rank: Super Contributor
timestamp: October 20, 2021, 12:17:46 am
content: Thanks a lot for all the info! I need some time to digest it.I prefer to stay with the Arduino Mega 2560. It is just a simple LCD and keypad tester, all through an I2C interface.I have all the libraries I need and there's no time to discover. Maybe some other time.

author: sleemanj
author_rank: Super Contributor
timestamp: October 20, 2021, 01:40:41 am
content: The barcode reader in keyboard mode (which is the default generally)mightwork with a USB PS/2 Adapter, which is nothing fancy just a dumb pin to pin adapter, so you could try it out and see all you need is a USB-A socket and some wire. See the reply in this thread for the wiring....https://forum.arduino.cc/t/is-it-possible-to-connect-a-usb-keyboard-to-the-arduino-without-a-shield/631323

author: tooki
author_rank: Super Contributor
timestamp: October 20, 2021, 02:17:04 am
content: Quote from: sleemanj on October 20, 2021, 01:40:41 amI would not expect a USB bar code scanner to support a passive PS/2 adapter, since they normally support PS/2 mode by using a different cable altogether. (Configuration of the scanner is performed by scanning special bar codes.)

author: sleemanj
author_rank: Super Contributor
timestamp: October 20, 2021, 03:04:35 am
content: Quote from: tooki on October 20, 2021, 02:17:04 amConveresely, if I was making a barcode reading wand and wanted to sell both a USB and a PS/2 version/cable, I'd make the wand capable of both just like a keyboard is, and the cable be just that, a cable.I have an el-cheapo wand it only has a fixed USB cable, defaults to keyboard mode (also can appear as a USB serial port). Maybe I should test the theory sometime.I just tested with my el-cheapo wand, it didn't work with that one, but the manufacturer doesn't have a PS/2 version by the looks so perhaps not much of a data point, but i didn't put any effort in either.

author: spostma
author_rank: Regular Contributor
timestamp: October 20, 2021, 09:43:16 pm
content: the lowest cost USB Host option would be the CH554; it even has DMA:https://github.com/Blinkinlabs/ch554_sdcchttps://github.com/Blinkinlabs/ch554_sdcc/tree/master/examples/usb_host_KMhttps://www.mikrocontroller.net/topic/462538?page=single(german)and the CH554 toolchain for Arduino fans:https://github.com/DeqingSun/ch55xduinoYou could use it us USB->PS2 or USB->UART converter.

author: westfw
author_rank: Super Contributor
timestamp: October 22, 2021, 04:56:00 am
content: QuoteThere are "USB Host Shields" for Arduino, usually based on the MAX3421. But they tend to be more expensive that boards that have native support for USB host (like the Teensies and SAMD51 boards.))Or you could look around to see if anyone is still selling the "Arduino Mega ADK" (Android Development Kit) - this is essentially a mega2560 with the MAX USB host chip already on the board.

author: Miti
author_rank: Super Contributor
timestamp: October 22, 2021, 07:34:12 pm
content: Quote from: westfw on October 22, 2021, 04:56:00 amThat’s a very interesting idea, thanks! I didn’t know about this board, even though it is right there in the boards manager.Do you know a good usb hid host library for that board?

author: salihkanber
author_rank: Regular Contributor
timestamp: October 22, 2021, 07:44:55 pm
content: I've found some videos I hope they might help:

author: Miti
author_rank: Super Contributor
timestamp: October 22, 2021, 08:07:16 pm
content: Right, it is right there on arduino.cc, USB Host Shield Library 2.0. I’ll give it a try.Cheers,Miti

author: rpiloverbd
author_rank: Regular Contributor
timestamp: December 12, 2021, 10:14:08 am
content: I think using an USB host shield will be the best option. Here you can find a detailed tutorial:https://www.theengineeringprojects.com/2020/12/introduction-to-arduino-usb-host-shields.html