GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

GPIO PI Beginners question

Sun May 13, 2012 6:41 am

Hi,

Can somebody tell me if the following is correct?

I want to use the PI to read out some photo transistor to follow the LED pulse of the electricity meter.
  • Can I connect a phototransistor between the 3.3V line and a GPIO port (0-7)?
  • On other boards people are using a 4K7 resistor between the 3.3V and the GPIO port. They connect the other line to a GND. Do I need also a resistor and using the 3.3 / GPIO AND GND
  • What is the most simple way to test the GPIO input? I've written a program to read out the input and want to test that before using the photo transistor. Can I connect a wire to the 3.3v and GPIO input line and shorten them to emulate a pulse?


Thanks for helping

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

Re: GPIO PI Beginners question

Sun May 13, 2012 2:12 pm

GAEvakYD wrote:Hi,

Can somebody tell me if the following is correct?

I want to use the PI to read out some photo transistor to follow the LED pulse of the electricity meter.
  • Can I connect a phototransistor between the 3.3V line and a GPIO port (0-7)?
You could, but then you need to add a resistor between the GPIO and GND, but it is much more common to connect the phototransistor in between the GPIO and GND, and add a weak pullup resistor.Obviously a lighted phototransistor will read as a "0", not as a "1", but that isn't a problem.
[*]On other boards people are using a 4K7 resistor between the 3.3V and the GPIO port. They connect the other line to a GND. Do I need also a resistor and using the 3.3 / GPIO AND GND
Normally yes, but the GPIO's of the PI have programmable pulups (and maybe also pulldowns), which you can turn on, but with an external pullup/pulldown you do not need to turn on the internal ones.
[*]What is the most simple way to test the GPIO input? I've written a program to read out the input and want to test that before using the photo transistor. Can I connect a wire to the 3.3v and GPIO input line and shorten them to emulate a pulse?[/list]
It's not a very good idea to directly connect a GPIO pin to 3V3, if you have made a mistake in programming the GPI0 as inputs, and the GPIO is trying to output a "0", you will blow up the GPIO pin, and maybe damage the SoC.
That is one reason to use a pullup, and and active pulldown switch (phototransistor), if you shorten the GPIO pin while its an output its safer, for two reasons, 1 the GPIO is more likely to output a "0", and 2 the GPIO outputs are current limited.
To test the GPIO as input the best way is to use a 10K pullup, and shorten it to GND with a 100 Ohm resistor connected to GND.
Thanks for helping
You are welcome.

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Tue Jun 12, 2012 8:57 am

After a long time of programming my C++ tool I finally found some time to fix the hardware stuff for my GPIO project. Thanks for mahjongg for the detailed answer. Sad to see that my knowhow about hardware is so minimal. Hopefully you guys would help me a little bit so I know what to buy and to solder.

About this comment to test the GPIO input:
To test the GPIO as input the best way is to use a 10K pullup, and shorten it to GND with a 100 Ohm resistor connected to GND.
Can anyone draw in paint or something how to solder this items to the three pins (GPIO/GND and 3.3V). Like I did it for my router to confirm my schema. http://ordelman.org/tmp/tplink_gpio20_deel2.jpg

The same question for this comment:
connect the phototransistor in between the GPIO and GND, and add a weak pullup resistor

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: GPIO PI Beginners question

Tue Jun 12, 2012 9:56 am

Code: Select all

3.3V------------------+----
                      |
                      -
                     | | 10k
                     | |
                     | |
                      -
                      |
                      |
     switch           |      
     +      +---------+------------- GPIO
          /
         /
        +
        |
        -
       | | 100 ohm
       | |
       | |
        -
        |
        |
GND-----+----

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Tue Jun 12, 2012 11:43 am

Plenty of examples on google - here is one :

Image

It is suggested that a 100ohm resistor is put between GND and the tranny. Also you may need to change Rc to be a potentiometer instead of a fixed value resistor. You can experiment with the circuit in your meter box without connecting it to the Pi and measure the voltage at Vout when the meters LED is on and off.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Tue Jun 12, 2012 6:34 pm

rurwin wrote:

Code: Select all

3.3V------------------+----
                      |
                      -
                     | | 10k
                     | |
                     | |
                      -
                      |
                      |
     switch           |      
     +      +---------+------------- GPIO
          /
         /
        +
        |
        -
       | | 100 ohm
       | |
       | |
        -
        |
        |
GND-----+----
Thanks, this is the option to test the GPIO input? Am I right that the left side of the swith must be connected to GND?

I liked this low level drawing for people like me. :D :D Need another one for the situation with the phototransistor.

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Tue Jun 12, 2012 6:43 pm

No, the left side of the switch doesnt need to be connected to 0v, it can be left floating.
My post above shows how to connect to phototransistor - the Vout connects to the gpio i/p.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: GPIO PI Beginners question

Tue Jun 12, 2012 6:49 pm

rurwin wrote:

Code: Select all

3.3V------------------+----
                      |
                      -
                     | | 10k
                     | |
                     | |
                      -
                      |
                      |
     switch           |      
     +      +---------+------------- GPIO
          /
         /
        +
        |
        -
       | | 100 ohm
       | |
       | |
        -
        |
        |
GND-----+----
So in order to use the gpio pins we need to hook it up to the 3v3 pin? I thought the gpios could output 3v3 on their own?
Dear forum: Play nice ;-)

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: GPIO PI Beginners question

Tue Jun 12, 2012 7:51 pm

abishur wrote:So in order to use the gpio pins we need to hook it up to the 3v3 pin? I thought the gpios could output 3v3 on their own?
Yes, but in this example the GPIO is being used as an input. See http://arduino.cc/en/Tutorial/Button for more info about pull-up/pull-down resistors. (the key thing, is to avoid 'floating' inputs)

EDIT: http://elinux.org/RPi_Tutorial_EGHS:Switch_Input is probably a better link ;)
Last edited by AndrewS on Tue Jun 12, 2012 7:58 pm, edited 1 time in total.

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: GPIO PI Beginners question

Tue Jun 12, 2012 7:57 pm

AndrewS wrote:
abishur wrote:So in order to use the gpio pins we need to hook it up to the 3v3 pin? I thought the gpios could output 3v3 on their own?
Yes, but in this example the GPIO is being used as an input. See http://arduino.cc/en/Tutorial/Button for more info about pull-up/pull-down resistors. (the key thing, is to avoid 'floating' inputs)
Ah, that makes sense. Thanks for the link!

Side note, they talk about a "pull up" or "pull down" resistor, but it's not like those are special types of resistors right? They're just there to... encourage? the electricity to flow along the desired path when the button isn't being pressed and make sure stray voltage doesn't result in undesired behavoir.
Dear forum: Play nice ;-)

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Wed Jun 13, 2012 7:19 am

That Arduino example is quite simple to understand for me. :)

-So to test the GPIO digital input I could use that diagram from Arduino? With a switch and a 10k pullup resistor and no 100Ohm resistor? With the only difference the PI must be connected to
the 3.3V and not like the arduino on the 5V.

http://arduino.cc/en/uploads/Tutorial/button_schem.png

-For the final setup I would use a phottransistor to generate the input. Is it so simple to only change the switch with the phototransistor? So connect the first leg to the 3.3V and the
other leg to the switch?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: GPIO PI Beginners question

Wed Jun 13, 2012 8:47 am

The IO pins on the Pi are less "robust" than the IO on the Arduino, so it's a good idea to include the 100Ohm resistor to limit the amount of current that can flow into the Rpi.
Look at the link I added in my "Edit" to my previous post :)

Disclaimer: I've not tried any of this with my own RPi yet, I'm just repeating what I've read elsewhere!

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Wed Jun 13, 2012 10:59 am

Are these two schema a good conclusion?

First to test the GPIO input without the transistor:
Image

Last the final setup with transistor instead of switch
Image

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: GPIO PI Beginners question

Wed Jun 13, 2012 11:33 am

GAEvakYD wrote:Are these two schema a good conclusion?
Two connections to GND? I don't think they'd do much ;)

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Wed Jun 13, 2012 12:50 pm

AndrewS wrote:
GAEvakYD wrote:Are these two schema a good conclusion?
Two connections to GND? I don't think they'd do much ;)
Aiiiii, big mistake. I've edited the two files. Looks better?

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Wed Jun 13, 2012 1:00 pm

Attached is correct for the switch.

Texy
Attachments
input_switch.JPG
input_switch.JPG (24.68 KiB) Viewed 9086 times
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Wed Jun 13, 2012 1:06 pm

Try this for the photo tranny, although you'll proably need to omit the pull-down, ie connect the emitter to 0volts (GND) :
Attachments
photo_switch.JPG
photo_switch.JPG (25.56 KiB) Viewed 9085 times
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Wed Jun 13, 2012 1:41 pm

texy wrote:Try this for the photo tranny, although you'll proably need to omit the pull-down, ie connect the emitter to 0volts (GND) :
Thanks. One last questions:
  • what you mean with the 10K pot? Does my dutch electronic company understand this keyword?
  • is there a difference between a 100Ohm resistor and a 100 Ohm Pull down or it that the same?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: GPIO PI Beginners question

Wed Jun 13, 2012 1:54 pm

If you connect the emitter to 0V, then the GPIO pin is at 0V. I'd say you also need a 100ohm resistor, otherwise when the potentiometer is at minimum there is a dead short between 3.3V and the GPIO pin.

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Wed Jun 13, 2012 2:12 pm

Yes - good call ;-)
Change the 10k resistor to a 100ohm resistor, and it definiately needs to be fitted.
The 'pot' is a variable resistor, or potentiometer - its resistance can be altered between 0ohms and its maximum, 10,000 ohms in this case.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: GPIO PI Beginners question

Wed Jun 13, 2012 2:24 pm

AndrewS wrote:The IO pins on the Pi are less "robust" than the IO on the Arduino, so it's a good idea to include the 100Ohm resistor to limit the amount of current that can flow into the Rpi.
Look at the link I added in my "Edit" to my previous post :)

Disclaimer: I've not tried any of this with my own RPi yet, I'm just repeating what I've read elsewhere!

If you put that page together, nicely done! For anyone trying to learn this (like myself ;-)) I *highly* recommend reading the arduino tutorial it links to (or I suppose I can just directly provide the link right here). It provides the best description of what's going on I've read yet!
GAEvakYD wrote:
Thanks. One last questions:

[*]is there a difference between a 100Ohm resistor and a 100 Ohm Pull down or it that the same?
Having spent some time reading about it, I can now saw that they are both just plain resistors. Saying "pull up" or "pull down" is merely specifying it's intended function; which is do I pull up this input to a high value when the switch is not pressed or do I pull down the input to a low value when the switch is not pressed?
Dear forum: Play nice ;-)

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Wed Jun 13, 2012 2:41 pm

.....which ends up meaning, will I read a 1 or a 0 when the switch is not pressed.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Wed Jun 13, 2012 6:30 pm

Ok, so this are the final two setups I need?
http://ordelman.org/tmp/input_switch.JPG
http://ordelman.org/tmp/final.png

What is the sense for the pot meter? I want to read 0 or 1, is there no average value to add with a resistor?

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO PI Beginners question

Wed Jun 13, 2012 6:47 pm

Yes but you may need to adjust how much light triggers a high or low.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

GAEvakYD
Posts: 17
Joined: Sat Sep 10, 2011 6:48 am

Re: GPIO PI Beginners question

Tue Jun 19, 2012 8:41 pm

Today I started to solder the two setups. I've started with the test setup with the switch. This works great. Default the GPIO port is value 1 and when I pushed the button the value become 0. Nice.

After this I started with setup two, the phototransistor. This setup drives me crazy after a lot of trying. The default value is 0 and stays 0. No 1 will be read after flashing a light in front of the phototransistor.
I've tried to switch the two legs of the transistor, with no positive result.

What are the possibilities to check? Can I replace the phototransistor in this setup by the switch? With that setup I can test the resistors and connections on the board. If that works, it must be the connection with the transistor. Otherwise??

Return to “Beginners”