bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

[Solved] GPIO - Python

Tue Aug 26, 2014 5:18 pm

Hi everyone, i come here because i have a problem with raspberry GPIO, and i don't understand what i suppose to do.

I explain.

I bought a button like this: Image

And cable to connect to GPIO.

One go to GND and the other on a pin (i know the forbidden pin, i guess).

I have a python script to show me if i push-on it is 1 or it is 0. With this one:

Code: Select all

#!/usr/bin/env python
# -*- coding: latin-1 -*-

import RPi.GPIO as GPIO, time
import time

GPIO.setmode(GPIO.BCM)
BUTTON1 = 2

GPIO.setup(BUTTON1, GPIO.IN)

while True:
	print GPIO.input( BUTTON1 ) 
If i connect my button on the pin 2 (BCM), it is work.
Like with the button 3.

But with any other: Like GPIO 10 or 9 or any others
I push-on -> 1 -> Ok working.
I push-off -> 10 00 11 01010101. no working. It make anything, he changes alone -> 00 100001111010111111000.

I don't know why.
I ask me, if i connect to the good pin ?

Could you help me plz ?

Thank a lot.


PS. I have a raspberry PI B and i use this schema to help me with pins.
Image


Best Regards.
Last edited by bakouz on Fri Aug 29, 2014 5:45 pm, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GPIO - Python

Tue Aug 26, 2014 9:24 pm

Do some searching on the forum for debouncing. I suspect the problem is your input is floating and needs to be pulled high or low. It's a common problem so I'm sure you'll find the information you need.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

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

Re: GPIO - Python

Tue Aug 26, 2014 9:55 pm

http://raspi.tv/2013/rpi-gpio-basics-6- ... pull-downs should point you in the right direction :)

bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

Re: GPIO - Python

Wed Aug 27, 2014 10:03 am

Thanks elParaguayo for your quick answer, that you said comforts me, I believed my raspberry was out ^^.

Thanks AndrewS, i will read your page tonight after my work day.

I will come back here for i say you what happen.

Thanks again.

Mosco
Posts: 3
Joined: Sun Dec 29, 2013 8:48 pm

Re: GPIO - Python

Thu Aug 28, 2014 4:24 pm

Hello there


Have a look at this picture....its been a while since I used the GPIOS but there are some pins which have default uses but can be used as GPIOs and for that they need to be chenged from the default functions. Unfortunately I cannot remember whether I dd this using python or something else so just look it up....The pins which are not working are probably the pins that have have default uses so just find out how to change them otherwise you only have 8 "free" GPIOs to use.

look closely at this image and you will see some pins are shows as "GPIO" but still have other labels like SPI and I2C etc...

Hope that helps...

here is the link to the image
http://www.google.co.za/imgres?imgurl=h ... =0&ndsp=19

bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

Re: GPIO - Python

Thu Aug 28, 2014 7:07 pm

Mosco wrote:Hello there


Have a look at this picture....its been a while since I used the GPIOS but there are some pins which have default uses but can be used as GPIOs and for that they need to be chenged from the default functions. Unfortunately I cannot remember whether I dd this using python or something else so just look it up....The pins which are not working are probably the pins that have have default uses so just find out how to change them otherwise you only have 8 "free" GPIOs to use.

look closely at this image and you will see some pins are shows as "GPIO" but still have other labels like SPI and I2C etc...

Hope that helps...

here is the link to the image
http://www.google.co.za/imgres?imgurl=h ... =0&ndsp=19

Hello,
Thanks for your answer, so if understand what you said, the GPIO 4 should be work ? ( i looked your image, thank for that.)
I tried, and it doesnt' work, it do 00111001

i will try with the Adrew's link. and i will see

If you remember where you saw this, it's could be great :)



EDIT:
Hi guys i read your links and i understand the problem, but i tried this code

Code: Select all

import RPi.GPIO as GPIO  
from time import sleep     # this lets us have a time delay (see line 12)  
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering  
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)      


try:  
	while True:            # this will carry on until you hit CTRL+C  
		if GPIO.input(4): # if port 4 == 1  
			print "Port 4 is 1/HIGH/True - LED ON"  
			print GPIO.input( 4 ) 
			
		else:  
			print "Port 4 is 0/LOW/False - LED OFF"  
			print GPIO.input( 4 )
			
		sleep(0.1)         # wait 0.1 seconds  
  
finally:                   # this block will run no matter how the try block exits  
	GPIO.cleanup()         # clean up after yourself  
And it doesn't work :/

Any idea?

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: GPIO - Python

Fri Aug 29, 2014 4:39 am

are you getting any specific errors?

maybe try debugging?

change whats under while True to this

Code: Select all

while True:
    print  GPIO.input(4)
    sleep(0.2)
you can lower the sleep if you want to 0.1. but just click the button and see if the numbers change. if they don't then you probably put it on the wrong pin or connected it wrong.

bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

Re: GPIO - Python

Fri Aug 29, 2014 7:18 am

mrteach wrote:are you getting any specific errors?

maybe try debugging?

change whats under while True to this

Code: Select all

while True:
    print  GPIO.input(4)
    sleep(0.2)
you can lower the sleep if you want to 0.1. but just click the button and see if the numbers change. if they don't then you probably put it on the wrong pin or connected it wrong.

Hi Mrteach,

I have not error with the last script i posted.
Just the same result.

And if you look my first post, we can see almost the same script as you, i have not error on python script.
Just the result of the print which is 011010101 when i pull-up and 0 continue when i pull-down.

I guess i should to disable UART and other pins like this.

Thanks for your help.

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

Re: GPIO - Python

Fri Aug 29, 2014 7:25 am

I've lost track of if you have a problem or not.

Please note that gpios 0, 1, 2, and 3 (I2C) have hard-wired pull-up resistors to 3.3V. The internal pull-up/down resistors will have no effect.

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: GPIO - Python

Fri Aug 29, 2014 7:36 am

I think you said you're wiring the button between GPIO and GND.

For that you need Pull-Up resistors, not Pull-Down, so you would use

Code: Select all

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
As joan said, the two pins that work have permanent Pull-Up resistors on the board. The others don't.

bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

Re: GPIO - Python

Fri Aug 29, 2014 8:16 am

rpdom wrote:I think you said you're wiring the button between GPIO and GND.

For that you need Pull-Up resistors, not Pull-Down, so you would use

Code: Select all

GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
As joan said, the two pins that work have permanent Pull-Up resistors on the board. The others don't.

Ohhh okeyy.
I think you are right !
I try tonight ! Thanks a lot !

And sorry joan if you dit not understand my explications ! It is a little bit difficul to explain in english^^
To summarize the python script does not give me an error like "Erro line 20":
And i know the script does not work because i have not the result i shoud to have ^^.

But i just understand the pull-down and up resistors so i think tonight it will be working !

bakouz
Posts: 6
Joined: Tue Aug 26, 2014 5:05 pm
Location: Bordeaux - France

Re: GPIO - Python

Fri Aug 29, 2014 5:43 pm

Yeaaaaah it is working !


Thank a lot all of you !
I just change :

Code: Select all

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
by

Code: Select all

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Thank again :)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: [Solved] GPIO - Python

Fri Aug 29, 2014 5:55 pm

Congratulations - have fun with your project.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”