Easy GPIO Hardware & Software

Do you intend 'basic' as 'least parts and as simple as possible' or 'minimal good implementation'?

1. This is okay, but low-side switching will provide more voltage to drive high-brightness LEDs and makes resistor calculation 'simpler' since the switching device won't contribute much voltage drop. You can make the circuit one component simpler by using a MOSFET instead and omitting the base resistor.

2. As mentioned you need to switch the low side here if you want to use 'N' devices. I think an opto is overkill for experimenting, but any dedicated I/O board could use them, and probably should in an education context.

3. Does the GPIO include pull-up/down resistors? 'Smooth' switching is generally not desirable on logic inputs, and can cause serious problems in some situations. Inputs should always traverse the Vil(max)->Vih(min) no-mans-land as quickly as possible. There are better - but more complicated - debouncing solutions available to solve this in hardware, but it's better dealt with in software IMO, so this circuit could simply be a switch from GPIO to Vcc/GND.

4. I would suggest a comparator and potentiometer to set the reference level instead. I know you want to avoid ICs, but I think (basic) comparator action is about as simple as it gets once you need to deal with the analogue world. Hysteresis is probably a good idea, and less easily explained, though...

Use 3V3 for the input circuits and 5V for the output circuits. Though socketed buffers are highly advised for those who don't know what they're doing

It is great learning this, and it is helping me understand things a lot better as I go (hopefully others too). Hopefully I can try to explain each part.

error404 said:

Do you intend 'basic' as 'least parts and as simple as possible' or 'minimal good implementation'?

It's a good question…and it comes down to keeping the concept a level well below what the Gertboard offers (which is an all you can eat buffet of goodness!). On the above balance points, it has to be sided on the 'least parts and as simple as possible' [add to that cheap as possible too] then if possible 'minimal good implementation'.

My thinking is, you may build a project using a R-Pi and develop it using the Gertboard, but it'll be nice to free up the Gertboard afterwards to develop your next project. Also, if you want to experiment with I/O without planning on doing anything too complicated.

For now, I'm thinking these circuits should be aimed at breadboard and stripboard designs, which keeps things low level and fully adaptable. This means, people can take the circuit designs, use additional suggested improvements (i.e to add 'minimal good implementation' aka buffer ICs) where they are happy to add the cost or understand enough to not need it. The idea is to create a guide for beginners, not to produce a product or board (although basic PCB designs could be made if required I guess – although I hope the circuits will be simple enough without).

- Circuits 1 [LED] and 2 [Motor Drive] -

Tomo2k said:

The motor drive would need to have the transistor between the motor and gnd, as drawn it won't work and may damage the RPi.– I would also use an opto-isolator to drive a MOSFET to protect the RPi from under/overvolt from motor start/stop. This also allows the motor to have a completely separate power supply at any (safe) voltage.

Oh yes, it would draw current from the base as well as collector, I suppose. As error404 also mentions, same will be best for the LED too.

- Circuit 3 [Switch] -

Minimal de-bounce or perhaps none at all (probably the easiest way is to test and try it out!), I agree to keep things simple the noise can be handled within the software. Will also ensure there are pull-ups.

- Circuit 4 [Analogue sensor to digital input] -

For most cases the simple circuit I think would be enough, perhaps we can create a more complex one later on using comparators (for specific applications). Perhaps most would find themselves better off using the Gertboard and it's ADC inputs for experimenting with, putting in software hysteresis (and any number of control methods they want to try).

- Buffering/Protection -

I think to resolve the buffer side of it, is to make it an optional addition to the circuit if required. The opto-isolating ICs make good sense, and to a beginner, very easy to explain their function, also they are something which once you have one you can reuse as you experiment.

However, would using those, allow for the lower powered transistors to be dropped anyway i.e for LED driving? Perhaps it would simplify some of the circuits anyway (since it is just designs can have both options).

I'll probably get hold of a LaunchPad board anyway, since its such a low price, it is worth having to play around with and test these things on (although will compare it's specs).

Again, thanks for the help, and keep the advice coming, I know I am not the only person who has the WILL but not necessarily the RIGHT knowledge to do the projects we have in mind.

- Circuit 4 [Analogue sensor to digital input] -For most cases the simple circuit I think would be enough, perhaps we can create a more complex one later on using comparators (for specific applications). Perhaps most would find themselves better off using the Gertboard and it"s ADC inputs for experimenting with, putting in software hysteresis (and any number of control methods they want to try).

That's fine, but sensors can't really be lumped together in this way, you should probably be more specific about each type of sensor you want to interface, and test out your circuits. For a potentially painful example, some sensors will be destroyed if Vcc is shorted to ground across them (variable resistor set to minimum), others will need amplification, etc. This circuit is also not appropriate for sensors that have a voltage output, and may damage them.

However, would using those, allow for the lower powered transistors to be dropped anyway i.e for LED driving? Perhaps it would simplify some of the circuits anyway (since it is just designs can have both options).

It will work. The 'standard' part that anyone should be able to get hold of easily anywhere is 4N2x (4N25, 4N28 etc.) family, but they are single-channel. They will not allow more current (the opposite actually), you'll certainly need a driver as well. The CTR spec will tell you the minimum ratio of input to output. Personally I don't think they're necessary here unless you're working with higher voltages that could fry the Pi board.

I"ll probably get hold of a LaunchPad board anyway, since its such a low price, it is worth having to play around with and test these things on (although will compare it"s specs).

Do! I've got several and they're fairly easy to use (though more complicated than AVR, say). I'm not sure it's the right thing to use to add I/O to an RPi, but they're handy, at least if you've done a bit of microcontroller programming before. If you haven't then starting with Arduino is probably better. They've also got a capacitance touch demo addon for it that comes with a much larger and more capable MSP430 and the cap touch board. Not quite as worth it, but a nifty toy.

----That's fine, but sensors can't really be lumped together in this way, you should probably be more specific about each type of sensor you want to interface, and test out your circuits. For a potentially painful example, some sensors will be destroyed if Vcc is shorted to ground across them (variable resistor set to minimum), others will need amplification, etc. This circuit is also not appropriate for sensors that have a voltage output, and may damage them.

Yeah that is a fair point, for this initial circuit I'm thinking basic resistive based sensors, such as light dependent resistors, thermistors, moisture etc. Things like wheatstone bridges, signal amplifiers and such like can be explored later. A separate circuit for say 0-5v output sensors, such as pressure/acceleration sensors would be a good idea though (I guess a comparator in this case would be a good option).

Is the following suitable?

----It will work. The 'standard' part that anyone should be able to get hold of easily anywhere is 4N2x (4N25, 4N28 etc.) family, but they are single-channel. They will not allow more current (the opposite actually), you'll certainly need a driver as well. The CTR spec will tell you the minimum ratio of input to output. Personally I don't think they're necessary here unless you're working with higher voltages that could fry the Pi board.

I think it's something which should be mentioned for use with the circuits, its something which can be used during testing etc if required, will save bad wiring cooking the pi at least. Chances are when you've soldered a circuit and tested/measured it, you could remove it and reuse for you next part.

I"ll probably get hold of a LaunchPad board anyway, since its such a low price, it is worth having to play around with and test these things on (although will compare it"s specs).

----Do! I've got several and they're fairly easy to use (though more complicated than AVR, say). I'm not sure it's the right thing to use to add I/O to an RPi, but they're handy, at least if you've done a bit of microcontroller programming before. If you haven't then starting with Arduino is probably better. They've also got a capacitance touch demo addon for it that comes with a much larger and more capable MSP430 and the cap touch board. Not quite as worth it, but a nifty toy.

With a few example programs to work from I should be ok with the programming side, so worth having a play with one to improve my hardware knowledge.

Out of interest, what makes it not suitable to add to the R-Pi, I was thinking you could interface via the SPI similar to PIC on the Gertboard (obviously lacking the built in protection etc). Thanks once again.

Yeah that is a fair point, for this initial circuit I'm thinking basic resistive based sensors, such as light dependent resistors, thermistors, moisture etc. Things like wheatstone bridges, signal amplifiers and such like can be explored later. A separate circuit for say 0-5v output sensors, such as pressure/acceleration sensors would be a good idea though (I guess a comparator in this case would be a good option).

You can probably just rearrange the pot from the sensor output to ground, wiper to your transistor, using the Vbe voltage as the threshold. Just make sure the pot value is high enough for the sensor to drive it.

With a few example programs to work from I should be ok with the programming side, so worth having a play with one to improve my hardware knowledge.

I was just thinking that it's a bit more complicated than some simpler microcontrollers and requires a bit more tricky setup, but there're lots of examples and some good documentation in the community so if you just want to mess around you'll be fine.

Out of interest, what makes it not suitable to add to the R-Pi, I was thinking you could interface via the SPI similar to PIC on the Gertboard (obviously lacking the built in protection etc). Thanks once again.

Well if all you want to add is GPIO, you can get SPI (or I2C) GPIO chips off the shelf such as MCP23S18, which gives 16 GPIO for < $2. LaunchPad isn't all that suitable because the largest chip that comes with it only provides 10 GPIO, and you'll need to use at least 3 of those for the serial interface. Plus write the code and a driver. It's just not that efficient when there are chips designed to do exactly that. OTOH, it does get you a basic ADC which could be useful.

First, a huge thank you to James and Gert! That is exactly the type of information we've been dying to know. Your responses are very much appreciated!

As meltwater said, I'm offering a PCB design/fabrication service at no charge (except the manufacturing and postage costs! ) for anyone who has circuit ideas but no way to get them built.

I'm also more than happy to "translate" breadboard photos, diagrams on (preferably unused) hankys, tattoos, etc, to PCBs.

Naturally, the critical issue here is protection of, and exposure of, the GPIO pins in the best possible way. So I'm thinking about a "standard" bit-bashed GPIO header that will allow anyone at all to add some LEDs, trannies (silicon trannies, not "hello sailor" trannies! ), switches, and maybe optos, with a minimum of fuss and maximal protection for the SOC device and the rest of the RasPi.

I do have some concerns with the use of opto isolators, from bitter experience.

We (that's the royal "we") need to bear in mind that these devices are painfully slow (in terms of the capabilities of the GPIO pins), so they probably won't ever be used near the maximum frequency that the GPIO pins are likely to be capable of. Unless, of course, the abstraction in the driver layer defines the maximum toggle frequency – maybe poor coding will be a benefit!

Another issue is that even with modern isolation technology (RF-RF instead of optical), optos (and by extension, RF-RF) will have a fair degree of manufacturing tolerance, and they ALL degrade with use. They degrade faster and further with ABuse. So unless you're driving a high-voltage (> 5V) or high-current load, optos would probably be your LAST port of call.

Most importantly, most optoisolators currently limit the use of an I/O pin to either input OR output, not both (at least, not without some additional circuitry). I know that's probably not a huge deal, but it's something that some constructors will trip over.

That said…There's no reason why a simple opto interface limited to speeds of a few kHz wouldn't be helpful in a classroom or hobbyist situation, just to be sure.

That would imply, for example, a 'basic' experimenter's board system with (say) 8 inputs and 8 outputs, with a library to initialise the pins.

And to satisfy the complexity enthusiasts, a different board with just SPI/I2C/UART, or a combination of both. Unfortunately, some of these protocols require bidirectional open-drain pins, which will preclude the use of optos if I2C connectivity is required, without resorting to expensive chips and complicated circuits.

I do hope I'm not stepping on anyone's toes with these ideas, I'm really just thinking out loud in terms of standardising protection and availability of the SOC resources from a hardware perspective.

Hopefully a balance can be made with presenting useful info without reinventing what is already available. Ideally enough to get beginners started will be fine (i.e. a set of example circuits, with code and reasons why they are as they are), thats the plan anyway.

Updated circuits...3 may or may not require smoothing cap, guess I'll see.

Thanks for the advice people, hopefully I've understood it enough. Things like optical buffers I will suggest they are used during testing at least, they look easy enough to use anyway.

Once I've built some of these circuits I'll create some wiki space and post details for them, which hopefully can continue with more complicated circuits too.

Hi - just to add a couple of points:

re: 1 LED Output

I would suggest the following

Note that you drive the IO Pin low to turn the LED on. The advantage is tha the port outputs are likely to have a higher sink ability (switched "0V") than source (Switched +ve). t also makes calculating the value of RLimit very easy!

i.e. Subtract LED forward voltage (usually between 1V8 and 2V2) from the supply voltage (3V3) then divide by the current required to light your LED (no more than 3-4mA, choose a low-current LED).

The Cct you show to drive a higher power LED is pretty much right, I would make it clear that the supply for the LED can (probably should!) be different from the 3V3(?) RasPi voltage.

This would be used as you show with an LED connected to "OC" or as below to a relay )I've shown a BT47W which is an industry standard - and quite cheap - 2 Pole Change Over relay. This is good for switching quite a few things for experiments. They are also in a standard package and very easy to mount on stripboard, etc. You can also wire to two "poles" in parallel to switch a higher current. NOT for mains use though.

I know this is very basic stuff, but I hope it may be of use to anyone who knows basically what they have in mind but wants a more specific details of what values, etc are likely to actually be!

ok and re: 3 Simple switch. I've made that work (using a 100n cap) connected to 5 PIC chip I/O Port, but with 200m of cable out to the (low quality) switch and a very simple software debounce. I would say you can feel confident that it would work with a reasonable button / switch and a few centimeters from the board!

IIRC the Linux GPIO driver layer provides configurable debounce in software, so I just wouldn't worry about that.

I see no reason whatsoever to include optoisolation unless working with substantially higher voltage rails in the circuit (to drive motors or actuators or whatever), or if interfacing mains. For a basic 'safe' GPIO board I would just use buffers with a selectable 3.3V or 5V power supply on the outputs. Socket them so they can be easily replaced if they get screwed up.