pi-gpio icon indicating copy to clipboard operation
pi-gpio copied to clipboard

Read operation toggles GPIO port value to 0 during read operation.

Open ravimindstix opened this issue 10 years ago • 4 comments

While using the library i found out that the read operation toggles the high value of gpio port to low. I looked further in the issue, it seems that when we want only read operation , we open the port but this sets the direction to OUT by default. This causes the automatic toggle of gpio port value from 1 to 0 . I tested the issue with gpio-admin commands and when i just export the port and read the value and close port, it works fine. The moment i set the direction after exporting and try to read to the gpio port value, it changes the value of the port to 0 somehow.

The issue occurs when we try to read the port with value high or 1. It doesn't affect ports with value already 0.

ravimindstix avatar Apr 21 '14 12:04 ravimindstix

Hey @ravimindstix. It sounds like you want to set the direction to in to avoid this issue. Does that seem to work?

rakeshpai avatar Apr 21 '14 13:04 rakeshpai

Hey Rakesh,

I tried all the possibilities it still turn the value of port to 0. If you use gpio-admin commands to to read the way you have written your library it will generate the error. The way i checked it using gpio-admin is ,

export port, set direction , read port , close . this way it generates the error .

if you do gpio-admin export read port and close port, it works fine.

and in your library in open function you set the direction and read function is separate. so by default it follows the first approach i mentioned which generates the error.

I guess it will be good if after open we have an option to call set direction or not , reason being read operation only read the value of the port irrespective of the port direction. so we don't need the set direction operation.

i guess the bug is in gpio-admin module.

Thanks & Regards Ravi

On Mon, Apr 21, 2014 at 6:42 PM, Rakesh Pai [email protected]:

Hey @ravimindstix https://github.com/ravimindstix. It sounds like you want to set the direction to in to avoid this issue. Does that seem to work?

Reply to this email directly or view it on GitHubhttps://github.com/rakeshpai/pi-gpio/issues/16#issuecomment-40934248 .

ravimindstix avatar Apr 22 '14 05:04 ravimindstix

Also having this issue, every time I gpio.read I need to be inside of gpio.write like so..

gpio.open(16, "output", function(err) {     // Open pin 16 for output
    gpio.write(16, 1, function() {          // Set pin 16 high (1)
    gpio.read(16, function(err, value) {
        if(err) throw err;
        console.log(value); // The current state of the pin
        gpio.close(16);
    });                    // Close pin 16
    });
});

or have a setTimeout of 600 or greater to avoid gpio-admin: could not flush data to /sys/class/gpio/*: Device or resource busy.

Either way I still end up broken as gpio.read with input set seems to toggle the value to 0 at gpio.close in all cases, which actually makes sense, but I always get a 0 when reading on output.. A method to get reliable output time after time is below but it intermittently changes the value so for the majority of projects this is pointless.

gpio.open(16, "input", function(err) {     // Open pin 16 for output
    gpio.read(16, function(err, value) {
        if(err) throw err;
        console.log(value); // The current state of the pin
        gpio.close(16);
        gpio.open(16, "output", function(err) {     // Open pin 16 for output
            gpio.write(16, 1, function() {          // Set pin 16 high (1)
                gpio.close(16);                     // Close pin 16
            });
        });
    });
});

This all seems a lot messier than gpio.read should be, hopefully I'm missing something.

8eecf0d2 avatar Aug 11 '14 01:08 8eecf0d2

I had the same problem while driving a relay board. Try to put a (pull down) resistor between the pin and the ground. Begin at 1KOhm and go lower. This can't do any harm so just try it ;-) Hope this helps you. I've spend much much time until I solve this...

emnik avatar Dec 15 '15 00:12 emnik