WORK IN PROGRESS
Let’s talk about doing buttons, simple and cheap!
I will not be discussing the housing or mounting of the buttons since you can pretty much use whatever one fancies, I am talking about the software and electronic side of things.
This write up is based on a little project I made 2 years ago, it’s very simple, a bunch of shift registers that read the state of buttons about 20 times a second, the Raspberry PI then checks the state of the buttons with the previous state, if any state has changed it will fire off a command over the network to a UDP port that DCS-BIOS is listening to. DCS-BIOS then sets the state of a switch in the simulator.
Here’s a simple overview of flow of a button press.
The trickiest thing of this whole project are the shift registers. For this project lets presume there are 2 types. the 165, and the 595. The 165 is a PISO(Parallel In - Serial Out) register, and the 595 is the exact opposite, a SIPO(Serial In - Parallel Out) register.
When we want to trigger something with a electrical current we use the 595, when we want to read the electrical state of something we use the 165. Now why would we use shift register when we can use directly put the switch on a port on the microcontroller? It’s rather simple really, when you have 120 switches you want to wire up, well then you’d need 5 or 6 microcontrollers, which becomes expensive rather quickly, besides do you plan on giving them all their own USB port?
The shift registers I am talking about here are 8 bit, that means they have 8 pins that can be used for input(165) or output(595). They still require 3 pins though, which is not very impressive at all. And this is why they are developed with a really neat feature. They have a pin dedicated to serial input/output. Let’s take the 165 for example, I would normally put one of the 3 pins on high, then low again. This would tell the chip to read the 8 input pins. Now I have read the electrical state of the input pins(either HIGH or LOW, there’s 5V or 0V on them). To get them to my chip I need to cycle through them one by one. so I tell my microcontroller to start listening on the serial wire, then I grab my third and final wire and pulse that. With every pulse a bit is read from the register(those 8 inputs we grabbed) and send to the micro, I time the pulses in such a way that I can read them and store them.
[NICE GRAPHIC I NEED TO MAKE]
Even the Sparkfun tutorial seems a bit dense if you are not familiar with them unfortunately. I’ve got work coming up in a few hours so I don’t have time right now to make a neat graphic but I’ll do that later this week.
So, that’s how the 3 lines work. If you would want to double the shift register then I’ve got the solution for you! The Clock(timing) and the Read(those 8 pin states) lines need to be extended to the next chip, all you need to do then is connect 2 extra pins that are on the chip with eachother. These are like overflow pins. What happens is when you need a bit it will empty that spot in the register and move everything up a position. That means that if you read bit 1, bit 8 shifts to position 7, 7 goes to 6, and so on. As soon as this happens the state on one of those extra pins is read, loading in a new bit in position 8. This bit comes from the next shift register! You can just keep on daisy chaining them, with the only penalty being time, since adding an extra chip suddenly has you reading 16 bits!
Let’s have a look at how this work in practice! You can see the four 165(input registers) happily sitting in their sockets. Note all the resistors I added? Yeah I didn’t need to do that. These have internal pull-up resistors anway… Would have save me some soldering! The header pins are the inputs, they are located directly next to the input pins on on the chips, I couldn’t be bothered with making a breakout place for them. At the top of the image you see another header bank, this is the ground. I opted to have all the buttons pull a input LOW, or making a “short”. The pins always read 5V unless a button is pressed, on one side the button is connected to the pin, and on the other side to the ground.
The black wires are the ground lines, these chips usually have a few pins you connect to ground so I made a single ground line that runs underneath all the chips for ease of use. The red lines are the power lines, providing a steady 5 volt supplied by the microcontroller.
I hope by now you’ve noticed a few things, the white, green, blue and singular orange wire.
If you paid attention then you could deduce that the orange wire is the serial data line that leads to the microcontroller, it only connects to a single 165
The white one and the green one go to all the microcontrollers, so one might conclude that they ought to be for timing and reading purposes.
This leaves us with the blue lines, that happily run from chip to chip with not much else going on, these ought to be considered the overflow lines.
If you had of all those guesses correct then well done!
Okay so this is still a bit of a work in progress, I figured this would be a 5 minute write up… I suppose not.
Bill Of Materials(BOM): - grabbed all data from ebay, not the cheapest source
20pcs 595 Shift Register = $1.51 - This one is used for setting outputs, LED’s usually.
10pcs 165 Shift Register = $1.88 - This one is used for reading switches
10pcs protoboard 7x5cm = $7.35 - Solder the wire and chips onto this!
50pcs DIP 16 socket = $2.20 - I’m a sucker for these sockets. you can just replace the chip if they are broken. not required.
Arduino/RaspberryPi/Teensy = $5 to $10, this could really be whatever you have at hand.
Wire and solder are standard consumables so I won’t count them for obvious reasons.