Raspberry Pi Pico - DIY Macro Keyboard

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's don here from novaspirit tech and welcome back to the channel and today we are going to be creating ourselves a little macro keyboard using the raspberry pi pico so let's get started [Music] now if you guys haven't seen my previous videos before on the raspberry pi pico i actually made a little playlist that you could check out and one of the things that i did mention on my first impressions video was to create a macro keyboard using the raspberry pi pico and we're finally at the stage where we could do this now if you've seen my previous videos on uh getting started on how i set up this computer using tawny as well as the micro python and also circuit python on my previous video with the auto clicking we now established a base on what we can use to create this macro keyboard so the stuff i'm going to be using today obviously is the raspberry pi pico which you can actually pick up from micro center vocally and i've also picked up a set of these little tactile switches also from micro center for 4 49 so yeah these things are pretty good now to jump into my desktop again i was telling you if you are interested in how i set this up just pop over to one of my previous videos on setting the tony up and you'll be at the environment that i'm at now i don't have to transfer anything over because we are using what we had before with the mouse auto clicker project which means i do have my adafruit circuit pi hid pre-installed and i also pulled this up because we are going to need to know the gpios so to begin we are going to need to code ourselves a little like hello world project for the button just so we know we could detect it and detect a button press we're going to start off with a very simple code which is import time import digital io and import board those are the three things we really need to know how we could detect the button now we do have to set this up so we're going to call this button one pin equals board dot gp 15. now for the gpios that's how i have this grid over here you could see it's gp0 gp1 and keeps going down and gp15 is on the bottom left i like to start on the corner so it's easier for me to determine and then on the right side you could also use these gpios as well so gp 16 gp 17 up to you so you could decide what gpio you want to use for me i'm just going to be using 15. next up we have to assign what the gpio pin is going to be so we're going to call it button equals digital io dot digital uh in out it is case sensitive so just bear that in mind and i am going to be this is going to be btn 1 underscore pin technically when you're coding this having all the variables up on top is easier to change around if you needed to but if you wanted to shorten up the code you could actually just put this in here and get rid of the variable all complete but if we're going to be playing around with this i'd rather keep a list of variables so i know that i could change button one to gpio 15 or 16 if i wanted to now let's place that back in and we assign the gpio2 digital in and out we're going to have to tell its input direction so we're going to do btn well let me put 1 here equals nope btn dot direction equals digital io dot direction dot input because that's what we're going to be doing when we press a button we're getting input not output like an led we're not powering something now here is the time where you actually have to make a decision on what you want the direction to be or what it's set at when you first start so it's called the pull so ptn1 poll equals digital io dot poll dot you can either set it up or down so in everything we do in coding circuit boards or stuff like this it has a zero and a one true or false on or off like your computer you know how that on switch has a zero and a one that's basically what we're doing do we want it to be in a true state or a false state when we are coding everything so if we put it into up that means that gpio will have a value of one which means it's true that means it's going to be outputting or actually have voltage going through that gpio now if i was to change this to down that means that pin is actually on a grounded state or a zero and we will have to send power to it to switch it from zero to one so i hope that makes sense and this also affects how we code something so i'm gonna kind of give you an example let's move on to our while loop or which is the main program so while true here is where we would actually define how this would work so if see since i have it down that means it's false right if btn1 dot value that means if it becomes true then perform this task and in this test it's going to be print button one pressed okay and then now we will want to sleep it so time dot sleep 0.1 so it doesn't go too fast it gives us a little bit of breathing room so we would set that time there so what happens is if we were to switch this to up the code over here would be not true okay so that means if not true because it's always going to be in a true state if you're up then do work but since we are going to use down i we don't need to declare that which means it's three less characters in your code again depending on what you're planning to do so now let's switch over to the board and i'll show you the board connection the first thing we need to worry about is the button that we're going to be using and as you can see there's actually pins here here here and here in an h pattern and the button itself is in is a gate you could say it will close and open the gate right now the gate is opened and when you push this button it will close the gate that means all the connections will connect and here you can apply ground or ground here and it's the same and here you can apply something else and it'll be the same but you can't plug in power here and ground here because it's going to connect so with that being said i am actually going to plug this into my gpio board like so and because we have this set it up as down and we have to send power to the gpio pin 15 looking at the pin out we know that the fifth pin one two three four five this one is power so we're gonna send this to this gp this pin right there and because we worked with gpio 15 we're going to put that there and we're going to connect it here or we could connect it here it will be fine either way that's that's going to work so now we have the connections going we could save our code and now hit the stop button real quick and then hit the play button so now if i was to press this button button one is pressed you see that also what's cool about this if you wanted to stick an led onto it you can actually just take an led ground it and i know one two and then the third one is ground and this one will be power and as you can see i have it like set up not pretty and if i was to set up the button it will actually also power the led this way we don't have additional coding you know because it's grounded right now and it will send power as soon as we hit the button anyway that's a story for another day now that we know that it's detecting our button presses and our code is working we can move on to creating this macro we were talking about back to your code as you may notice i only have this setup for one button later down in the future we could just copy and paste and we could set up as many buttons as we want but we mainly want to play around with one button first so we don't get ahead of ourselves so it's time to actually import the rest of our library so we're going to import usb hid import actually from ada fruit underscore hid keyboard import capital keyboard from ada fruit underscore hid key codes we want to import so import capital key code and now we could declare what our usb hid interface gonna be so let's do it under the variables we'll keep that up there and in here we're gonna do keyboard equals keyboard usb underscore hid dot devices okay so now we declare that and we're able to use our keyboard and now we could actually have it do something so let's make it do alt tab every time i press the key it's still gonna print button one so i know button one has been pressed and we're gonna go keyboard dot press key code dot alt and key code dot tab okay so with the comma it actually separates it into two different you can set it as many as you want well not as many as you want but you could actually set multiple key combinations so if i wanted to do alt shift tab i would actually put another comma and put key code dot shift in there and how do i know what this is head back over to the adafruit library and if you go into where is it hid right over here there's these python codes now in keyboard itself it actually gives you what you can do with it so we have press which is what we just did here and we also have release so we know that when we press it we still have to release it or it's going to stay on alt tab forever and there's release all and send and that's basically it so we have send release all release and press and because we're trying to do macro keys we want to press it and then release it not just type in a key now as far as key codes go pop it back out of there go into key codes and this will actually give you what you can do with the key code so if i wanted to type the letter a i would do keycode.a or if i wanted to do return enter or something like backslash you would type that in so it would be keycode.backslash or if you wanted like function keys like f1 f2 keycode.f1f2 so your keycode that is not mentioned here you might want to dig it up but you can actually modify this file if you need to for different key codes but yeah you even got mac codes like f13 f14 stuff like that so continuing with the code i would do time dot sleep and i would give it about one 0.1 so it gives it time to like actually press the key and then keyboard dot press oop sorry release now you could actually release all but that's not going to work in an environment where you're pressing multiple hotkeys or macro keys so you want to be able just to release the keys that you are pressing right now which is key code dot alt and key code dot tab this way it won't interfere with other buttons that you have valued at and once you are done with this it will go back into sleep and that should be it so with this little bit of code we should be able to alt tab and create our first macro key pressing that button so let's give that a try i'm going to save this stop it first then save it and then stop it again and then hit play so now if i press this it should alt tab here's a here's a good example there you go you see how it's going through and bringing the file of the folder back and forth because alt tab is actually happening at this point so now i got a macro key that essentially just alt tabs for me so if you're gonna use this for obs you probably want maybe something like shift you know shift then um key pad one let me see if that's correct let me see keypad one i would actually have to type out the word one so keypad one and same goes for here it'll be keypad 1 and it'll be shift over here and this will actually trigger the shift and keypad 1 button which you will have to memorize on your obs all right moving on let's how do we add more buttons so to add more buttons we would just do copy this code and remember spacing is very important in python i would just copy this code and change this to button two and then change whatever you want this to be so button two button two and i am not gonna do anything on button2 value same goes for here i will copy this command and paste it there is definitely a more elegant way to do this especially if you run it on a list or an array and then you can loop through the array but we're not about that life right now so you see i'm changing all the button once to button two and in here vtn to underscore pin and i would do board dot gp 14 which is the next button right up here so you see how it's 14 over here and there i would have button one and button two all set up and button one and button two so now i'm gonna try that as well i'm gonna pop over to the raspberry pi pico slap in a second button take this pin put it into gpio 14 and then i will still need to power this so i'm just going to piggyback off the first pin over here and there we have our setup two buttons with the two gpios and then it's both powered and let's save the code oh i got to stop it first save the code and i could press play and if i was to press the first button it'll alt tab well it's gonna do shift key code keypad one because that's how i programmed it and button two you see how it's now button two coming up and the more buttons you want you just need to keep copy and pasting and adding more to your code and adding more gpios you have up to what 28 buttons you could put in so you could do up to 28 gpios if you needed a big keyboard if you have something like this you could essentially set it up with four buttons five buttons however much you want and have your macro keys go from there okay with it all said and done this is how i currently have my setup with my raspberry pi pico right now and basically what this is is uh i got the mute key for my discord so i could just mute everything i got scroll up and scroll down on these buttons and then i also got volume up and volume down nothing dramatic is just something i'm playing around with but yeah you could program all your macro keys to do something like this basically and yeah you can add more two series whatever you want anyway that's it for me guys and if you guys really enjoyed this type of series where it's like code with me and i'm showing you what i'm doing as far as the coding wise let me know down in the comments below and if you guys got any other ideas for projects for the raspberry pi pico or even arduino or the esps let me know down in the comments below i've been having a lot of fun just coding this stuff and teaching you guys if you guys have any questions about this particular project let me know down in the comments below also if you guys are new to this channel consider subscribing also hitting that bell notification icon so you know when the next video is going to be out and as i say my nerd cave hack till it hearts
Info
Channel: Novaspirit Tech
Views: 43,188
Rating: undefined out of 5
Keywords: novaspirit, tech, pico micropython, raspberry pi pico, micropython, rshell, thonny, raspberry pi, pico, circuitpython, raspberry pi pico micropython, raspberry pi pico projects, pico projects, rpi pico, micropython for raspberry pi pico, micropython for raspberry pi, circuit python, python, usb hid, usb_hid, adafruit_circuitpython_hid, macro keyboard, macro keys, stream deck, diy macro keyboard, diy marco keys
Id: aEWptdD32iA
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Fri Feb 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.