John Public
Posts: 90
Joined: Thu Jan 16, 2014 2:16 pm

Having trouble setting GPIO high

Wed Mar 21, 2018 5:26 pm

My i5 is set to the physical pin 40 (GPIO29 or WiringPi pin #29). I have Raspberry Zero and I'd like to set it to high. I have nothing connected to the pin. For some reason the following code odes not work as I'd expect:

Code: Select all

printf("set to%d HIGH\n",i5);      
pinMode(i5,OUTPUT);         
digitalWrite(i5,HIGH);
pinMode(i5,INPUT);
i=digitalRead(i5) ; 
printf(" --> what is it now %d (should be 1)\n",i);      
pinMode(i5,OUTPUT);
pinMode(i5,INPUT);
i=digitalRead(i5) ;  
printf("reading %d and its is now %d\n",i5,i);
pinMode (i5, OUTPUT) ; 
//sleep(1);
pinMode(i5,OUTPUT);      
i=digitalRead(i5) ; 
printf("reading %d and its is now %d\n",i5,i);
pinMode (i5, OUTPUT) ; 
What I get is:

Code: Select all

set to29 HIGH
 --> what is it now 0 (should be 1)
reading 29 and its is now 0
reading 29 and its is now 1
Sometimes (seldom) I get

Code: Select all

set to29 HIGH
 --> what is it now 0 (should be 1)
reading 29 and its is now 1
reading 29 and its is now 1
What could be wrong?

User avatar
joan
Posts: 15363
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Having trouble setting GPIO high

Wed Mar 21, 2018 5:45 pm

The code you posted uses 15.

Expansion header pin 40 is actually GPIO21.

To test try

sudo pigpiod

pigs w 21 0
pigs r 21
pigs w 21 1
pigs r 21

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6051
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Having trouble setting GPIO high

Wed Mar 21, 2018 6:06 pm

joan wrote:
Wed Mar 21, 2018 5:45 pm
The code you posted uses 15.
Expansion header pin 40 is actually GPIO21.

To test try

sudo pigpiod

pigs w 21 0
pigs r 21
pigs w 21 1
pigs r 21
Or rather, i5 which, according to the printf output is 29 (wiringPi 29 == phys. pin 40, BCM 29 is N/A and phys. pin 29 == wiringPi 21/BCM 5). @John Public, are you sure your code is using the wiringPi numbering scheme? As an alternative to @joan's test you could (also) check with wiringPi's gpio command (including gpio readall** perhaps) which supports both wiringPi & BCM "modes".
Trev.
** See here for some examples: http://www.cpmspectrepi.uk/raspberry_pi ... adall.html
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
joan
Posts: 15363
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Having trouble setting GPIO high

Wed Mar 21, 2018 6:21 pm

FTrevorGowen wrote:
Wed Mar 21, 2018 6:06 pm
joan wrote:
Wed Mar 21, 2018 5:45 pm
The code you posted uses 15.
Expansion header pin 40 is actually GPIO21.

To test try

sudo pigpiod

pigs w 21 0
pigs r 21
pigs w 21 1
pigs r 21
Or rather, i5...
Ah, that makes sense now.

PiGraham
Posts: 4449
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Having trouble setting GPIO high

Wed Mar 21, 2018 6:58 pm

John Public wrote:
Wed Mar 21, 2018 5:26 pm

Code: Select all

pinMode(i5,OUTPUT);         
digitalWrite(i5,HIGH);
pinMode(i5,INPUT);
i=digitalRead(i5) ; 
printf(" --> what is it now %d (should be 1)\n",i);      
[/quote]

When you change the pin to input is is no longer an output, so you read the unconnected input state.
If you want to read back the output state just leave the pin as an output:

[code]pinMode(i5,OUTPUT);         
digitalWrite(i5,HIGH);
i=digitalRead(i5) ; 
printf(" --> what is it now %d (should be 1)\n",i);      
[/quote]

John Public
Posts: 90
Joined: Thu Jan 16, 2014 2:16 pm

Re: Having trouble setting GPIO high

Thu Mar 22, 2018 8:21 am

I am a bit confused. Dropping all pinMode(nnn,i) -functions appeared to fix everything. What is the point of having them if it is more consistent to just read and write and forget about modes?
Is there some caveats? Is there a way to check whether a pin is "read"-mode or "write"-mode?

PiGraham
Posts: 4449
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Having trouble setting GPIO high

Thu Mar 22, 2018 8:33 am

John Public wrote:
Thu Mar 22, 2018 8:21 am
I am a bit confused. Dropping all pinMode(nnn,i) -functions appeared to fix everything. What is the point of having them if it is more consistent to just read and write and forget about modes?
Is there some caveats? Is there a way to check whether a pin is "read"-mode or "write"-mode?
Because you have to choose the function of the pin. Either it outputs the state you choose (output) or it doesn't (input)
In either case you can read the level.

If the pin is set as input you can't control it's high/low state with a write function. Think of the mode as setting a direction of an arrow for which way the signal goes, set from inside to out, or set from outside to in.

There are ways to check the mode, WiringPi gpio readall does that, for one, but your own code can track what mode it sets so you don't need to read it.

PiGraham
Posts: 4449
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Having trouble setting GPIO high

Thu Mar 22, 2018 8:37 am

John Public wrote:
Thu Mar 22, 2018 8:21 am
I am a bit confused. Dropping all pinMode(nnn,i) -functions appeared to fix everything.
You need to set mode to output to control the pin level high or low. Otherwise the pin voltage won't change.
Almost all gpio default to inputs at boot.

PiGraham
Posts: 4449
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Having trouble setting GPIO high

Thu Mar 22, 2018 9:22 am

This equivalent circuit might help to understand what's going on.
Image

http://www.mosaic-industries.com/embedd ... ifications

Setting mode to output will set the Output Enable so that the output register can drive the gpio pin. Read always reads the line labelled input, whether the output enable is set or not.

Return to “General discussion”