First: What is a simple solution for turning the Raspberry Pi on and off with a power switch in a smarter way than cutting the power supply and risk data loss?
Second: What is a simple way for connecting arbitrary arcade controllers to the RPi? In the following we present the ControlBlock, a Raspberry Pi (TM) Add-On that solves both problems.
[box type=”info” align=”aligncenter” class=”” width=”100%”]While this is the original article about the ControlBlock you can find an updated version about the ControlBlock here.[/box]
There exist individual solutions for both use cases: You can get power switch solutions for the Raspberry Pi as well as input accessories that can be connected to the RPi via USB. However, I wanted to have a more compact and neatly integrated solution – something that provides a whole bunch of general purpose input and output pins together with a reliable and intuitive power switch solution.
- 1 Introduction
- 2 Functionalities
- 3 Dimensions and Interfaces
- 4 Dimensions
- 5 Hardware Interfaces
- 6 Assembly of the ControlBlock
- 7 Attaching a Power Button
- 8 Attaching a Status LED
- 9 How to attach Arcade Buttons, Joysticks, Gamepads
- 10 Software Setup and Configuration
- 11 Command-Line Installation
- 12 Using Arcade Controllers with the ControlBlock
- 13 Using (S)NES Controllers with the ControlBlock
- 14 Only one Gamepad
- 15 4-Player Extension
- 16 Power Switch Functionality
- 17 Video Demonstrations
- 18 Power Switch
- 19 Arcade Controllers
- 20 SNES Controllers
- 21 Conclusion
The ControlBlock is an add-on board for the Raspberry Pi (version 1, 2, and 3) models A+ and B+. It provides a microcontroller-based power-switch functionality as well as two General Purpose Input/Output (GPIO) Expanders with overall 32 pins so that you are able to connect quite anything you like to it.
Besides the hardware itself, a driver software is presented here, that allows for an easy installation and configuration of the ControlBlock functionalities, especially the connection of game controllers. Currently, the driver supports these game pad types:
- Arcade Controllers
Connect any controllers that are based on microswitches and the driver will map these either to two distinct game pads or to keyboard presses with MAME layout, which can be seen as the de facto standard in this field.
- SNES Controllers
The driver also supports the polling scheme of Super Nintendo Entertainment (SNES) controllers. Currently, the driver supports up to two SNES controllers that can be attached to the ControlBlock.
- NES Controllers
Since Nintendo Entertainment System (NES) controllers and SNES controllers are so similar in their hardware, the driver also supports up to two NES controllers.
The sources of the driver are publicly available at Github and can be further enhanced to support additional controllers in the future.
In the following, details are given for the individual features that are mentioned above. Functionalities, dimensions, and hardware interfaces are described first. Then, the assembly is described in more details. After that, the configuration and a demonstration video is presented. A conclusion is given at the end of this article.
The ControlBlock provides two main functionalities that can be used independently from each other:
- Power Switch
The Raspberry Pi comes without a power switch. As soon as you plug the micro USB cable into the RPi, it turns on. If you want to shutdown the RPi, you need to call a shutdown command to to bring the system into a state in which you can safely remove the USB cable again.
The ControlBlock provides a connector for a toggle switch to control the power supply of the RPi. Furthermore, it provides a connector for an LED to indicate whether the RPi is off, booting, running, or shutting down. The power supply is controlled by a tiny microcontroller that monitors the button state as well as the state of the RPi and switches a MOSFET accordingly. This means that there is no need anymore to plug and unplug the USB cable from the RPi if you want to completely turn your RPi off.
General Purpose Input/Output and Game Controllers
The 2×40 pins header of the Raspberry Pi already comes with a bunch of GPIO pins that can be used for all kinds of things to control and sense. However, these pins go directly into the main controller of the RPi and there is no further circuitry protection. Also, if you want to attach a set of arcade controllers, this task can become quite troublesome. The ControlBlock comes with two GPIO expanders MCP23017: Overall, you have 32 additional GPIO pins that can be controlled via the I2C interface of the RPi. The GPIOs can be accessed with terminal blocks that also provide cable protection. And if you accidentally create a short circuit you do not have to fear to fry your whole Raspberry with that then.
Also, if you want to connect your RPi to any kind of game controllers, you have to decide which GPIO pins to use and you must adapt your driver software such that exactly these pins are controlled. The ControlBlock makes the task of connecting game controllers to the Raspberry Pi very easy: The ControlBlock PCB is clearly marked with pin outs for players 1 and 2 and the provides driver software for the ControlBlock can easily be configured for different controller types.
Dimensions and Interfaces
The dimensions of the ControlBlock PCB are chosen such that it exactly matches the mounting holes of the Raspberry Pi models A+ and B+. The ControlBlock PCB comes with the same round corners as the Raspberry itself. Of course, these round corners provide no further functionalities, but give the whole setup a neat look. Regarding the GPIO usage the ControlBlock is attached to the first 2×6 pins of the Raspberry Pi GPIO header.
The ControlBlock does not fit the Raspberry Pi models A and B. The reasons for this are the composite out connector and the missing mounting holes of the Raspberry Pi models A and B.
The hardware interfaces of the ControlBlock are described in the following:
- 2×6 pin female header as connector to the Raspberry Pi
The ControlBlock PCB is attached to the Raspberry with a 2×6 pin female header. This header makes use of pins 1 to 12 of the J8 header. This header is used for connecting the voltage and ground pins as well as the I2C pins between the RPi and the ControlBlock. The PCB is prepared for breaking out all other pins of that header so that you can easily access all other pins that are not used by the ControlBlock.
Micro or B-Type: USB Connectors for the power supply
The first version of the ControlBlock had a USB mini connector. Because so many people asked for it the ControlBlock got a USB Micro connector now. That means you can simply use your existing USB Micro cable. The USB Micro connector comes by default with the ControlBlock. If you would like to use a bigger USB connector, you can optionally solder a USB-B socket to the ControlBlock! This should give you the flexibility that you need for your project.
Pin Outs for 5V Supply Voltage and Ground
If you do not want to use USB connector, GND and the 5V supply voltage can also be accessed via two pins so that you could use batteries or whatever you like.
Pin Out for a Toggle Switch
To control the power state of the Raspberry Pi the ControlBlock provides an interface for attaching a toggle switch. The on-board microcontroller monitors the state of that switch as well as the one of the Raspberry.
Pin Out for Status LED
The current power state can be indicated with a 5V status LED that can be attached to the two pins that are provided by the ControlBlock. These states can be “off”, “booting”, “on”, and “shutdown”. The different state are indicated with easy-to-distinguish static and pulsing patterns.
2×16 GPIO pins
Overall, the two GPIO expanders provide 32 GPIO pins for arbitrary usage. With revision 2.X of the Raspberry Pi, these pins can be accessed via the SPI interface of the Raspberry Pi. With revision 1.X of the ConterolBlock, these pins can be accessed via the I2C interface with the I2C addresses 0x20 and 0x27.
Alternatively, to manually access the pins you can use the provided ControlBlock driver software to easily connect and poll various game controllers. For this, the top silkscreen of the ControlBlock PCB is marked with pin outs for players 1 and 2.
In-Service Programmer (ISP) pin-out for ATTiny
The Power Switch logic is realized with the help of an Atmel ATtiny85 microcontroller. If you want to you have the possibility to access the microcontroller with the ISP header. In this way you could reprogram the microcontroller with whatever functionality you like.
GPIO pins used by the ControlBlock
The ControlBlock revision 2.X uses these GPIO pins of the 40-pin header of the Raspberry Pi: 3.3V (pin 1), 5V (pin 2), GPIO 17 (pin 11), GPIO 18 (pin 12), MOSI (pin 19), MISO (pin 21), SCLK (pin 23), CE0 (pin 24).
Assembly of the ControlBlock
- 1x 2-pin female terminal block
- 2x 2-pin male header
- 1x 2×6-pin female connector
- 4x 8-pin female terminal block
The assembly of the THT components is done in a few steps, which are described in detail in the following:
- Solder the 2-pin female terminal block on the top side (i.e., the side with the white marking print) of the ControlBlock. Take care for the correct orientation of the terminal block!
2. Solder the two 2-pin male headers for the button and the status LED. Note that you might to adapt this step according to the button and/or the LED that you want to use with the ControlBlock.
3. Solder the 2×6 female pin header on the bottom side of the ControlBlock (i.e., the side without the white marking print)
4. Solder the first 8-pin terminal block on the top side of the ControlBlock. Take care for the correct orientation of the terminal block! First, solder a single pin and check the resulting alignment of the terminal block. If the terminal block is placed well, continue with the other seven pins.
5. Solder the other three 8-pin terminal blocks in the same way as you did with the first 8-pin terminal block.
The fully assembled ControlBlock should look like this:
Attaching a Power Button
To turn the Raspberry Pi on and off with the ControlBlock you need to attach a toggle switch to the two button pins on the ControlBlock. Technically speaking, the microcontroller on the ControlBlock looks, if the two button pins are connected or not. If they are connected, a GPIO pin of the microcontroller on the ControlBlock is pulled to GND and interpreted accordingly.
- It is important that you use a toggle switch and not a momentary button with the ControlBlock. Otherwise the Raspberry Pi will be turned off again right after booting.
- If you do not want to use the power switch functionality you can disable this in the configuration file /etc/controlblockconfig.cfg by setting “powerswitch”: false.
- The power switch circuitry of the ControlBlock leads to a tiny voltage drop and we made the experience that a good quality power supply and a good quality USB cable are mandatory for a working setup. If unsure, we can recommend the official Raspberry Pi Power Supply.
Attaching a Status LED
The ControlBlock has pin outs for an optional status LED that indicates the power state of the Raspberry Pi. You can directly attach an LED to the pins that are marked with “LED”. You need to pay attention to the polarity of the LED: The LED pins are marked with “+” and “-” for that.
The LED will blink in four different patterns that depend on the power state of the Raspberry Pi:
- Off: The LED is simply off.
- Booting: The LED slowly fades in and out.
- On: The LED constantly stays on.
- Shutting down: The LED fades in and out twice as fast as during boot up.
How to attach Arcade Buttons, Joysticks, Gamepads
The cables from the arcade buttons, joysticks, as well as from the (S)NES gamepads are connected to the ControlBlock via the screw terminal blocks. You need a slotted screwdriver with a maximum width of 2.5 mm for that. Detailed instructions for the individual controller types are given further below.
Software Setup and Configuration
The GPIO ports of the ControlBlock can be accessed via the I2C interface of the Raspberry Pi. The addresses of the two GPIO expanders are 0x20 and 0x27.
The power switch functionality of the ControlBlock is controlled with the GPIO pins 17 and 18 of the Raspberry Pi:
- Pin 17 can be used to indicate the power state of the Raspberry Pi. This pin is read by the ControlBlock”s microcontroller to control the on-board power switch.
- Pin 18 can be read by the Raspberry Pi to get status of the button that is attached to the ControlBlock.
The ControlBlockService is an open-source driver for the ControlBlock. It provides a service for interacting with the power button signals as well as for mapping attached game controllers to corresponding game pad devices on the Raspberry Pi. The installation only takes a few steps and can either be done completely from command line or half-automatically with the help of the RetroPie-Setup Script. In the following, these two methods are described in more detail.
Before you start with the installation of the ControlBlock driver, make sure that the SPI-interface (in case of ControlBlock rev. 2.X) or the I2C-interface (in case of ControlBlock rev. 1.X) is enabled. You can do this via the tool raspi-config in the “Advanced Options” menu. Enable the interface and let the kernel module be loaded by default. Reboot your system to let the changes take effect.
The command-line installation consists of these steps:
- Installing required packages
- Fetching the latest version of the ControlBlock driver and service from the public repository.
- Compiling the driver binary.
- Installing the driver binary.
- Installing the service.
Here is a step-by-step guide:
- Eventually: Download and copy the RetroPie SD card image version 3.5 to the sd card
- Do not connect any other (USB) controller to the Raspberry for now to avoid any unnecessary interference
- Connect the power supply and the switch with the ControlBlock and switch on the Raspberry
- When EmulationStation has loaded and shows the controller configuration, press F4 to get to the command prompt.
- Alternatively: Connect to the Raspberry Pi via SSH to get to the command prompt
- Run „sudo raspi-config“, select „Advanced Options“, select „SPI“ and enable the SPI interface and the SPI kernel module on boot by default by selecting „Yes“ in the following two screens.
- Exit raspi-config by selecting finish and select „reboot“ when asked for it.
- Again, press F4 in EmulationStation to get to the command prompt or connect to your RPi via ssh
- Update the apt package cache: sudo apt-get update
- Install any APT updates: sudo apt-get upgrade -y
- Reboot (IMPORTANT, otherwise we might get a modprobe error) via “sudo reboot”
- Install necessary packages: sudo apt-get install -y cmake g++-4.9 doxygen
- Clone the ControlBlock driver repository:
git clone --recursive https://github.com/petrockblog/ControlBlockService2.gitIf you have revision 1.X of the ControlBlock use the command
git clone https://github.com/petrockblog/ControlBlockService.git
- Change into the driver folder: “cd ControlBlockService2” (or “cd ControlBlockService” for rev. 1.X)
- Build Makefile: mkdir build && cd build && cmake ..
- Compile the driver: make
- Install the driver: sudo make install
- Install the service so that the driver is loaded on boot: sudo make installservice
- You can now configure the ControlBlock service by editing its configuration file via „sudo nano /etc/controlblockconfig.cfg“.
- After changing the configuration, you can reload the service via „sudo service controlblockservice restart”
- You can test your attached controller(s) with the tool jstest, e.g., with „jstest /dev/input/js0“ (Exit with Ctrl-C)
This video demonstrates the above steps:
Using Arcade Controllers with the ControlBlock
The ControlBlock comes with silkscreen indicators that describe a default pin out for arcade controllers: You can connect two joysticks and 12 buttons for each of the two players:
The ControlBlock service can be configured in two different ways when using arcade controls:
As one option it can be configured such that it provides two game pad devices in the system, one for each player. To do so, the configuration in the file /etc/controlblockconfig.cfg must set “gamepadtype” to “arcade”:
"gamepadtype" : "arcade"
As another option the ControlBlock service can be configured such that it provides a virtual keyboard with the de-facto MAME standard. To do so, the configuration in the file /etc/controlblockconfig.cfg must set “gamepadtype” to “mame”:
"gamepadtype" : "mame"
The button mapping is shown in the following diagram:
Using (S)NES Controllers with the ControlBlock
The ControlBlock can also be used to poll NES and SNES controllers. Here is a diagram that describes the pin out for using NES and SNES controllers with the ControlBlock:
Here is a pin out diagram for NES and SNES connectors:
If you want to use a SNES case, here is a diagram that shows the pin out of the original SNES controller front:
In order to poll NES and SNES controllers the configuration in the file /etc/controlblockconfig.cfg must set “gamepadtype” to “snes”:
"gamepadtype" : "snes"
You can also connect a latching reset button to Player-2, Input B. If the button is pressed a virtual ESC-key press will be triggered.
Only one Gamepad
If you want to connect only one gamepad to the ControlBlock you can set the element onlyOneGamepad to true: It enables only one gamepad in the system (e.g., if only Player-1 buttons are wired to the ControlBlock in your setup, this prevents a ghost gamepad from being selected as default player 2 in retroarch)
The driver can handle up to two ControlBlocks. This means that you can stack two ControlBlock on top of each other to have inputs for four players. To do so make sure that you set different addresses for each of the ControlBlocks. You can set the address of each ControlBlock by setting the two solder jumpers of each ControlBlock accordingly. The values of the solder jumpers have to be set in the configuration file with the elements SJ1 and SJ2. Also, you have to enable the second ControlBlock by setting the element enabled for the second ControlBlock to true.
Power Switch Functionality
To enable or disable the power switch functionality you can set the element powerswitchOn to true or false:
- true: Activates the handling of the power switch signals of the ControlBlock.
- false: Deactivates the handling of the power switch signals of the ControlBlock.
The following video shows the power switch functionality of the ControlBlock:
The the following demonstration video shows arcade controllers for two players that are mounted on a simple wooden console. The bottom of it looks as following:
This video shows the controllers in action:
This video shows the ControlBlock with SNES controllers attached to it:
The ControlBlock is an extension board for the Raspberry Pi (TM). The two key features of the ControlBlock are
- a power switch functionality and
- general purpose input/output (GPIO) pins ready to use, e.g., for connecting arcade or SNES controllers.
The power switch functionality allows to turn on and off the power to the Raspberry Pi with a toggle switch. Therefore, a toggle switch can also be connected to the ControlBlock. There is also a connector for a status LED that indicates the power status of the Raspberry Pi.
The GPIO pins can be accessed from the Raspberry Pi via the I2C interface. The ControlBlock makes it very easy to connect arcade controllers for up to two players. The open-source driver for the ControlBlock can be configured for various controller types. It supports, e.g., arcade controllers that are mapped to game pad devices, as well as a mapping to a MAME keyboard configuration. The ControlBlock also supports, e.g., the polling of SNES and NES controllers so that it also provides the functionality of the RetroPie GPIO Adapter.
If you are interested in getting the ControlBlock for your project, it can be ordered at the shop.