# Connect an ATmega328 to a Raspberry Pi

I've been stuck on this problem for a couple of months now and haven't had much luck finding the exact information that I need. So, I'm currently working on a project to make a handheld raspberry pi, and utilizing small push buttons to communicate with the Pi as a USB device or via GPIO pins as a keyboard. I'm not too worried about the code to translate a signal to a keystroke, but I'm not entirely sure on how to actually connect an ATmega328 to the Pi. I've looked around online and see people using an entire Arduino board to do what I'd like, but it needs to be able to stay compact, so I plan to send the code to the ATmega328 and directly connect it within the circuit. I don't have much experience with building circuits that communicate with computers, so any help would be greatly appreciated.

Here's an idea of the kind of circuit that I'm looking for. I'm not entirely sure on the exact layout, but this is sort of a rough sketch to give an idea of what I'm trying to accomplish.

• I think it might help if you tried to explain more clearly what it is you are asking. Give some more detail about what you are trying to do. What are you having problems with? Why are you planning to use an ATmega328? Why can't the R-Pi alone do the job? Maybe read the Help Centre to get some more ideas about how to ask good questions. – gbulmer Aug 24 '14 at 19:41
• I intend to use an ATmega328 in order to take the inputs from push buttons and convert them into something that the Pi can decipher, such as a keystroke. I'd like to use the system as a handheld emulator and didn't want to rig something up with an old usb controller. My big question is what is a good method to use to connect push button signals to the Pi? I figured that the ATmega328 would work, as that it what I'd seen was used in the Arduino Uno, but I'm unsure of how to connect it to the Pi without using the entire Arduino board. – Atarinerd80 Aug 24 '14 at 19:51
• Isn't it easier to wire the buttons directly to the Pi? – venny Aug 24 '14 at 19:55
• I think you may be making some assumptions which aren't necessarily true. 1. The R-Pi can read pushbuttons, it does not need an ATmega to do that; wire the buttons directly to the R-Pi using the GPIO pins. This is not ideal, so maybe there is something you have read that causes you to want a different approach. 2. An ATmega328 will not look like a keyboard to the R-Pi, so a program running on the R-Pi see will not see keystrokes. 3. Ardiuno boards have three major pieces: the ATmega328, USB-to-serial, power supply, and then stuff like headers and LEDs. USB-to-serial could be replaced by I2C. – gbulmer Aug 24 '14 at 20:01
• Maybe draw a diagram of what you are trying to build, take a photo and post that on a photo-sharing site. Then everyone can get a common understanding. What is it you are trying to emulate? – gbulmer Aug 24 '14 at 20:05

There are several different parts to the project.

Reading pushbuttons on the R-Pi is doable. In some ways it is easier on an Arduino, (That sketch isn't how one would do it electrically, but that is straightforward to fix)

The problem is the emulators for the older games consoles. I have some experience supporting a team of students who wanted to run a games emulator on a R-Pi.

Each emulator will have some assumptions built-in about its games controllers. Some of the popular games console emulators already have controller emulators, which complete the hardware.

In some cases the games console emulator is straightforward to complete by building some hardware that emulates the specific games controller used by that console.

AFAICT you need to identify each game console. Then you either buy the controller hardware, or build the games controller to satisfy the console emulator, or you need to hack on the emulator source code.

IIRC the games controller the students needed to make was straightforward. Information about the controller was available on the web. The electronic interface between the RPi console emulator and the controller wasn't very complex. They built it with an Arduino, and it worked well enough for them to be satisfied.

Overview:
Exactly what you need to build for the controller mostly depends on the games console emulator you want to use. Until you decide on that, there is very little value in focusing on connecting buttons. The problem is to make a games controller which has the right electrical behaviour to work with the emulator.

You need to choose the games console emulator (or emulators) first. Then research the controllers. There is little benefit working on the pushbuttons before that; pushbuttons are relatively easy to wire up. The much harder part is building an interface between the games console emulator and the controller because you don't have free choice on how that works; the games emulator is already made to work in specific ways.

Summary: A few controllers might be built using an ATmega328, because it implements enough of the hardware interface. However, for other controllers it is the wrong place to start.

Recommendation: Pick the games console emulator with the games you most want to play, and research how it talks to its specific games controllers. Ideally find some instructions on how to build a working games controller for that emulator. Learn how it works by follow the instructions and build it. If it is far away from your goal, build it as quickly and cheaply as you can, and treat this as a learning project.

Once you have got something working, you will understand much more about the possibilities and constraints. So you will be better equipped to do what you want to do.

• Alright, that helps out a lot. I'm better in programming than I am in electronics. I plan to write my own program to implement several different emulators than can be started within said program. But thank you for the help. I'll just have to do a bit more research. There's just so much to it, it's hard to figure out where to start. – Atarinerd80 Aug 24 '14 at 22:06
• I think writing a games console emulator is quite a lot of work. Do you plan on using an existing processor emulator? One hard part is providing an adequate emulation of the game controller for the games (being run in the games console emulator). Figuring out which emulators are important (maybe priorities) and find out what each controller looks like to the game might be one place to start. Try to make the list of things to do as short as possible, put them in a good order to do them, break them into pieces which can be done in a day, and start doing the first thing. You'll learn by doing. – gbulmer Aug 24 '14 at 22:24
• Oh, no I just plan to use emulators that already exist, but just create a frontend with a nice GUI that will list the ROMs for each console. Once the game is chosen, then it will open the game within the specified emulator. But thanks for the advice. I think that it'll be a difficult project, but I'll be able to learn quite a bit from it. – Atarinerd80 Aug 24 '14 at 22:49

My vote is for directly attaching the buttons to the Raspberry Pi. Linux GPIO is extremely straightforward to access from userland (even a shell script can access GPIO!).

Having said that, if you want to use the buttons to control off-the-shelf programs (that probably expect input from a keyboard/mouse), you'll need to generate Linux event keypresses from GPIO events. Luckily, Linux has the gpio-keys driver. Something like this in your DTS file will do the trick:

gpio-keys {
compatible = "gpio-keys";

left-key {
label = "Left key";
gpios = <&gpio0 17 0>;
linux,code = <69>; /* KEY_LEFT */
};
}


You may have to configure pin muxing depending on the default state of the pins out of reset.