0

I use Raspberry's GPIO from Bash because it only require 6 lines for what I need: a button and a LED.

I can read an input, and write an output. Very simple.
But I can not find how to active/desactivate the internal pull-up or pull-down resistors.

I searched into the Python library, and found the place where the magic happens. But I'm unable to understand what is done.
Same thing with the wiringPi library (the C code is very similar).

--> what are the low level steps to control the internal pull-up resistors?

UPDATE: I already see an answer with "a library is the only way", and I see similar answers everywhere. But if it can be done inside a library, it probably can be done with Bash (without having to install/compile/whatever a library).

4
  • Add your bash script and relevant links to the libraries your referencing.
    – CoderMike
    Commented Jan 26, 2019 at 22:22
  • Your question is too vague. All bash scripts end up calling some executable code. You could enable on boot - see raspberrypi.org/forums/…
    – Milliways
    Commented Jan 26, 2019 at 23:01
  • "it probably can be done with Bash" -> An incorrect assumption, unless you mean by calling an appropriate third party command.
    – goldilocks
    Commented Jan 27, 2019 at 17:59
  • Sounds like your question could be rephrased as "I found this section of Python/C code in a library but don't understand how it works so as to replicate it in another languge"? Commented Jan 27, 2019 at 18:04

4 Answers 4

3

You need to use a library as there is no sys file system interface.

If you use the pigpio daemon you could use the pigs pud command (man pigs).

If you use wiringPi it is simplest to use its gpio utility (man gpio).

2

You may want to try this. Please note that I've not verified this answer. I'll try to verify it once my DVM is returned:

Assuming GPIO pin4 is the objective pin:

sudo bash  
echo "4" > /sys/class/gpio/export  
echo "1" > /sys/class/gpio/gpio4/active_low  

The default for active_low is 0, which means the pin is pulled up (active low). Setting it to 1 as we've just done means the pin is pulled down (active high).

Please let me know if you have a chance to try this, and if so, whether or not it works. Again, it's not a verified answer as my DVM has been loaned out.

5
  • Already tested: it invert the input of the pin (ie 0 volt give a 1). It is available for every pins, with an internal pull up or a pull down. It also work when using an external pull-up or down resistor. I do not know if this is pure software, or related to the GPIO controler. Commented Jan 26, 2019 at 23:40
  • @GregoryMOUSSAT: Does that mean it worked, or no?? If no, can you elaborate a bit more?
    – Seamus
    Commented Jan 26, 2019 at 23:46
  • It invert the input of the pin (ie 0 volt give a 1). This is not related to any pull-up/down resistor. Commented Jan 27, 2019 at 0:22
  • 1
    @GregoryMOUSSAT: Yes - that is what I'd expect. If you've designated the pin as an input, then you should see voltage at that pin change from logic-high (~3.3 volts) to logic low (~0 volts) when you change from a pullup resistor to a pulldown resistor. If this is what's happening, then I think your question is answered, but if you disagree, let's step through it.
    – Seamus
    Commented Jan 27, 2019 at 0:38
  • 2
    You can't control the internal pulls via the sysfs interface. It's a linux kernel generic GPIO thing.
    – goldilocks
    Commented Jan 27, 2019 at 17:58
2

Looking at gpio sources, the sequence to set pull-up/pull-down is:

 *(gpio + GPPUD)              = pud & 3 ;            delayMicroseconds (5) ;
 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;    delayMicroseconds (5) ;

 *(gpio + GPPUD)              = 0 ;                  delayMicroseconds (5) ;
 *(gpio + gpioToPUDCLK [pin]) = 0 ;                  delayMicroseconds (5) ;

I'm pretty sure you won't get the exact timing with a bash script, but if the timing can be more lax (millisecons), you may have a shot writing to the corresponding addresses with devmem2.

Practically speaking, I would take @joan's advice and install a library though.

-2

Does your software installation include raspi-gpio?

user1@rpi3b2:~$ raspi-gpio help

WARNING! raspi-gpio set writes directly to the GPIO control registers
ignoring whatever else may be using them (such as Linux drivers) -
it is designed as a debug tool, only use it if you know what you
are doing and at your own risk!

The raspi-gpio tool is designed to help hack / debug BCM283x GPIO.
Running raspi-gpio with the help argument prints this help.
raspi-gpio can get and print the state of a GPIO (or all GPIOs)
and can be used to set the function, pulls and value of a GPIO.
raspi-gpio must be run as root.
Use:
  raspi-gpio get [GPIO]
OR
  raspi-gpio set <GPIO> [options]
OR
  raspi-gpio funcs [GPIO]
OR
  raspi-gpio raw
Note that omitting [GPIO] from raspi-gpio get prints all GPIOs.
raspi-gpio funcs will dump all the possible GPIO alt funcions in CSV format
or if [GPIO] is specified the alternate funcs just for that specific GPIO.
Valid [options] for raspi-gpio set are:
  ip      set GPIO as input
  op      set GPIO as output
  a0-a5   set GPIO to alternate function alt0-alt5
  pu      set GPIO in-pad pull up
  pd      set GPIO pin-pad pull down
  pn      set GPIO pull none (no pull)
  dh      set GPIO to drive to high (1) level (only valid if set to be an output)
  dl      set GPIO to drive low (0) level (only valid if set to be an output)
Examples:
  raspi-gpio get              Prints state of all GPIOs one per line
  raspi-gpio get 20           Prints state of GPIO20
  raspi-gpio set 20 a5        Set GPIO20 to ALT5 function (GPCLK0)
  raspi-gpio set 20 pu        Enable GPIO20 ~50k in-pad pull up
  raspi-gpio set 20 pd        Enable GPIO20 ~50k in-pad pull down
  raspi-gpio set 20 op        Set GPIO20 to be an output
  raspi-gpio set 20 dl        Set GPIO20 to output low/zero (must already be set as an output)
  raspi-gpio set 20 ip pd     Set GPIO20 to input with pull down
  raspi-gpio set 35 a0 pu     Set GPIO35 to ALT0 function (SPI_CE1_N) with pull up
  raspi-gpio set 20 op pn dh  Set GPIO20 to ouput with no pull and driving high
1
  • Rather than cut-and-paste of the help text from the command can you show how to answer the question with the command? Commented Jan 29, 2019 at 13:02

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.