Channels ▼

Al Williams

Dr. Dobb's Bloggers

Leonardo's Watching

June 26, 2013

Last time I looked at how easy the Arduino Leonardo makes it to build a USB HID device. The Leonardo can act like a serial device, a keyboard, or a mouse. It can even act like all three at once. A lot of processors can do this, but the Leonardo's software libraries make USB coding trivial, and that's a welcome relief.

More Insights

White Papers

More >>

Reports

More >>

Webcasts

More >>

I wanted a simple but useful project to show just how simple a USB HID device can be. I happened to have an HC-SR04 ultrasonic ranging module sitting around my office. If you haven't seen one, it is a small PC board (about the size of a USB stick). On the board are two silver things that look like dime-sized speakers. One of them is a special speaker and the other is a microphone. You supply 5V and ground. Then you pulse an input pin to the device. It emits an ultrasonic ping and measures the time it takes for the sound to return to the microphone. It then pulses a signal back to you on an output pin to indicate how far the sound travelled.

That sounds complicated (and it actually is — for example, distant objects require more gain than near objects, so you have to increase the gain over time) but the device is very inexpensive. I can't recall seeing one for more than US$10, and if you shop online you can find them for significantly less than that.

They are popular in robots, but you can also use them as liquid level sensors, to make a digital tape measure, or anywhere you need to sense and range an object. There are a few Arduino libraries available for the device, but I didn't use anything. It is simple enough and for the project I had in mind; I didn't really need the exact range, anyway.

What did I have in mind? I want to mount the little ultrasonic board over my monitor and let it watch for when I leave my computer. By sending some keystrokes to the computer, it can activate the screen saver when I leave. When I return, it can then dismiss the screen saver for me. Since the screensaver quickly powers the monitor down, this is a green project.

I use Linux about 99% of the time, and on Linux I prefer KDE, so my code is made for that environment. You'd have to modify it slightly for Windows or Gnome, most likely. There was a small catch, though. Even though the Leonardo's mouse emulation worked, the KDE screen locker didn't recognize those mouse movements as sufficient to stop the screen saver. I didn't bother figuring out if I just hadn't moved it enough, or if it ignores motion on only one axis, or if there was some other issue. I just wound up using the keyboard for both functions.

The code to drive the ultrasonic board was easy to write, especially since I didn't care about how far away the sensed object really is. Just knowing it is further away (or closer) than it was last time is enough to create the program. Here's the function that drives the sensor board:

Here's the function that drives the sensor board (you can also download the entire file here):

unsigned distance()
{
  unsigned count=0;
  unsigned timeout=TIMEOUT;
  digitalWrite(trigger,HIGH);
  delay(1);
  digitalWrite(trigger,LOW);
  while (digitalRead(echo)==0 && timeout--);
  while (digitalRead(echo)==1 && count<MAXDIST) count++;
  return count;
}
The trigger variable shows where the board's input trigger is connected and the echo variable holds the output pin. The count variable will return a relative number (bigger is further away) or a maximum number if there is never any response.

That could hardly be simpler, but what about the USB part? That's simple too, thanks to the Leonardo library:

void loop() {
  static unsigned vold=0;
  unsigned v=distance();
  Serial.print(v);
  Serial.print(" ");
  Serial.println(vold);
  if (v<900 && vold>900)
  {
    // send an unlock
    Serial.println("Unlock");
#if 0
// For some reason the KDE screen locker won't
// wake up on a fake mouse event
    Mouse.begin();
    delay(100);
    Mouse.move(100,0,0);
    delay(100);
    Mouse.move(-100,0,0);
    Mouse.end();
#else
    Keyboard.begin();
    Keyboard.press(KEY_LEFT_SHIFT);
    delay(10);
    Keyboard.release(KEY_LEFT_SHIFT);
    Keyboard.end();
#endif
    digitalWrite(led,LOW);
  }
  if (v>900 && vold<900)
  {
    // lock screen
    Serial.println("Lock");
    Keyboard.begin();
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('l');
    delay(20);
    Keyboard.releaseAll();
    Keyboard.end();
    delay(5000); // don't try to unlock too fast
    digitalWrite(led,HIGH);
  }
  vold=v;
  delay(2000);
}

If you take out the serial statements I used for debugging and the #if 0 part of the code (this is the mouse code), this isn't much program at all. As I mentioned last time, I didn't use a wait on the serial port because that would require you to connect a serial terminal to the device for it to operate. The way I have it now, it runs and if there's no serial terminal connected, the debug output is simply lost.

The other output I used was the built-in LED on the Leonardo. It turns on when the board is waiting for an unlock event. There is a 5-second delay to prevent it from chattering as you leave (that is, trying to lock and then immediately unlock as you pass through the sensitive zone).

The only real variables of interest are v and vold. The v variable holds the current distance (yes, a terrible variable name, I should be ashamed, but this was a quickie). The previous cycle's distance is in vold (which, given that you have a variable named v, isn't a bad name at all).

When the code wants to lock, it sends a CTRL+ALT+L. The library has simple methods for sending just strings, but if you want to send things like this, you need to simulate the key presses and release:

    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('l');
    delay(20);
    Keyboard.releaseAll();

Overall, this isn't much of a chore considering how hard it is to normally write an HID device. Something this simple opens up a whole line of projects for custom keyboards and mice using sensors like accelerometers, cameras, etc. I could easily see hooking a Leonardo up to a "bigger computer" (like a Raspberry Pi or a Beagleboard) so that the bigger computer could do something like process video while the Leonardo handled the HID interface.

What will you do with a Leonardo? Drop me a note or share your ideas in the comments.

Related Reading






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video