Skip to content

Instantly share code, notes, and snippets.

@spuder
Last active August 28, 2022 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spuder/e3d233bbe68968bd9c499a8fef12e9d9 to your computer and use it in GitHub Desktop.
Save spuder/e3d233bbe68968bd9c499a8fef12e9d9 to your computer and use it in GitHub Desktop.
digispark USB fun

Digispark doesn't allow using the normal Serial.println() that an arduino uses becuase it doesn't show up as a com port.

Instead you need to use hidapitester or https://github.com/Bluebie/digiusb.rb ruby app to read from USB data.

Digispark has VID/PID of 16C0/05DF

bluebie/digiusb

Start the digterm ruby app and enter 1 in the terminal. When 1 is pressed, the blue LED (pin 1) should turn on for 1/10 of a second

gh repo clone Bluebie/digiusb.rb
cd digiusb
gem install colored
gem intall libusb
./bin/digiterm
>hello world
>1 
// To upload this sketch to digispark, you must either use platform.io or a really old version of arduino on a 32 bit mac (64 bit wont work)
// You more than likely can't plug the digispark into a usb 3.0 port. Use a usb 2.0 port or a usb extenstion cable
// https://www.youtube.com/watch?v=hVQubqgdoZg
// To actually upload code with platform.io, click upload, then unplug/replugin in the device within 5 seconds.
#include <Arduino.h>
#include <DigiUSB.h>
void setup() {
pinMode(1, OUTPUT);
digitalWrite(1,HIGH);
delay(500);
digitalWrite(1,LOW);
DigiUSB.begin();
}
void get_input() {
int lastRead;
// when there are no characters to read, or the character isn't a newline
while (true) { // loop forever
if (DigiUSB.available()) {
// something to read
lastRead = DigiUSB.read();
//If you type 1, it will turnon the LED for 100 ms
if (lastRead == '1') {
digitalWrite(1,HIGH);
delay(100);
digitalWrite(1,LOW);
}
DigiUSB.write(lastRead);
if (lastRead == '\n') {
break; // when we get a newline, break out of loop
}
}
// refresh the usb port for 10 milliseconds
DigiUSB.delay(10);
}
}
void loop() {
// print output
DigiUSB.println("Waiting for input...");
// get input
get_input();
}
@spuder
Copy link
Author

spuder commented Aug 28, 2022

Send 42 from digispark to PC once a second

define USB_CFG_DEVICE_NAME     'D','i','g','i','B','l','i','n','k'
#define USB_CFG_DEVICE_NAME_LEN 9
#include <Arduino.h>
#include <DigiUSB.h>

void setup() {
    DigiUSB.begin();
    pinMode(0,OUTPUT);
    pinMode(1,OUTPUT);
    pinMode(2,OUTPUT);
}

unsigned long t = 0;
void loop() {
  DigiUSB.refresh();
  if (t==0 || millis() > t+1000) {
    digitalWrite(1, HIGH);
    delay(50);
    digitalWrite(1, LOW);
    DigiUSB.write(42);
    t = millis();
  }
}

hidapitester read data

hidapitester --vidpid 16C0:05DF --usagePage FF00 --open --read-feature 0 --close

2A = 42

Opening device, vid/pid:0x16C0/0x05DF, usagePage/usage: FF00/0
Device opened
Reading 64-byte feature report, report_id 0...read 2 bytes:
 00 2A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Closing device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment