0

I try to port my GPIO project from quick2wire to WiringPi. My aim is to read the state of a photocell connected to pin 24 of my raspberry pi (model B, revision 2).

My code (python 3) looks like this:

import wiringpi2 as wiringpi
import time

wiringpi.wiringPiSetupSys()
# wiringpi.wiringPiSetupGpio() # it would work with this instead of wiringPiSetupSys()

counter = 0
status = "unknown"
while True:
    counter += 1
    light = wiringpi.digitalRead(24)
    if not light:
        status = "ON"
    else:
        status = "OFF"
    time.sleep(3)
    print('(' + str(counter) + ') ' + status)

When I wiringpi.wiringPiSetupGpio() instead of wiringpi.wiringPiSetupSys() the code works as expected. Using wiringpi.wiringPiSetupSys() however the output doesn't change - it would stay ON or OFF no matter how the state of the photo cell changes.

I also made sure that the pin is exported

$ gpio exports
GPIO Pins exported:
   5: in   0  none 

As I checked the state of the photocell with gpio gpio -g read 24 in the shell, I can exclude the possibility of a hardware issue.

As I want to use the code in a web application, I would strongly prefer to use wiringPiSetupSys() as a non-root user.

Can you help me solve or analyze the cause of this issue?

2
  • Did you ever get it to work in your code? I can't get my python script to output the status of the pin.
    – Ryan
    Commented Apr 11, 2017 at 16:44
  • I decided to do the whole GPIO thing without a library like wiring pi. They all didn't work as expected and I didn't want some update to break the whole system. Also, using GPIO with plain python is not that hard. Find my code for reading a pin here (notice that I used an interrupt instead of looping the request, which should be advantageous in most cases): github.com/speendo/photocell/blob/…
    – speendo
    Commented Apr 13, 2017 at 10:55

1 Answer 1

1

Found the solution myself.

The gpio documentation clearly states

This exports the given pin (BCM-GPIO pin number) as an input or output and makes it available for a user program running as the same user to use.

So to make it work, I had to do the following:

gpio unexport 5
gpio export 24 in
$ gpio exports
GPIO Pins exported:
  24: in   1  none  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.