Dodo
Posts: 6
Joined: Tue Jul 02, 2013 1:53 pm

GPIO pins resetting

Wed Jul 03, 2013 2:21 pm

Hi

I'm trying to get readings from a number of SHT11 sensors and turn on a LED for each sensor that returns a measurement.
The sensors share a common clk on pin 7.
Pins 11, 13 and 15 are dedicated data pins, one for each of the 3 sensors.
Pins 8, 10, 12 are used for LEDs, one for each sensor.

The program works, but acts very odd. It read data from the sensors that are present, but when I try to turn on the corresponding LED it only blinks and doesn't stay lit as it should.

It seems the LED's are only lit until I use oi.output() the next time even if it's for a completely different pin. Why doesn't the pins keep the level I set them at?

My entire program is below.

And a last question.

in the set_light() function I have to include io.setup() for the pin I want or I get an error saying the pin isn't setup even though I do this at the start of the main function. What gives? Python seems to be behaving oddly.

Code: Select all

    from sht1x.Sht1x import Sht1x as SHT1x
    import time
    import RPi.GPIO as io

    def set_light(light):
            io.setup(light, io.OUT)
            io.output(light,1)

    def measure(data, clk, light):
            try:
                    sht1x = SHT1x(data, clk, SHT1x.GPIO_BOARD)
                    temp = sht1x.read_temperature_C()
                    hum = sht1x.read_humidity()
                    dew = sht1x.calculate_dew_point(temp,hum)
                    set_light(light)
                    time.sleep(2)
                    print 'Temp: %f, hum: %f' % (temp, hum)
            except:
                    print 'Sensor not responding'

    if __name__ == '__main__':
            io.setmode(io.BOARD)
            io.setup(8, io.OUT, initial = 0)
            io.setup(10, io.OUT, initial = 0)
            io.setup(12, io.OUT, initial = 0)

            io.output(12,1)
            data = 11
            clk = 7
            light = 8

            for i in range(0,3):
                    print 'Sensor %d' % (i+1)
                    measure(data,clk,light)
                    data = data + 2
                    light = light + 2

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: GPIO pins resetting

Wed Jul 03, 2013 5:06 pm

Do you need to set pins 11, 13 & 15 to inputs? Not done much with GPIO so could be completely wrong but worth a try.

Dave.
Apple say... Monkey do !!

Dodo
Posts: 6
Joined: Tue Jul 02, 2013 1:53 pm

Re: GPIO pins resetting

Wed Jul 03, 2013 5:53 pm

The sht1x module handles that part, and it works as I am reading measurements.

Dodo
Posts: 6
Joined: Tue Jul 02, 2013 1:53 pm

Re: GPIO pins resetting

Thu Jul 04, 2013 8:45 am

I guess it would be good form to answer my own question as I just found the answer.

It turns out (unsurprisingly maybe) that the sht1x methods runs GPIO.cleanup() when they return, thus breaking the settings I had set in the main program. Commenting 2 lines in the source for the sht1x fixed the problem. Ugly fix and if I find I'll be using the SHT1x a lot I'll write a better fix, maybe a toggle to allow GPIO settings to be retained after calling the sht1x-methods if desired.

Now it functions as I expect.

Return to “Python”