How To Build Your Own Arduino-Powered USB Button
How to make a DIY Arduino powered USB button, for people new to electronics and code
Hello! Today I'll show you how to wire and program your own USB powered button. You will be able to send a command to your computer with this button, and be able to have the button light up with a built in LED. This tutorial is for people who are new to electronics, don't have much experience with coding, and/or just want a big red custom button for whatever reason. This tutorial is based off of two other tutorials you can see here and here. These tutorials are older and presume some level of familiarity with electronics tinkering and/or code. My goal is to make a tutorial for people trying a project like this for the first time. With all that out of the way, lets get started.
A quick overview of what we'll be doing. First, I'll go over all the parts you'll need. Then we'll set up the Teensy board, the brains of the operation, and write code to control the logic for the button. After that, we'll connect all the wires and test what we've got. Then, we'll solder everything together to permanently connect the wires. Finally, I'll show you how to get the last bit working.
First off, you're going to need a few things. The following links are places you can purchase these supplies.
The following are necessary if you're going to solder the wires to your Teensy board/button. Honestly I recommend it, it isn't too difficult even for a beginner. If you're scared you could just wire everything together and get silly with the electrical tape, it's up to you.
You can find a breadboard, pins, and jumper wires in kits, I've gotten this one before on Amazon.
Ok, now that we've got everything together we can get started on the software side. First, plug in the Teensy board into your computer. If you're using a different kind of Arduino board, skip down to the Arduino IDE step.
To get the the Teensy set up, follow along with the instructions on PJRC's website. Step 2 is to download the Teensy Loader Application, there are options for PC, Mac, and Linux (I'm on PC). The Teensy Load Application is a single file .exe, so you can throw it into any folder.
You can also download the "LED Blink, Both Slow & Fast" but it's just the default application preloaded on the Teensy. The main file is a .ino file type, which is used in Arduino software. You can continue following the instructions for the Teensy Loader, but we're going to skip ahead to downloading the Arduino software in this step. Once you've downloaded the software, continue with the above tutorial to the bottom of the page.
The Arduino IDE (integrated development environment) is what you'll use to write the code and deploy it to the Teensy. Once it's downloaded, go ahead and install it. Then, continue following the above tutorial to the point where you have installed the example "Blink.ino" to the Teensy.
Now it's time to write some code. We'll start with the Blink example, you can find it by going to File->Examples->Basic->Blink
This is the code that the Teensy board comes with by default. The language is called Arduino, and is based off of C++. Lets take a look.
void setup()
? // initialize digital pin LED_BUILTIN as an output.
? pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
? digitalWrite(LED_BUILTIN, HIGH); ?// turn the LED on (HIGH is the voltage level)
? delay(1000); ? ? ? ? ? ? ? ? ? ? ?// wait for a second
? digitalWrite(LED_BUILTIN, LOW); ? // turn the LED off by making the voltage LOW
? delay(1000); ? ? ? ? ? ? ? ? ? ? ?// wait for a second
}{
Here we can see a basic script for the Teensy board (or any Arduino board that has LED_BUILTIN). This will set the built-in LED to HIGH (on) and then it will wait a second before turning the LED to LOW (off), and repeat. I recommend altering one of the delay(1000) to another number like delay(5000), just so you can confirm it has changed. Keep in mind, the delay is in milliseconds, so 1000 is 1 second. Once you've edited the code a little, test it out by first selecting your Teensy board in the dropdown menu at the top, then clicking the right facing arrow "Upload" button to both verify your code and upload it to the Teensy board.
Your board should now start blinking at whatever rate you set. Now that you can reliably upload a new program to your board, lets take a closer look at the Teensy 4.0 board and learn a bit about it.
The above shows the map of the Teensy 4.0 board, and I have added red numbers to highlight specific ports we will use. I want to go over it in regards to the following code so that, if you're using a different board, hopefully you can piece together enough useful information to adapt it for your specific board.
The red number 1 shows a ground pin. We complete the circuit for our connections by using one wire to go to the bottom, and then another wire coming back to ground. There are actually three grounds available on this particular board, but I selected this one so that we can use a row of 7 breakout pins. I decided on a 7 pin row, started from the ground, due to the fact that it leaves a 2 pin gap in between the 3 pins pins we are actually using. This is handy when it comes to soldering the breakout pins to the board, especially if you aren't super confident in your solder skills (like I wasn't). The pins labeled 2 and 3 are for sending/receiving signals from the button. One last pin to take note of is number 13 on the right hand side, that is for the LED which we can use for testing purposes. We can also call this port with the name "LED_BUILTIN" like we did in the "Blink.ino" example.
Now that we're a bit more familiar with the board, lets write some custom code and wire something together. We'll start with some new code; in the Arduino editor make a new file (the Arduino IDE calls it a "sketch" because they're very unique and cool), I named mine "testButton.ino". Lets dive in:
领英推荐
// Include necessary libraries for keyboard emulation and debouncing
#include <Keyboard.h>
#include <Bounce.h>
// Define pin numbers for the red button and the LED
const int redButtonPin = 22;
const int ledPin = 13;
// Create a Bounce object to handle button debouncing on redButtonPin
Bounce buttonTrue = Bounce(redButtonPin, 10);
void setup() {
? // Set the LED pin as an output to control an LED
? pinMode(ledPin, OUTPUT);
? // Set the red button pin as an input with a pull-up resistor
? pinMode(redButtonPin, INPUT_PULLUP);
? // Initialize the Keyboard library for emulating keyboard actions
? Keyboard.begin();
? // Turn on the LED initially (HIGH state)
? digitalWrite(ledPin, HIGH);
}
void loop() {
? // Update the button debouncer
? buttonTrue.update();
? // Check if the button has a falling edge (transition from HIGH to LOW)
? if (buttonTrue.fallingEdge()) {
? ? // Emulate a key press for the '+' key
? ? Keyboard.write('+');
? ? // Turn off the LED (LOW state)
? ? digitalWrite(ledPin, LOW);
? }
? // Check if the button has a rising edge (transition from LOW to HIGH)
? if (buttonTrue.risingEdge()) {
? ? // Emulate a key press for the '-' key
? ? Keyboard.write('-');
? ? // Turn on the LED (HIGH state)
? ? digitalWrite(ledPin, HIGH);
? }
}
This code does two main things. First, it lights of the Teensy's LED. When the button is pressed down, it prints a "+" if you are currently in a text box somewhere on your computer, also it turns off the LED. When the button is let go, it prints a "-" to a selected text box, and also the Teensy's LED is lit up again.
We can go ahead and send this code to the Teensy. But before we do, we need to make a quick change to the USB type so that the Teensy can interact with the computer correctly. Go to Tools-> go to the bottom to USB:Serial, then change it from Serial to Serial + Keyboard + Mouse + Joystick.
When you upload this to the Teensy, the LED should turn on. Great! Bad for your retinas but good for the rest of you. Now let's wire it up. Refer to the following image to see where you wires should go. Remember, the wire for the button needs to go to pin 22 on the Teensy board because that's what we defined in our code, if you're using a different board then you'll need to either change the pin in the code or change where you plug in the wire.
I used red and green wires for this. Typically red/black are for "hot" wires while green or bare wires are for the ground. If you've done it all right, and correctly uploaded your code to the Teensy, you should be able to click your button and get a result. Notice how the LED turns off while the button is clicked, and back on when the button is released.
Pro-tip: you may get multiple responses back for a single click, like multiple +-+-+- in this case. The most likely culprit is a poor connection between your wires and the Teensy board or the button microswitch. Press them down to form a better connection. Once these are soldered together the issue will stop.
Now we've got a half decent button. Quick, make a few posts showing it off. Once you're done, we're going to switch from using the LED in the board to using the LED in the button. To do this we need to make a change to our code, but not all that different from what we've got now. Go ahead and open up a new sketch, I call this one "finalButton.ino"
// Include necessary libraries for keyboard emulation and debouncing
#include <Keyboard.h>
#include <Bounce.h>
// Define pin numbers for the red button and the new LED
const int redButtonPin = 22;
const int buttonLedPin = 19;
// Create a Bounce object to handle button debouncing on redButtonPin
Bounce buttonTrue = Bounce(redButtonPin, 10);
void setup() {
? // Set the new LED pin as an output to control the new LED
? pinMode(buttonLedPin, OUTPUT);
? // Set the red button pin as an input with a pull-up resistor
? pinMode(redButtonPin, INPUT_PULLUP);
? // Initialize the Keyboard library for emulating keyboard actions
? Keyboard.begin();
? // Turn on the new LED initially (HIGH state)
? digitalWrite(buttonLedPin, HIGH);
}
void loop() {
? // Update the button debouncer
? buttonTrue.update();
? // Check if the button has a falling edge (transition from HIGH to LOW)
? if (buttonTrue.fallingEdge()) {
? ? Keyboard.print("Hello world");
? ? //note, we have changed from Keyboard.write to Keyboard.print.
? ? //write only allows for single characters while print allows for more
? }
? // Check if the button has a rising edge (transition from LOW to HIGH)
? if (buttonTrue.risingEdge()) {
? ? Keyboard.print("! ");
? }
? // Check the state of the button and control the new LED accordingly
? if (buttonTrue.read() == LOW) {
? ? // Button is held down, turn off the new LED (LOW state)
? ? digitalWrite(buttonLedPin, LOW);
? } else {
? ? // Button is not held down, turn on the new LED (HIGH state)
? ? digitalWrite(buttonLedPin, HIGH);
? }
}>
There's only a few differences in this code. Instead of using pin 13 for the built in LED, we'll be using pin 19 to access the LED of the button. Also we need to change the Keyboard.write() method (because it only allows for single characters) and change it to Keyboard.print() (which allows for multiple characters). Here's what the button will do: First it lights up it's own LED. When the button is pressed, the LED turns off and the keyboard prints "Hello world". When the button is released, the LED turns back on and the keyboard prints "! ".
At this point, this is what the Teensy board and the button's microswitch should look like.
In this photo I show the bare wires so you can see where everything goes, but in reality I used electrical tape to hold all of this together. I highly recommend using electrical tape to keep yourself from going crazy trying to hold the wires. Also, as you can see in the photo, I held the ground wire for the button LED to the ground wire for the button microswitch wire. If you're using a Teensy 4 or more, however, you could also put this or any additional ground wires into any other ground pin.
Here's a gif showing the completed code and button wiring in action.
Now it's time to solder it all together. Again, you should attempt some soldering beforehand just so you know what to expect. We're going to first solder a row of 7 breakout pins to the bottom side of the Teensy board, on the ground, 22 and 19 pins. Notice in the following gif the extra row of breakout pins, those are to make the board a bit flatter to make soldering a bit easier. Also I have the pins we're soldering in the breadboard so that they stay in the same place while soldering.
Next, work on the button. Solder the male ends of the male-to-female jumper wires onto the button microswitch connection followed by the LED connection as demonstrated in the image below. Then, you can easily attach the female end of these wires to the previously soldered breakout pins on the Teensy board.
Finally, take a male-to-male jumper wire for the ground and cut it in half. Then take either a male-to-female or female-to-female wire and cut it in half as well, you want to keep the female end. Strip the ends of each end wire and a female end wire. Then, twist the three ends together and solder them.
Solder the male ends of ground wires to the button connector and LED connector. Finally, plug the female end of the combined soldered ground wire into your board.
Congratulations! You made a button. Go ahead and fit the microswitch/LED back into the button and enjoy.
Further Developments:
You have about a million things you can do with your new button. You could make a nice box to hide all of the wires, you could dive into the Arduino code and add new functionality, or you could learn how serial ports work to send a signal from some other program to your button to light up the LED.
I made this tutorial because I thought this was a great project for a beginner, but all other tutorials for this kind of project assumed some knowledge already. If you have any problems with this, please let me know and I'll help as best as I can.
Good luck and happy coding/soldering!