3

I have a python code to get readings from a light sensor. I have upgraded the raspberry pi too. But when I tried to execute the code it gives me an error as follows

Error message

Here is my python code

#!/usr/local/bin/python

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BOARD)
pin_to_circuit = 17

def rc_time (pin_to_circuit):
    count = 0
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(pin_to_circuit, GPIO.IN)

    while (GPIO.input(pin_to_circuit) == GPIO.LOW):
         count += 1

    return count

try:
    while True:
        print rc_time(pin_to_circuit)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

I have tried so many solutions found on the internet but none of it worked. Please help.

a_guest
  • 34,165
  • 12
  • 64
  • 118
Joe
  • 45
  • 1
  • 9
  • According to [this answer](https://raspberrypi.stackexchange.com/a/12967) you should probably use `GPIO.setmode(GPIO.BCM)` instead of `GPIO.setmode(GPIO.BOARD)`. Otherwise it cannot interpret the `17` as pin number. With `GPIO.BCM` this corresponds to "GPIO17". **Side note:** Please post the traceback as code, not as an image. – a_guest Jul 12 '17 at 07:19
  • @a_guest Yes it worked. But I get the reading as zero (0). What can I do for that? – Joe Jul 12 '17 at 07:21
  • What do you try to achieve? What do you expect as a result? What is your setup? You might also get more useful information on https://raspberrypi.stackexchange.com/. – a_guest Jul 12 '17 at 07:24
  • @a_guest I using a ldr to get the intensity of light. That's what I expect to get. But I get only 0 – Joe Jul 12 '17 at 07:28
  • 1
    If you want to obtain the signal of a sensor then `GPIO` is not the right setup. This allows only for for high / low voltage. Instead you might want to take a look at [ADC](https://www.raspberrypi.org/learning/physical-computing-with-python/analogue/) (Analogue to Digital Converter). But again, the people at https://raspberrypi.stackexchange.com can probably help you out better. – a_guest Jul 12 '17 at 07:34
  • @a_guest Thanks for the help. I will try it there – Joe Jul 12 '17 at 07:36

1 Answers1

1

Raspberry Pi has two ways to address the GPIO pins: GPIO.BCM and GPIO.BOARD. While BCM is how the broadcom chip, which is the brain of rpi, sees the pinheader, BOARD is relatively easy for we humans to read and address.

You have used GPIO.BOARD, and trying to address pin 17. The image below will clear that pin 17 is a GPIO as per GPIO.BCM, but according GPIO.BOARD, it's pin 11.

So, basically, you are trying to configure pin 17 which come out to be a supply 3V3, which is not user configurable.

All you need to do is either change GPIO.setmode(GPIO.BOARD) to GPIO.setmode(GPIO.BCM) or change pin_to_circuit to pin number 11.

rpi gpio header

Aniket K
  • 11
  • 3