🧩 Step-by-Step: Arduino USB Connect and Inject Function
1. Use a USB-Capable Arduino Board
- Recommended: Arduino Leonardo, Micro, or Due
- These boards use the ATmega32U4 or SAM3X8E chips which support native USB HID (Human Interface Device)
2. Connect Arduino via USB
- Plug your Arduino into your PC using a USB cable
- Open the Arduino IDE
- Go to Tools > Port and select the correct COM port
- Select your board under Tools > Board
3. Inject Data via USB (Serial or HID)
You can send data from your PC to Arduino using:
- Serial Monitor (text input)
- Custom software (e.g., Python script using
pyserial
) - USB HID emulation (keyboard, mouse, joystick)
Example: Serial Injection
String input; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { input = Serial.readStringUntil('\n'); if (input == "LED_ON") { digitalWrite(13, HIGH); } else if (input == "LED_OFF") { digitalWrite(13, LOW); } } }
Type LED_ON
or LED_OFF
in the Serial Monitor to control the onboard LED.
4. Run USB Functions (e.g., Emulate Keyboard or Mouse)
Example: Emulate a USB Keyboard
#include
This sends keystrokes to the connected PC when the Arduino boots.
5. Connect to USB Hardware (Advanced)
If you want Arduino to control external USB devices (like a USB flash drive or printer), you’ll need:
- A USB Host Shield
- Libraries like
USBHost
orUSB_Host_Shield_2.0
- Compatible board (e.g., Arduino Mega or Uno with shield)
Would you like help setting up a USB Host Shield or writing code to interact with a specific USB device like a flash drive or game controller?
No comments:
Post a Comment