Easy GPIO Hardware & Software


134 posts   Page 5 of 6   1, 2, 3, 4, 5, 6
by gordon@drogon.net » Mon Apr 02, 2012 12:45 pm
pwm said:


Gert, thank you for the sources that fill in more of the high level details to drive various I/O devices exercised in the video demo. It's a pity the 'stdio.h' file has been omitted as this would complete the picture by providing the base address/peripheral resource. A link to a relevant 'stdio.h' would be most welcome.



stdio.h is standard C library stuff - nothing to do with the GPIO on the R-Pi. If you want to see stdio.h, then look for /usr/include/stdio.h in any linux system.

the GPIO stuff (addresses, modes, the way to peek & poke it) is all defined in gertboard_demo.c

It's still a sub-optimal way to do it IMO - it needs root access to run that, and if the program goes wrong theres potential to scribble all over memory. I'll be looking to write a device driver for it when I get mine - unless someone else has done it (or is doing it!)

G
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1487
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by rPiWM » Mon Apr 02, 2012 9:09 pm
GordonH, thank you for your post. Indeed, resource_BASEs are #defined in gertboard_demo.c (at last) with a number in answer to my Q1 in an earlier post.

I'll watch for your device driver for the PWM resource.

mole125, Gert's sources suggest that he is using the BCM2835 embedded PWM channel for the demo.
Posts: 5
Joined: Tue Mar 20, 2012 12:05 pm
by Dave_G_2 » Thu Apr 19, 2012 6:53 pm

Hi all.

I apologise if I have posted in the wrong section, wasn't too sure where my question should go.


but have subsequently found this thread which seems more appropriate.


I intend to build a small interface board making use of GPIOs 0 -7

As a quick method to test, I would like to use a script to send data out.

From the script example over at http://elinux.org/RPi_Low-leve.....eripherals

it shows only a single bit set as an output (4) and the value is sent to /sys/class/gpio/gpio4/value.

How would I be able to send a byte at a time without having to set each /gpio_x/value one at a time?

Also is it possible to send multiple bytes one after the other or is there some kind of delay or "kludge" that has to be used?

Sorry if they are stupid questions, but I've never used ARM before.

Thanks in advance,

Dave.

User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Fri Apr 20, 2012 8:23 am
Hi Dave,

Unfortunately I've no way to test, but I imagine there should be a way to send bytes out to the GPIO (assuming of course the data boundaries of the addresses are aligned), that is something you'll have to look at closely on the Broadcom Datasheet (within the document section of the wiki).

As far as delay is concerned, I would expect the script route would probably present enough delay that you'd probably not need to add your own.  Something which you'll probably find out with some testing.

Sorry that I can't give any more info, since I would rather be able to test what I say before making too many suggestions.

_________________________________________________

As far as the Easy GPIO sections, I am keen to get the following completed soon:

1) Some simple circuits which will provide some protection to the RPi.  At the moment, I think this will be in the form of some Opto-Isolators, simply because they aren't too complex for beginners to understand and creating a circuit to use them should be easy enough for most novice users.

2) Some real RPi code and examples.  Sorry but I can't do it until I get one (I could guess but that won't be fair on anyone, since it unlikely to be correct without testing).
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Fri Apr 20, 2012 8:51 am
Hi meltwater

Thanks for the reply.

I too do not have a physical unit to play with, just trying to learn as much as possible before my Pi arrives.

What you say about the inherent delay using scripts makes sense.

Managed to read the datasheet, concentrating mainly on pages 89 – 104 and I also downloaded the source for the Gertboard to compare.

What I have found is that the GPIO hardware is mapped to 20000000h by the OS (the CPU bus addresses for the I/O start at 7E000000h) see page 5 of datasheet.

So the address range of interest starts at 20000000h which is the first GPIO register called GPIO Function Select 0 which manipulates GPIO lines 0 – 9 by setting them either as inputs, outputs or used for alternate functions.

The next address 20000004h (all regs are 32 bits) deals with GPIO 10 -19 and so on.

There are other registers to set the pull ups/ pull downs, to set the value of the bits and so on.

So why can't one just simply access these registers directly using either ASM or C ?

Surely then 4K boundary alignments and all the other stuff is not required as we are accessing the GPIO at a very low level?

Or am I missing something?

As regards your GPIO protection requirements, have you considered the 74HC245 octal bus transceiver?

It could be used for input or output by using the direction pin (could use an extra I/O to control direction).

Some series resistors (about 330 ohms or so) ahead of the 245 would also help.

You wouldn't get optical isolation but they are much much faster then the run of the mill optos and their pin-outs make them very easy to use.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by Frank Buss » Fri Apr 20, 2012 8:56 am
There is an example how to access the GPIOs without /sys/class:

http://elinux.org/Rpi_Low-leve.....eripherals

The datasheet at http://www.raspberrypi.org/wp-.....herals.pdf , page 89 ff. explains the details. Short version: there are SET, CLR and LEV registers. Each register has 32 bits, which controls 32 pins. If you write to the SET register, the GPIO output is outdated to 1 for all 1 bits in the register, cleared for all 1 bits in CLR register and you can just set the state with the LEV register.

I think opto isolators are not the best idea, because they work in just one direction and depending on the type, you'll need some more electronics for driving the LED (because of the low output current allowed on the RPi GPIO) and amplifying the photodiode on the receiving side. My board is available now and better for experiments: http://www.frank-buss.de/io-ex.....index.html Or you could just use some drivers, like SN74LVC1T45, which would even allow it to convert the 3.3 V to 5 V, e.g. for attaching an Arduino.
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by Dave_G_2 » Fri Apr 20, 2012 9:04 am
Hi Frank

Thanks for your confirmation that one can indeed just access the registers directly.

As regards the GPIO protection, I normally use the 74HCT245 as protection.

Of course there is no opto isolation but like you say direction is then not a problem as the 245 has direction control which can be connected to another I/O.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Fri Apr 20, 2012 9:51 am
Yep an experimenters I/O board like above is great and worth using (plan to pick one up at some point).

--------------

However, I am aiming to focus on grass roots circuits as a basis for basic projects, so it will help to suggest some options.

I'm quite happy to consider any of the solutions, ideally want to keep it as simple as possible and low cost (around 50p to £1 for the chips).

OptoIsolators - bit more expensive anyway (around £1 for 4 i/o)

74HCT245 octal transceiver - would probably set up with two (or duplicate one), one in each direction (perhaps with jumper control). Provides 8 i/o.

SN74LVC1T45 - Are there any multi channel versions of this?

What issues would there be by using ULN2003 (7 darlington array), for pure outputs (for driving things, not other logic)?
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Frank Buss » Fri Apr 20, 2012 10:08 am
Yes, there are 8 bit level converters, like SN74LVC8T245. I think ULN2003 should work, too, but it can just drive in open collector style, so might be useful for switching lamps, but e.g. not for driving a H-bridge for motor control. For such applications the MIC4421/4422 is nice, or use just a gertboard :-)
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by Dave_G_2 » Fri Apr 20, 2012 10:14 am
As regards the ULN2003, the only possible issue that might arise is the base drive available from the GPIO pins.

What is the gain (beta) of the xstors in the ULN as this would determine the required base current to drive them. (Ic = beta x Ib)

Since the ULN uses darlington pairs, I think it's a non issue due to the inherently high gain which would easily allow it to drive a relay or even a small motor.

Best to check the base drive requirements anyhow just to be 100% sure.

Have you checked the range of quad optos available from Toshiba?

It may work out cheaper then using individual 4N25 type optos for each GPIO pin.

Is 3.3V really a problem when driving a 5V input?

Surely it would still be seen as a valid logic high state?

Of course the other way round would be a problem for the Pi.

What I have done before on other circuits, is use a simple voltage divider on 3.3V logic inputs being driven from 5V logic.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Fri Apr 20, 2012 10:43 am
Fair enough, I may see if I can source a ULN2003 to experiment with, since they are fairly low cost.

As far as SN74LVC8T245...at the moment I've not had a burning need for 3.3V to 5V converting, yet.  I guess, in a lot of cases you'll only need a couple to create a bus, so may be a waste to try to use them for all GPIO.  I think I'll treat them as a separate issue to general I/O protection, unless there is a low cost option for multi channels.

Forgive my knowledge, but couldn't you do basic voltage shifting with opto's anyway?

Work out at about £1.20 for 4 channels.

74HCT245 - around 0.70p for 8 channels.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Fri Apr 20, 2012 10:54 am
That is quite a price difference.

Yes, you are correct that one can do level shifting with optos.

You may also want to check out the ULN2803 as it has 8 drivers.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Fri Apr 20, 2012 11:29 am
The ULN2803 is slightly more cost, but 8 drivers opens up more options.  Either will be worth playing with.

I may end up with playing with all of them at some point.

I guess the info i am missing is, what is the major disadvantage of using opto isolators over the 74HCT245 octal transceiver.  I am guessing the cost, and perhaps added latency, and additional wiring (as you effectively create two separate circuits).

The octal transceiver buffer probably is easiest, since it basically is a in-line solution. (I'm getting confused now with the 74HCT245/74LVC8T245 codes now), but would the following TI SN74LVC244AN be a good solution?

http://uk.farnell.com/texas-in.....dp/1749755

I know it lacks the direction selection, which is a pain if you need to change it dynamically (is that needed?).  Anyway, I'm useless at selecting ICs, it is a bit of a black art.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Fri Apr 20, 2012 12:03 pm
But the 74LVC244AN has OE (output enable) inputs which can be made use of similar to the circuit I have uploaded here:

http://www.freeimagehosting.net/92ncc
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Mon Apr 23, 2012 10:15 am
Dave_G_2 said:


But the 74LVC244AN has OE (output enable) inputs which can be made use of similar to the circuit I have uploaded here:

http://www.freeimagehosting.net/92ncc


Very handy, I guess with those chips you can even do it with a single one for 4 channels.

I might see if I can put some basic info in the wiki regarding protection, although until I"ve had time to fully do it, it will be more general bits.

Some information is better than nothing and will at least suggest that some kind of protection is worth adding.

UPDATE: Done a basic page, please feel free to comment and expand on it please (as you can probably tell, I'm not that familiar with this topic).

http://elinux.org/RPi_Tutorial.....n_Circuits

I think it'll be worth having a simple print out slot-on pinout card, which fits over the pin header labelling up each of the pins.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Mon Apr 23, 2012 9:45 pm
meltwater

Certainly a good start but you forgot to mention other devices such as zener diodes and transzorbs that can also be used for protection.

Polyfuses are great for current limiting but take many seconds to heat up and go into their high impedance state by which time the delicate GPIO I.C. has long been destroyed.

That is why polyfuses are mainly used in power supplies and in line with speakers which can handle a bit of "abuse" until the polyfuse kicks in.

A very common and effective protection method is to put a series resistor on the I/O line followed by shunt zener going from the I/O line to ground and another from the I/O line to supply.

One drawback to this type of arrangement is that if a voltage is applied to the I/O line without the rest of the circuit being powered up, we can get "latch-up" effects due to the diodes which "supply" the circuit.

Use this only if it"s guaranteed that any inputs will only occur when the circuit is powered up, else the use of opto couplers is probably the best way.

Just don't forget the anti-parallel diode with the opto's LED to protect it against excessive reverse voltages.

Another source of damage is induced spikes into long lines connected to I/O pins due to nearby thunderstorm activity.

A good solution to this is a LC type LPF (to reduce the wideband noise spikes) followed by series resistor/s and a shunt zener or transzorb.

A word or caution about  filters is ringing and back EMF caused by the inductors so this has to be taken into consideration if designing them in.

A resistor in parallel with the inductor/s has the effect of bringing down the Q of the inductor and as a result the ringing is reduced as well.

A good source of information and ideas about protection, is to study the schematics of PLC controllers used in harsh industrial environments.

Their input and output ports have to be very well protected to cope with this.

Good quality I.C. sockets are also recommended where possible to allow the easy changing of the interfacing/protection I.C.s if they are damaged which hopefully won"t be too often.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Tue Apr 24, 2012 9:13 am
Thanks for the feedback.  I"m still learning about this aspect so I know I"ll miss stuff, interesting though.

1. Zener diodes/transzorbs – Yep I can see how they would work well to clamp the voltage.  Not seen many 3.3V transzorbs in farnell, so I expect it would need to be zeners for most people.

I take it they are the same as Transient Voltage Suppressors (TVS) or are these different again?

2. Polyfuses – I only mentioned it in reference to the one which protects the RPi psu, that bit can probably be removed (although I"ve not seen anywhere which mentions that it takes time to reset, so perhaps that should be added in the main part if it is not there).  I"d not considered using them for I/O pin protection.

3. Opto-couplers – Although I like the idea of them, but as Frank Buss mentioned above, it seems quite hard to find ones which have current low driving requirements. Would be great if we can find some low current ones which are usable.

The anti-parallel diode I assume would be for if you are using it for inputs?

4. I"d not thought about long I/O lines (more advanced than my current thinking), will get into issues such as voltage loss and perhaps even RF pickup too.  That could be added as an additional subsection.  I guess worth mentioning the usual noddy about avoiding mains (it might seem obvious but someone will have a bright idea of controlling their bedside lamp or something)!

5. Yep, will put in a note about using IC sockets.  Will also add, a multi-meter is worth using as a quick sanity check (better still an oscilloscope if available).

All of the sections need more detail and some actual circuits, but hopefully that can be filled out over time.  I was more aware that there was no basic information at all regarding how to protect the circuits, and more and more people will have hardware to play with.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Tue Apr 24, 2012 10:07 am
Hi meltwater

There are low voltage transzorbs available.

The lowest is meant for 5V lines and can be used as pre-clamping followed by another resistor and some zener diodes doing the secondary clamping down to 3.3 or 2.7V.

The idea here being that a transzorb can normally dissipate much more power then a zener so any spikes/over-voltage is primarily and mostly attenuated down to 5V by the transzorb then "tamed" a bit more by the zeners.

I have uploaded a schematic here:

http://www.freeimagehosting.net/ukwfl

Some may say it"s a bit of an overkill, but the cost of the extra components and a bit of PCB area, it"s much better then toasting your Pi.

Transzorbs and TVS devices are pretty much the same thing.

Another option is to use BAV99 diodes between the I/O line and +V and also GND as being used on the Raspberry Pi boards.

A polyfuse can be used on an experimenting board to protect the supply if this is available for the experimenter to add extra circuitry of their own and would need a supply.

A polyfuse can be used on a I/O line but only ones that have a low impedance drive.

There are polyfuses available for currents as low as 50mA.

As far as optos go, the amount of "drive" needed for the LED is pretty much dependent on the current transfer function/gain of the opto, consult the datasheet for this spec.

As an example, the cheap and cheerful 4N25 requires 10mA.

This current could be reduced slightly when the opto is driving "light" loads such as the Pi"s I/O lines with either the pull up or down resistors activated.

The anti-parallel diode I mentioned, is to protect the opto"s LED against high reverse voltages which could be encountered from the "outside world".

Again the datasheet would give this limit.

It"s best to use "beefier" diodes here such as the 1N4007 or even the 1N5408 and avoid the 1N4148 or 1N914 type diodes.

Driving mains loads is always a bit tricky when it comes to kids and inexperienced users and is best avoided.

Not only is the safety issue of the user/s of paramount importance, but there are also RFI/EMI and isolation criteria in most countries that must be met with mains interfacing.

Yes you are correct that people will try do crazy things with the Pi and mains stuff.

I recall as a kid I had the bright (or should that be dim) idea of varying the brightness of a mains bulb by simply using a rheostat, after all who needs a proper dimmer using triacs and the such. :-)

Needless to say it vaporized instantly leaving a smoldering metal shaft and the room smelt of burnt carbon for hours afterwards.
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by meltwater » Tue Apr 24, 2012 11:11 am
Excellent, not sure when I'll have time to update all of that, but will update here when i have.

Love the freeimagehosting.net thing, you get some interesting images on there by clicking next (i.e. I got this one http://www.freeimagehosting.net/e5094).

The 4N25s are nice and cheep, 10mA is on the high side but more than suitable for prototyping, would probably go for 5mA ones if I can.

Yep, also I don't think anyone's house insurance will be too keen when they pick through the remains of the house, to find a RPi at the heart and some mains switching circuits hacked together and left on.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Dave_G_2 » Tue Apr 24, 2012 11:18 am
http://www.freeimagehosting.net/e5094).

Nice um.....er.....hedgehog. :-)
User avatar
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm
by Sleepy » Sun Apr 29, 2012 2:51 pm
Frank Buss said:

Or you could just use some drivers, like SN74LVC1T45, which would even allow it to convert the 3.3 V to 5 V, e.g. for attaching an Arduino.

I do not want to appear as heretic... It is just because I am presently using a circuit I made connected to the parallel port of my full size PC, which I would like to pass on to the PI GPIO without major changes if possible...

Would it be possible to use a SN7406N buffer (just for output)?

Levels should be recognised though the 7406 is actually a TTL device, but I do not want to fry my Pi for problems I am not able to foresee (current draw seems low and acceptable, but I am no real expert of the matter).

Thank you for the advice!

Sleepy
Posts: 10
Joined: Sun Apr 29, 2012 1:33 pm
by mahjongg » Sun Apr 29, 2012 3:28 pm
If you just want to use a GPIO pin as an output, to output a TTL level (or larger) signal through a TTL buffer, then most TTL"s will happily accept the 3V3 output level as a valid "1". No problem at all.

in fact a regular SN7406N will accept voltages as low as 2.0 Volt as "1".

basically this is true for most 74HCT logic, like the 74HCT04N.

If you are switching inductive loads with it, then either add a reverse diode across the load. or use an ULN2003, that will work fine if your load current doesn"t exceed 300mA.

If you need more load current, simply use a ULN2003, with a 1K series resistor on the ULN2003"s input. See the ULN2003/2001 datasheet.

Problems only arise if you try to send TTL levels into a GPIO port, as these simply cannot cope with signals that are larger than its power supply voltage.  A simple solution , suitable for slow signals, is simply to use a resistor divider. A "luxury" solution would be to use a buffer converter with two power inputs, this is also the best solution when you want to create a bidirectional port, only problem is that you need a second GPIO port to control the 3-state input (direction)of the buffer. Another possible solution for a bidirectional port is to use a level translator circuit, such as those developed for the I2C bus. You can built your own with just a few FET's, see this white paper about it:

http://ics.nxp.com/support/doc.....n97055.pdf

Obviously such a solution isn't necessary if the other devices on the bus can also work with 3V3 levels.
User avatar
Forum Moderator
Forum Moderator
Posts: 4894
Joined: Sun Mar 11, 2012 12:19 am
by meltwater » Mon May 28, 2012 8:33 am
I hope to return to doing more on the GPIO soon, now I have a RPi.
It is always nice to see flashy lights working on the board, although still very nervous making those connections!

I should have some example code for driving LEDs and reading the switches soon (should finish off those two guides). I will then see if I can get the Alpha-numeric display going and take a look at the protection circuits (I'll probably have a look at a few different ones, but since gert is using a 74xx244 device I may give that a go to start with). I think it will be very helpful to have something which can do level shifting as well so I'll probably look at something for that.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by bigjohnson » Thu Jun 28, 2012 9:01 am
I read the uln200x datasheet and the correct solution could be use a uln2001, that haven't the input resistor with a 1k resitor on the base.

Alberto
http://www.panu.it/raspberry/ Raspberry Pi GPIO pinout details
http://www.panu.it/raspand232/ Raspberry Pi serial console
http://www.panu.it/raspandmax/ use MAX1202 MAX1203 MAX1204 SPI adc with Raspberry Pi
Posts: 11
Joined: Thu Jun 28, 2012 8:55 am
Location: Milan, Italy
by meltwater » Thu Jul 12, 2012 8:05 pm
Right, been a little busy with the Magpi recently, but I have been taking some time out to get some serious hardware playing done.

This includes....an alpha-numeric 16x2 LCD display running off two GPIO pins....hopefully useful for debugging.

Hopefully can get some more of the GPIO guides updated in the wiki, and perhaps some stuff to go in the MagPi at some point.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am