Custom USB games controllers with Raspberry Pi Pico | HackSpace 42

via Raspberry Pi

Games controllers – like keyboards – are very personal things. What works for one person may not work for another. Why, then, should we all use almost identical off-the-shelf controllers? In the latest issue of HackSpace magazine, we take a look at how to use Raspberry Pi Pico to create a controller that’s just right for you.

home made retro gaming joystick box
Gaming like it’s 1989

We’ll use CircuitPython for this as it has excellent support for USB interfaces. The sort of USB devices that we interact with are called human interface devices (HIDs), and there are standard protocols for common HIDs, including keyboards and mice. This is why, for example, you can plug almost any USB keyboard into almost any computer and it will just work, with no need to install drivers.

We’ll be using the Keyboard type, as that works best with the sorts of games that this author likes to play, but you can use exactly the same technique to simulate a mouse or a gamepad.

Before we get onto this, though, let’s take a look at the buttons and how to wire them up.

We’re going to use eight buttons: four for direction, and four as additional ‘action’ buttons. We’ll connect these between an I/O pin and ground. You can use any I/O pin you like. We’re going to use slightly different ones in two different setups, just because they made sense with the physical layout of the hardware. Let’s take a look at the hardware we’re using. Remember, this is just the hardware we want to use. The whole idea of this is to create a setup that’s right for you, so there’s no need to use the same. Think about how you want to interact with your games and take a look at the available input devices and build what you want.

The connectors should just push onto the buttons and joysticks
The connectors should just push onto the buttons and joystick

The first setup we’re creating is an Arcade box. This author would really like an arcade machine in his house. However, space limitations mean that this isn’t going to be possible in the near future. The first setup, then, is an attempt to recreate the control setup of an arcade machine, but use it to play games on a laptop rather than a full-sized cabinet.

Arcade controls are quite standard, and you can get them from a range of sources. We used one of Pimoroni’s Arcade Parts sets, which includes a joystick and ten buttons (we only used four of these). The important thing about the joystick you pick is that it’s a button-based joystick and not an analogue one (sometimes called a dual-axis joystick), as the latter won’t work with a keyboard interface. If you want to use an analogue joystick, you’ll need to switch the code around to use a mouse or gamepad as an input device.

You can solder the pin headers straight onto Pico
You can solder the pin headers straight onto Pico

As well as the electronics, you’ll need some way of mounting them. We used a wooden craft box. These are available for about £10 from a range of online or bricks and mortar stores. You can use anything that is strong enough to hold the components.

The second setup we’re using is a much simpler button-based system on breadboard-compatible tactile buttons and protoboard. It’s smaller, cheaper, and quicker to put together. The protoboard holds everything together, so there’s nothing extra to add unless you want to. You can personalise it by selecting different-sized buttons, changing the layout, or building a larger chassis around this.

Insert coin to continue

Let’s take a look at the arcade setup first. The joystick has five pins. One is a common ground and the others are up, down, left, and right. When you push the joystick up, a switch closes, linking ground to the up pin. On our joystick the outermost pin is ground, but it’s worth checking on your joystick which pin is which by using a multimeter. Select continuity mode and, if you push the joystick up, you should find a continuous connection between the up pin and ground. A bit of experimentation should confirm which pin is which.

In order to read the pins, we just need to connect the directional output from the joystick to an I/O pin on Pico. We can use one of Pico’s internal pull-up resistors to pull the pin high when the button isn’t pressed. Then, when the button is pressed, it will connect to ground and read low. The joystick should come with a cable that slots onto the joystick. This should have five outputs, and this conveniently slots into the I/O outputs of Pico with a ground on one end.

You can solder the pin headers straight onto Pico
You can solder the pin headers straight onto Pico

The buttons, similarly, just need to be connected between ground and an I/O pin. These came with cables that pushed onto the button and plugged into adjacent pins. Since Pico has eight grounds available, there are enough that each button can have its own ground, and you don’t have to mess around joining cables together.

Once all the cables are soldered together, it’s just a case of building the chassis. For this, you need five large holes (one for the joystick and four for the buttons). We didn’t have an appropriately sized drill bit and, given how soft the wood on these boxes is, a large drill bit may have split the wood anyway. Instead, we drilled a 20 mm hole and then used a rotary tool with sanding attachment to enlarge the hole until it was the right size. You have to go quite easy with both the drill and the sanding tool to avoid  turning everything into shards of broken wood. Four small holes then allow bolts to keep the joystick in place (we used M5 bolts). The buttons just push into place.

With a combination of small sections of wire and jumpers, you can create whatever pattern of wiring you like on protoboard

With a combination of small sections of wire and jumpers, you can create whatever pattern of wiring you like on protoboard

The only remaining thing was a 12 mm hole for a micro USB cable to pass through to Pico. If you don’t have a 12 mm drill bit, two overlapping smaller holes may work if you’re careful.

The buttons just push-fit into place, and that’s everything ready to go.

A smaller approach

Our smaller option used protoboard over the back of Pico. Since we didn’t want to block the BOOTSEL button, we only soldered it over part of Pico. However, before soldering it on at all, we soldered the buttons in place.

Tactile switches typically have four connections. Well, really they have two connections, but each connection has two tabs that fit into the protoboard. This means that you have to orientate them correctly. Again, your multimeter’s continuity function will confirm which pins are connected and which are switched.

Protoboard is a PCB that contains lots and lots of holes and nothing else. You solder your components into the holes and then you have to create connections between them.

We placed the buttons in the protoboard in positions we liked before worrying about the wiring. First, we looked to connect one side of each switch to ground. To minimise the wiring, we did this in two groups. We connected one side of each of the direction buttons together and then linked them to ground. Then we did the same to all the action buttons.

There are two ways of connecting things on protoboard. One is to use jumper wire. This works well if the points are more than a couple of holes apart. For holes that are next to each other, or very close, you can bridge them. On some protoboard (which doesn’t have a solder mask), you might simply be able to drag a blob of solder across with your soldering iron so that it joins both holes. On protoboard with solder mask, this doesn’t work quite so well, so you need to add a little strand of wire in a surface-mount position between the two points and solder it in. If you’ve got a pair of tweezers to hold the wire in place while you solder it, it will be much easier.

For longer connections, you’ll need to use jumper wire. Sometimes you’ll be able to poke it through the protoboard and use the leg to join. Other times you’ll have to surface-mount it. This all sounds a bit complicated, but while it can be a bit fiddly, it’s all fairly straightforward once you put solder to iron.

Program it up

Now that we’ve got the hardware ready, let’s code it up. You’ll first need to load CircuitPython onto your Pico. You can download the latest release from circuitpython.org. Press the BOOTSEL button as you plug Pico into your USB port, and then drag and drop the downloaded UF2 file onto the RP2 USB drive that should appear.

We’ll use Mu to program Pico. If you’ve not used CircuitPython before, it’s probably worth having a quick look through the ’getting started’ guide.

The code to run our games controller is:

import board
import digitalio
import gamepad
import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)

keycodes = [Keycode.UP_ARROW, Keycode.DOWN_ARROW, Keycode.LEFT_ARROW, Keycode.RIGHT_ARROW,                   Keycode.X, Keycode.Z, Keycode.SPACE, Keycode.ENTER]

pad = gamepad.GamePad(
    digitalio.DigitalInOut(board.GP12),
    digitalio.DigitalInOut(board.GP14),
    digitalio.DigitalInOut(board.GP9),
    digitalio.DigitalInOut(board.GP15),
    digitalio.DigitalInOut(board.GP16),
    digitalio.DigitalInOut(board.GP17),
    digitalio.DigitalInOut(board.GP18),
    digitalio.DigitalInOut(board.GP20),
)
last_pressed = 0
while True:
    this_pressed = pad.get_pressed()
    if (this_pressed != last_pressed):
        for i in range(8):
            if (this_pressed & 1<<i) and not (last_pressed & 1<<i):
                kbd.press(keycodes[i])
            if (last_pressed & 1<<i) and not (this_pressed & 1<<i):
                kbd.release(keycodes[i])
        last_pressed = this_pressed
    time.sleep(0.01)

This uses the HID keyboard object (called kbd) to send key press and release events for different key codes depending on what buttons are pressed or released. We’ve used the gamepad module that is for keeping track of up to eight buttons. When you initialise it, it will automatically add pull-up resistors and set the I/O pins to input. Then, it will keep track of what buttons are pressed. When you call get_pressed(), it will return a byte of data where each digit corresponds to an I/O pin. So, the following number (in binary) means that the first and third buttons have been pressed: 00000101. This is a little confusing, because this is the opposite order to how the I/Os are passed when you initialise the GamePad object.

The while loop may look a little unusual as it’s not particularly common to use this sort of binary comparison in Python code, but in essence, it’s just looking at one bit at a time and seeing either: it’s now pressed but wasn’t last time the loop ran (in which case, it’s a new button press and we should send it to the computer), or it isn’t pressed this loop but was the previous loop (in which case, it’s newly released so we can call the release method).

The << operator shifts a value by a number of bits to the left. So, 1<<2 is 100, and 1<<3 is 1000. The & operator is bitwise and so it looks at a binary number and does a logical AND on each bit in turn. Since the right-hand side of the & is all zeros apart from one bit (at a different position depending on the value of i), the result will be dependent on whether the value of this_pressed or last_pressed is 1 or 0 at the position i. When you have an if condition that’s a number, it’s true if the number is anything other than 0. So, (this_pressed & 1<<2) will evaluate to true if there’s a 1 at position 2 in the binary form of this_pressed.  In our case, that means if the joystick is pushed left.

You can grab this code from the following link – hsmag.cc/USBKeyboard. Obviously, you will need to update the GPIO values to the correct ones for your setup when you initialise GamePad.

We’ve taken a look at two ways to build a gamepad, but it’s up to you how you want to design yours.   

Issue 42 of HackSpace magazine is on sale NOW!

hackspace issue 42 cover

Each month, HackSpace magazine brings you the best projects, tips, tricks and tutorials from the makersphere. You can get it from the Raspberry Pi Press online store or your local newsagents. As always, every issue is free to download from the HackSpace magazine website.

The post Custom USB games controllers with Raspberry Pi Pico | HackSpace 42 appeared first on Raspberry Pi.

Configuring the PATH System Variable

via SparkFun: Commerce Blog

A path is the name of a file's directory, which specifies a unique location in a file system. Whereas, the PATH system variable ($PATH), specifies a set of directories where executable programs are located. This allows software applications to access commonly executed programs.

New!

Configuring the PATH System Variable

March 23, 2021

A simple guide for configuring the PATH variable on Win 10, Mac OSX, and Linux systems.

There are different methods for modifying the ($)PATH variable on various operating systems. The directions in our new tutorial are based on the most common methods for each operating system. For more information, just use your favorite search engine with the keywords: path system variable along with <the name of the OS you are working on>.

comments | comment feed

Bringing Snake to your coffee table

via Arduino Blog

Snake is a classic game — more a genre of games — that dates all the way back to 1976 with the release of the Blockade arcade game. Many consoles and devices have received variations of Snake, putting it in the same league as Tetris and block breakers. Now Ty and Gig Builds have used an Arduino to construct a giant coffee table version of the game.

One of the reasons that so many variations of Snake exist is because it runs well on very low-resolution screens. That also made it perfect for this project, since an LED matrix makes for a great low-res display. Ty and Gig originally built that matrix for an interactive coffee table project and they were able to repurpose it for this game.

WS2811 individually-addressable RGB LEDs form that matrix. They shine through a holes drilled into a sheet of plywood mounted underneath the coffee table glass. An Arduino Mega controls those LEDs, but any Arduino board would work for the job. The only other hardware components are a 5V power supply and an analog joystick module.

The joystick is comprised of two potentiometers, so the Arduino simply checks those to determine which direction to move. Programming was a small challenge, because the 1D array of LEDs had to be translated to the 2D display. With that figured out, programming the game was straightforward. Players direct the LED snake to collect as many apples as possible without colliding with itself, like they have done for the past four decades.

The post Bringing Snake to your coffee table appeared first on Arduino Blog.

Raspberry Pi touchscreen music streamer

via Raspberry Pi

If you liked the look of yesterday’s Raspberry Pi Roon Endpoint Music Streamer but thought: “Hey, you know what would be great? If it had a touchscreen,” then look no further. Home Theater Fanatics has built something using the same RoPieee software, but with the added feature of a screen, for those who need one.

Subscribe to Home Theater Fanatics on YouTube for more great builds like this one

The build cost for this is a little higher than the $150 estimate to recreate yesterday’s project, given the inclusion of a fancier Digital Audio Decoder and the touchscreen itself.

Hardware

connecting raspberry pi to touchscreen
It really is a super user-friendly walkthrough video

The brilliant Home Theater Fanatics show you how to put all of this together from this point in the build video, before moving on to the software install. They take care to go through all of the basics of the hardware in case you’re not familiar with things like ribbon cables or fans. It’s a really nice bird’s-eye view walkthrough, so beginners aren’t likely to have any problems following along.

ribbon attaching to raspberry pi
See – close-ups of how to connect your ribbon cables and everything

Software

Same as yesterday’s build:

At this point in the build video, Home Theater Fanatics go through the three steps you need to take to get the RoPieee and Roon software sorted out, then connect the DAC. Again, it’s a really clear, comprehensive on-screen walkthrough that beginners can be comfortable with.

Why do I need a touchscreen music streamer?

touchscreen music player
Get all your album track info right in your face

Aside from being able to see the attributed artwork for the music you’re currently listening to, this touchscreen solution provides easy song switching during home workouts. It’s also a much snazzier-looking tabletop alternative to a plugged-in phone spouting a Spotify playlist.

The post Raspberry Pi touchscreen music streamer appeared first on Raspberry Pi.

Reverse-engineering a vintage comparator chip

via Dangerous Prototypes

Reverse-engineering a vintage comparator chip:

So what is this chip? Maybe it’s simply four comparators, but they could have a specific purpose. The chip could be a converter for four differential input signals, e.g. DCS (Differential Current Switch) logic. Another chip in the family seems to be Emitter-Coupled Logic, so this chip could be four ECL inverters (but it doesn’t make sense to have four pins for the reference voltage). It’s a bit puzzling that two comparators have inverted and noninverted outputs, while two have single outputs.

See the full post on Ken Shirriff’s blog.

This Arduino-controlled ball launcher lets your pup play fetch for hours

via Arduino Blog

Many dog breeds require a lot of exercise and mental stimulation to be healthy and happy, but not many of us want to spend our time endlessly throwing a tennis ball. That’s why Connor Benson used an Arduino Nano to build an automatic ball launcher that is capable of keeping his pup entertained all day long.

In this case, the Nano is being employed to sense when a ball has been dropped into the ball launcher, spin up the launcher’s motors, and then release the ball down into the spinning wheels. The board requires very little power, so this machine can run on a battery pack for a relatively long time.

The frame and mechanical components are 3D-printed in a nice bright blue color. An Adafruit proximity sensor is implemented to detect when a ball is in the machine and then releases the ball with a hobby servo motor. The 3D-printed wheels that grip the ball and fling it out are spun using powerful brushless DC motors. The Nano controls those via electronic speed controllers (ESCs).

Now Benson can play fetch with his dog for hours at a time without tiring out his arm and the dog certainly seems happy with the project.

The post This Arduino-controlled ball launcher lets your pup play fetch for hours appeared first on Arduino Blog.