Saulverde
Posts: 15
Joined: Fri Aug 17, 2012 12:18 am

Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 3:18 am

I'm trying to run through some tutorials to get an idea of how to do some simple coding for the gpio pins on the Pi. I was wondering if I could get some help. Below is the code provided by Adafruit for one of their tutorials. Looking at the code I can see where they are reading from pin 18 but I don't see where they defined the send pin.

I checked against the low level peripheral wiki page. Does the 3.3v pin have a special name? Could someone break down for me where the output pin is defined?

If the output pin = RCpin then is there something else I should be reading to get pin names or just a better understanding?

Code: Select all

#!/usr/bin/env python
# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!
import RPi.GPIO as GPIO, time, os
DEBUG = 1
GPIO.setmode(GPIO.BCM)
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
while True:
print RCtime(18) # Read RC timing using pin #18
Thanks for any help, even if it's just pointing me somewhere else. :)

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 7:47 am

@Saulverde: You posted the code without indention, but indention is important in Python. Otherwise it is not clear which code belongs to which function or loop or any other ”block construct”.

There is no send or output pin defined. As it is about *reading* a sensor, I do not see why you expect one?

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
Grumpy Mike
Posts: 917
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 8:49 am

When you are following a tutorial it is always best to include a link to it.
I am not sure what you mean by the send pin.

However you ask about he name of the 3.3v pin. This pin is not controllable in software. It is just a pin that is connected to the 3V3 power supply so you don't need to do anything in the software to make it "work".

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 9:03 am

Here is the full code from the http://learn.adafruit.com/basic-resisto ... ll-reading :

Code: Select all

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!
 
import RPi.GPIO as GPIO, time, os
 
DEBUG = 1
GPIO.setmode(GPIO.BCM)
 
def RCtime (RCpin):
  reading = 0
  GPIO.setup(RCpin, GPIO.OUT)
  GPIO.output(RCpin, GPIO.LOW)
  time.sleep(0.1)
 
  GPIO.setup(RCpin, GPIO.IN)
  # This takes about 1 millisecond per loop cycle
  while (GPIO.input(RCpin) == GPIO.LOW):
    reading += 1
    return reading
 
while True:
  print RCtime(18) # Read RC timing using pin #18
The tutorial has a huge mistake in it. The code above uses "setmode" to declare BCM references. That means when you feed it "18" you are using GPIO18 ... which is actually physical pin 12. It should not refer to "Pin #18". It should refer to "Pin #12" or "GPIO18".

The process goes something like this :

1) Tell the GPIO library we are using GPIO references.
2) Define a function called RCTime that accepts a number (RCPin)
3) Then repeatedly call the function and feed it "18" as we want to use GPIO18 (pin 12). Print the result.


4) The function sets the pin as an output and sets it low for 0.1 seconds. This drains the capacitor.
5) Then it sets it as an input and loops until it goes high. This time is the time it takes for the capacitor to charge and is proportional to the resistance of the sensor.

So this example only uses one pin but switches it from an Output to an Input as required.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

User avatar
Grumpy Mike
Posts: 917
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 9:29 am

The tutorial has a huge mistake in it.
I don't see how this is a huge mistake, it is the way it should be done. Using physical pin numbers in code is just stupid and is the huge mistake.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 10:12 am

I don't see how this is a huge mistake, it is the way it should be done. Using physical pin numbers in code is just stupid and is the huge mistake.
Sorry but including "Pin #18" in code when they are referring to "Pin 12 - GPIO18" is a mistake. It is huge because it confusing to everyone. You might as well use "GPIO #12" when referring to P1-12.

"Pin #18" to 99% of the population means the physical pin.

I think you need to re-read what I wrote. No one is advocating using physical pin numbers in code. Not me, not Adafruit. Jumping on my attempts to help someone is the only thing "stupid" here. Just because you wrote a few articles in the 1940s in "Valves Weekly" doesn't give you the right to be so bloody rude.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 10:35 am

Using physical pin numbers in code is just stupid and is the huge mistake.
Because humans can count pin numbers but have difficulty in remembering what GPIO number each pin is (and of course it now varies from rev 1 to rev 2 in the case of "Pin 13") - it is neither stupid or a mistake.

It can be a large mistake to mix the schemes up but both have value and neither is stupid.

Simon
PS used to be on the right-wing - Pin numbers good - GPIO numbers bad - but I've moved to the center and live and let live now :)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Saulverde
Posts: 15
Joined: Fri Aug 17, 2012 12:18 am

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Tue Sep 18, 2012 12:36 pm

I'm sorry, I should have included a link to the tutorial so it was more clear, or copied it correctly.

With that said, hank you MattHawkinsUK. Your explanation was excellent and gave me a much better grasp of what the code was doing than the tutorial alone, which didn't take much since I had very little understanding of the code apparently. :)

As far as the pin numbering goes, that is sadly the one part that I did understand as it turns out. Having no previous knowledge served me well there as I just referred to the low level peripherals wiki for the gpio numbers. I've done a couple with wiringpi and of course used his diagrams for those tutorials. You just have to keep an eye out for which gpio mode is called. I don't normally hook anything up without a gpio diagram on a screen somewhere.

Saulverde
Posts: 15
Joined: Fri Aug 17, 2012 12:18 am

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Thu Sep 20, 2012 5:30 pm

So trying to be smart I was thinking if you used a gpio as the send pin then you could detect the state switch from high to low and from low to high. Thereby not switching a pin twice to get one reading. With that in mind I've spent the last few days trying to do a better job at understanding code than I did the first time around.

The code is below. It returns all 0's with the wait timer of course. It returns all zeros with no pins connected so it has to be a coding problem. If the code was right I should get stuck in a while loop if I don't connect the pins. I'm just not smart enough to see what I need to here.

I defined everything, set the initial states for the pins and then started the reading loop. Any help would be appreciated Thanks.

Code: Select all

import RPi.GPIO as GPIO, time, os      

DEBUG = 1
reading = 0
RCPin = 18
Sendpin = 17
GPIO.setmode(GPIO.BCM)

#This toggles the output pin from a low state to a high state and writes time til sensed to global Variable 'reading'
def LoHi ():
    global reading
    reading = 0
    GPIO.output(Sendpin, GPIO.HIGH)
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1

#This toggles the output pin from a high state to a low state and writes time til sensed to global Variable 'reading'
def HiLo ():
    global reading
    reading = 0
    GPIO.output(Sendpin, GPIO.LOW)
    while (GPIO.input(RCpin) == GPIO.HIGH):
        reading += 1

#This sets the initial pin states. 		
def pinset ():
    GPIO.setup(Sendpin, GPIO.OUT)
    GPIO.output(Sendpin, GPIO.LOW)
    GPIO.setup(RCPin, GPIO.IN)
#This determine which way to toggle the pin
def RCtime ():
    time.sleep(0.1)
    if (GPIO.output(Sendpin, GPIO.LOW) == True):
        LoHi()

    elif (GPIO.output(Sendpin, GPIO.LOW) == False):
        HiLo()

    return reading
pinset()
while True:                                     
    print RCtime()     # Read RC timing using pin #18

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Thu Sep 20, 2012 6:23 pm

Not quite sure what you are trying to do with this code. It is significantly more complicated than the original. It requires twice as much code and an extra pin. What is the "sendpin" for and where have you connected it in the circuit? It is almost certainly going to stop the RC charging circuit working as intended.

Also

Code: Select all

(GPIO.output(Sendpin, GPIO.LOW) == True
is probably always true. So is

Code: Select all

(GPIO.output(Sendpin, GPIO.HIGH) == True
So I would expect the IF statement to only ever run LoHi().

GPIO.output(Sendpin, GPIO.LOW) sets the Sendpin Low. Not much point comparing it to True or False as the result is always going to be the same.

With "no pins" connected I wouldn't expect it to do much? It runs straight through the code and outputs reading=0.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

Saulverde
Posts: 15
Joined: Fri Aug 17, 2012 12:18 am

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Thu Sep 20, 2012 8:00 pm

Thanks again. I changed the lines referenced to

Code: Select all

(GPIO.input(RCPin) == GPIO.LOW)
and

Code: Select all

(GPIO.input(RCPin) == GPIO.HIGH)
I also corrected an error in the loops where I had put RCpin instead of RCPin.

My intention was to do basically the same thing the adafruit code was doing except sense each pin state change instead of every other pin state change. I wanted to set the initial pin states then start the logic loop which determines what state gpio17 is in, toggles that pin state and then reads a numerical value for the time it takes the change to be detected by gpio18. I just had the detection wrong. I was trying to find info for rpi.gpio but the link below wasn't very helpful. Not to me at least, I was looking for like a library of functions and explanations I could understand.

Thanks again Matt.

I know, now I am using 2 gpio pins for something that I could use one pin for.

Saulverde
Posts: 15
Joined: Fri Aug 17, 2012 12:18 am

Re: Adafruit Basic Resistor Sensor Reading on Raspberry Pi

Thu Sep 20, 2012 8:44 pm

Forgot the link and don't have an edit button for that last post.

http://code.google.com/p/raspberry-gpio-python/

Return to “Python”