Exploring USB Device Detection with Arduino and USB Host Shield

 

Exploring USB Device Detection with Arduino and USB Host Shield

USB (Universal Serial Bus) is a ubiquitous interface for connecting peripherals to computers and other devices. Arduino, with its versatility and expandability, can be used to interface with USB devices using a USB Host Shield. In this article, we'll explore how to detect and identify USB devices connected to an Arduino using a USB Host Shield.


Hardware Used:

HardwareDescription
Arduino BoardAny compatible Arduino board with sufficient GPIO pins and USB Host Shield library support.
USB Host ShieldA shield specifically designed for USB host functionality, compatible with the selected Arduino board.
USB DevicesVarious USB peripherals like mass storage devices, input devices (keyboard, mouse), communication devices, etc.
Serial MonitorA computer running Arduino IDE or any other serial terminal software for monitoring the Arduino's serial output.
Power SupplyDepending on the connected USB devices and Arduino board, a stable power supply may be required for proper operation.
Optional ComponentsBreadboard, jumper wires, enclosures, LEDs, resistors, switches, etc., for prototyping and customization.


Hardware Setup:





Arduino sketch:


Verifying the VIP and PID:



Link:


Arduino sketch:

#include <usbhub.h>
#include <SPI.h>

USB Usb;
USBHub Hub(&Usb);

#define MAX_ROOT_PORTS 2 // Assuming there are 2 root ports on your USB host shield

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait until Serial is ready
  SPI.begin(); // Initialize SPI
  if (Usb.Init() == -1) {
    Serial.println("Failed to initialize USB Host Shield");
    while (1); // Halt if initialization fails
  }
  Serial.println("USB Host Shield initialized");
}

void loop() {
  Usb.Task();
  delay(100); // Adjust delay as needed
   if (Usb.getUsbTaskState() == USB_STATE_RUNNING) {
    for (uint8_t port = 0; port < MAX_ROOT_PORTS; ++port) {
      USB_DEVICE_DESCRIPTOR dev;
      if (Usb.getDevDescr(port, 0, sizeof(dev), (uint8_t*)&dev)) {
        // Print VID and PID of connected USB devices
        Serial.print("VID: ");
        Serial.print(dev.idVendor, HEX);
        Serial.print(", PID: ");
        Serial.println(dev.idProduct, HEX);
      }
    }
  }
}

Comments

Popular posts from this blog

555 Timer Astable mode, Lt spice simulation, and real-time testing

PIC12F675 LED Blinking Code