User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

More than 8 GPIO

Mon Mar 11, 2013 11:24 pm

I'm building a pi powered internet radio. I'm going to use raspberry pi spy and meistervision's guides as a basis.

Meistervision only has 2 buttons. I would like a couple more. Can I get these straight from the GPIO?
Spy uses the following for the display
GPIO7 P1-26
GPIO8 P1-24
GPIO18 P1-12
GPIO23 P1-16
GPIO24 P1-18
GPIO25 P1-22

Does this mean I can use the following for switches?
GPIO4 P1-7
GPIO17 P1-11
GPIO27 P1-13
GPIO22 P1-15

And can the i2c and UART be used as GPIO as well?
GPIO2 P1-3 (i2c)
GPIO3 P1-5 (i2c)
GPIO14 P1-8 (UART)
GPIO15 P1-10 (UART)

I really want 4 switches for my project, and I can find use for 8. I've tried using the i2c bus, but my programing skills aren't up to scratch, and I can't find a simple enough guide.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13210
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: More than 8 GPIO

Tue Mar 12, 2013 12:24 am

yes, all the GPIO that have secondary special functions (UART, I2C, SPI etc) also are simple general purpose I/O too, but in some cases they default to the special function (specifically the UART), but most often they only gain their special status after you run a driver.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Tue Mar 12, 2013 1:14 am

So that means I can use 15 pins as GPIO, inc the SPI and i2c as GPIO?
(GPIO's 2, 3, 4, 7, 8, 9, 10, 11, 17, 23, 22, 23, 24, 25, 27)

That would be perfect. 6 for the LCD, 1 for the back-light control, leaving 8 for buttons. I don't need more than that, so won't bother trying to get the UART as GPIO.

Is there any things I need to do to make them all work as GPIO as opposed to there alternative function? Is there anything I need to do to prevent me frying them if I program them wrong (a likely outcome). I am going to connect the switches like the the pic at the bottom here.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Tue Mar 12, 2013 3:05 am

I've had a go a laying out my proposed schematics. Please see attached. I notice raspberry pi spy uses GPIO 15 for the back-light control, but this is UART by default. Should I move this to GPIO 2?
Attachments
Internet Radio Raspberry Pi 001.gif
Proposed layout for my internet radio
Internet Radio Raspberry Pi 001.gif (53.35 KiB) Viewed 3459 times

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Wed Mar 13, 2013 2:30 am

Anyone? Does that look workable?

btidey
Posts: 1636
Joined: Sun Feb 17, 2013 6:51 pm

Re: More than 8 GPIO

Wed Mar 13, 2013 11:00 am

Inputs etc look OK, but the back-light signal via the transistor looks dubious to me. There should be a resistor in the base lead at least to limit the current.

User avatar
joan
Posts: 14985
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: More than 8 GPIO

Wed Mar 13, 2013 11:03 am

VeryNoob wrote:Anyone? Does that look workable?
By gpio 2 do you mean pin 2 (the 5V power rail)?

That means the backlight will be permanently on (unless modified by your pot). If you want to vary the intensity in software you'd need to a attach it to a pin connected to a gpio.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Wed Mar 13, 2013 11:21 pm

joan wrote:By gpio 2 do you mean pin 2 (the 5V power rail)?
There are so many labels for everything, it gets confusing. I mean GPIO2, which is Pin P1-3, which has the alternative function of i2c.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

New diagram

Wed Mar 13, 2013 11:51 pm

Here is the new diagram. I've added a 27k ohm resistor as per Pi Spy. Missed that one, and I've moved the backlight to GPIO2 (pin P1-3).

How critical are the resistor values? Can the 560 ohm be replaced with a 470 or a 680? Is it better to go higher resistance or lower? Might save me a trip if I can use ones I already have.
Attachments
Internet Radio Raspberry Pi 002.gif
Internet Radio Raspberry Pi 002.gif (53.8 KiB) Viewed 3284 times

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13210
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: More than 8 GPIO

Thu Mar 14, 2013 12:13 am

resistor values for the LED are not critical, lowering them will simply brighten the LED.
In this case the 5K pot is very dominant, so it doesn't matter if the other resistor is 470R or 560R. In fact the actual LED('s) used for backlighting are leading here, they very much determine the value of the series resistance that is needed for optimal brightness. In fact, sometime two LED's are used in series, and as each LED has about 2V forward voltage, it means there is in that case just 1V left for the series resistor. If however the LED's are placed in parallel, or if just one LED is used, the forward voltage may well be just 2V, leaving 3V for the series resistor.
Typically LED currents from 1 to 10mA may be used. So the lowest expected resistance may be around 47 ohm, but the highest will probably be no more than 2K2 (so 5K seems to be a bit much, unless you want to have a very dim backlighting). Just try a 470 (or 560 Ohm) resistance first, and see what happens.

rest of the schematics are quite conventional, but should work. I see an opportunity to combine the input keys and most of the LCD control lines, so less GPIO's can be used, but it will increase the complexity of the software, this will do.

Oh, the basis resistor, I would lower it to 4K7.

If you use the PWM capabilities of GPIO18 you might even be able to software control the backlight brightness, (or contrast settings) but that also further complicates the hardware and the software software!

btidey
Posts: 1636
Joined: Sun Feb 17, 2013 6:51 pm

Re: More than 8 GPIO

Thu Mar 14, 2013 12:24 am

You want to switch the transistor hard on so that it is saturated. You first look at the maximum current you want the transistor to switch. If your backlight resistor values are OK then that looks like max is 5V / 560R or about 9mA. Assuming the gain of the transistor is at least 100 then it needs a base current of 9mA / 100 or 90uA, but you actually want to drive it about 5 to 10 times harder than that to get it well saturated. So say about 0.5mA of base current. That is well within capacity of a GPIO pin. Pin when high will be 3.3V, base will be at 0.6V so to get that current you need a resistor of 2.7V/0.5mA or 5.4K. It's not that critical and any value between 2.2K and 6.8K should do the job OK without needing too much current from GPIO pin.

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Thu Mar 14, 2013 12:31 am

Thank you everyone for your help. I really do appreciate it.

As I am only very new at this, I'm going to stick with using heaps of GPIO for my first run. I like the idea of being able to wire up just the LCD first, get that going, test it out, then adding pieces bit by bit so I can easily see where I make mistakes, rather than trying to debug the whole thing.

However once I get it all running I want to build a couple of these radios for different rooms, so I might look into some alternatives that are a bit more 'efficient'. I'm just waiting on a couple of parts to arrive, and I'll report back on how everything went together.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: More than 8 GPIO

Sat Mar 16, 2013 1:03 pm

The part which worries me are the data lines.
The LCD is powered by 5V. That suggest that the logic and thus the data lines are also 5V.
I suspect that when you try to read the four data lines will be at 5V and the Pi can only cope with 3V3.
Not a problem as long as you never READ from the device.

User avatar
joan
Posts: 14985
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: More than 8 GPIO

Sat Mar 16, 2013 1:21 pm

Gert van Loo wrote:The part which worries me are the data lines.
The LCD is powered by 5V. That suggest that the logic and thus the data lines are also 5V.
I suspect that when you try to read the four data lines will be at 5V and the Pi can only cope with 3V3.
Not a problem as long as you never READ from the device.
I think it's quite common to tie pin 5 (R/W) to ground to prevent that happening.

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: More than 8 GPIO

Sat Mar 16, 2013 8:05 pm

joan wrote:
Gert van Loo wrote:The part which worries me are the data lines.
The LCD is powered by 5V. That suggest that the logic and thus the data lines are also 5V.
I suspect that when you try to read the four data lines will be at 5V and the Pi can only cope with 3V3.
Not a problem as long as you never READ from the device.
I think it's quite common to tie pin 5 (R/W) to ground to prevent that happening.
Indeed - and I've been doing that for a while myself - e.g.

https://projects.drogon.net/raspberry-p ... d-library/

I guess there is a slightly possibility that the outputs might float to 5v before the chip fully initialisaes and ralises that the R/W line is tied to ground, but so-far I seem to have gotten away with it myself.

There are displays that work at 3.3v now though, however they're not as common as the 5v ones. (and always powere the backlight off the 5v supply - more mA!)

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
VeryNoob
Posts: 49
Joined: Thu Feb 14, 2013 1:07 pm
Location: Australia

Re: More than 8 GPIO

Sun Mar 17, 2013 2:48 am

Gert van Loo wrote:The part which worries me are the data lines....
As per the other posts, I am going to tie the RW pin on the LCD to ground, so that even if I stuff up the coding the LCD can't send data.
Is there a bettery way other than a level converter? As I don't plan on using this as an experimentation kit, I just want a radio I built myself, I don't really want to have a heap of parts I don't really need. Once I'm happy with the breadboard wiring I am going to do something a little more durable, and I want to keep it pretty small.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: More than 8 GPIO

Sun Mar 17, 2013 4:10 pm

If you don't need to read then that is simplest solution.

Alternative you can also add 2 resistors per data line and then you can read and write.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13210
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: More than 8 GPIO

Mon Mar 18, 2013 8:39 pm

to expand on Gerts answer, he's talking on a resistor divider that translates 5V from the LCD's data to 3V to the GPIO. At the same time outputting a 3V signal to the LCD will transmit undivided to the LCD. How? Simply connect a 3K3 resistor from the GPIO pin to GND, and add a 2K2 resistor between the LCD (data line) and the GPIO pin. If the LCD puts out a 5V signal it will be divided by (3.3/ (2.2+3.3)) = 0.6 times 5V = 3V. But when the GPIO drives out a 3.3V signal it will simply drive the 3K3 resistor to 3.3V, and the 2K2 resistor wont have any effect, as the LCDs input pin has a very high resistance when acting as an input, and 3.3V normally is enough "high" for the LCD.

Then, reading an LCD's memory isn't very useful, except when you want to scroll the content, or want to know if the LCD is ready. But this "trick" can be used in many other places too.

Return to “Interfacing (DSI, CSI, I2C, etc.)”