USB Host Shield Connecting to any USB Device???

Hello Arduino World and Members,

I am an intermediate programmer in Arduino, not the best, but do know my way around.

Just wondering if someone can lead me the right way to get information on how to get data from a USB Sensor (transducer sensor) using and arduino uno and USB HOST Shield rev2. I tried playing around with the mouse Code sample, but unsuccessful. I really don't understand how the Host USB Shield is getting information from the USB Device. This is where i need help. if there is a template or a sample code (basic) to start with that will be great.

Thanks in advance..

Have you exhausted all the documentation available on-line for the USB host shield?
Paul

Thank you Shannon for your reply.

Most of the information I use as a reference is this is link https://chome.nerpa.tech/mcu/connecting-barcode-scanner-arduino-usb-host-shield/comment-page-1/. It helps a bit, but maybe i am not understanding the logic. That is why i would like to know if someone has a basic sketch, that i can play with and modify it to what i need it for. and then of course share the results in case others come across the same problem i did.

with great appreciation and regards,

USB documentation begins by explaining the negotiations that MUST go on to set the basic operating parameters such as the data speed rate and support for various levels of the USB protocols. You really need to find the basic documentation.

Paul

I have the exact same question and all the example codes i found did not help.
Unfortunately it’s unclear for me how to send/receive commands (strings) to/from USB device connected on USB host shield. Everything i have tried so far was without success.

Currently i am controlling a USB turntable via serial monitor from a PC.
I only need a serial terminal (like the one from the Arduino installation) to send some commands to the turntable so that it starts rotating. The only thing i need to do is to set the baudrate to 115200 and line feed to (CR). The turntable sends back some simple status information. See illustration and screenshot (terminal window from PC which is connected to the turntable) below.

I would like to control the turntable with Arduino Uno and USB Host Shield so that all the commands are sent from the Arduino to the Turntable. Beside this the Arduino need to read the feedback to trigger next actions.
It seems to be easy thing but i could not find anything in the internet how to do this. I was not able to modify existing code from e.g. XBox or PS3/4 controller, barcode reader.
My questions:

  • Is it possible to use the USB Host Shield to do what i want to do?
  • What would be a very easy arduino code to send the command/string “move 312” to the turntable and read out the feedback send from the turntable to the Arduino? Can somebody help me here?

Thanks for your help and support!
Chris

  ##########################                     ###################
  # PC (COM4, 115200 Baud) #--------USB----------# Turntable (USB) #
  ##########################                     ###################

                 >>>>>>>>>>>>>>> move 312 >>>>>>>>>>>>>>>

                 <<<<<<<<<<<< status running <<<<<<<<<<<<

                 <<<<<<<<<<<< status stopped <<<<<<<<<<<<

#########################################################################
Planned Situation:

  ###################                     ###################
  #    Arduino Uno  #                     #                 #
  #        +        #--------USB----------# Turntable (USB) #
  # USB Host Shield #                     #                 #
  ###################                     ###################

                 >>>>>>>>>>>>>>> move 312 >>>>>>>>>>>>>>>

                 <<<<<<<<<<<< status running <<<<<<<<<<<<

                 <<<<<<<<<<<< status stopped <<<<<<<<<<<<

#########################################################################

Hey guys, thanks a lot for the help. I finally i understood how to read from any USB. The part where I was confused and didn’t understand was the part of the driver part of the USB. Well it all depend on the chip set the the USB you are trying to connect with the Host USB Shield has, In most cases it will be the PL2303**. in other cases it will be THe FTDI Chip set.
And the Library has example for both.

In my case with the Omega PX409-USBH Series pressure transducer. I was able to communicate with it, but because it asks for a command ref to send information, I couldn’t get it going and of course the part for sending is not 100% all there in the library. anyways, I simulated that process by plugging a pl2303 on the computer and another on the USB Host Shield. i was able to send from my PC but i wasn’t able to send anything from the HOST to the PC . see the pic below.

the code i used is straight form the samples from the USB Host Shield 2.0 Library \ pl2303 samples. they basically use the same for gps, gprs, and xbee, and i modified it to my needs. I am still working with it, and will keep updating my progress.

if anyone has doubts or if the code is not clear or can be improved. please feel free to respond, as no question is too dumbs in cases of like these learning process.

/* Arduino terminal for PL2303 USB to serial converter  */
/* Inserts linefeed after carriage return in data sent to and received from usb device */
/* USB support */
#include <usbhub.h>
/* CDC support */
#include <cdcacm.h>
#include <cdcprolific.h>
//#include "pgmstrings.h"
#include <avr/pgmspace.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
//#include <spi4teensy3.h>
#endif
#include <SPI.h>

class PLAsyncOper : public CDCAsyncOper
{
public:
    uint8_t OnInit(ACM *pacm);
};

uint8_t PLAsyncOper::OnInit(ACM *pacm)
{
    uint8_t rcode;

    // Set DTR = 1
    rcode = pacm->SetControlLineState(1);

    if (rcode)
    {
        ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
        return rcode;
    }

    LINE_CODING	lc;
    lc.dwDTERate	= 9600;
    lc.bCharFormat	= 0;
    lc.bParityType	= 0;
    lc.bDataBits	= 8;

    rcode = pacm->SetLineCoding(&lc);

    if (rcode)
        ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);

    return rcode;
}
USB     Usb;
//USBHub     Hub(&Usb);
PLAsyncOper  AsyncOper;
PL2303       Pl(&Usb, &AsyncOper);

uint32_t DELAY_TIME;


void setup()
{
  Serial.begin( 9600 );
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  Serial.println("Start");

  if (Usb.Init() == -1)
      Serial.println("OSCOKIRQ failed to assert");

  delay( 200 );

  DELAY_TIME = millis() + 5000;
}

void loop()
{
    Usb.Task();

    if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
    {
       
        uint8_t  rcode;
        char strbuf[] = "P" ;

               /* reading the keyboard */
       if(Serial.available()) {
         uint8_t data= Serial.read();

         if ( data == '\r' ) {
           Serial.print("\r\n");  //insert linefeed
         }
         else {
           Serial.println( data,"\r" );  //echo back to the screen
         }


         /* sending to the usb Device */
         rcode = Pl.SndData(1, &data);
         if (rcode)
            ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
       }//if(Serial.available()...

       delay(50);

        /* reading the converter */
        /* buffer size must be greater or equal to max.packet size */
        /* it it set to 64 (largest possible max.packet size) here, can be tuned down
        for particular endpoint */
        uint8_t  buf[64];
        uint16_t rcvd = 64;
        rcode = Pl.RcvData(&rcvd, buf);
         if (rcode && rcode != hrNAK)
            ErrorMessage<uint8_t>(PSTR("Ret"), rcode);

            if( rcvd ) { //more than zero bytes received
              for(uint16_t i=0; i < rcvd; i++ ) {
                if( buf[i] =='\r' ) {
                  Serial.print("\r\n");  //insert linefeed
                }
                else {
                  Serial.print((char)buf[i]); //printing on the screen
                  Serial.print("");
                }
              }
            }
        delay(10);
    }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.