2

I am trying to trying to control the GPIOs on my RPi with Python but without one of those modules that require root permissions to be used. I found that trivial way of just "echoing" 1 or 0 to the value file of the GPIO through the console. I also wrote a Python script doing this automatically. So far so good. But this only works if the file (in this case: /sys/class/gpio/gpio17) already exists. I could easily create this folder everytime after booting the Pi, through the console like this:

pi@raspberrypi ~ $ cd /sys/class/gpio
pi@raspberrypi /sys/class/gpio $ echo "17" > export

After this I will have the folder /sys/class/gpio/gpio17 in which I find the information about the pin and can modify it.

So basically my question is: How would I incorporate this step into my Python script. Is there a way to move to the directory and export this folder using Python? I figured it out using a seperate Bash script whitch I then run inside my Python script, but I'd like to avoid using several languages and was wondering if there was an equivalent to the "echo" comand in Python. I searched for some days now but couldn't find anything. If there is, a link would be highly appreciated as well!

Thank You in advance,

a newbie

5
  • 1
    cd doesn't create a directory. It just changes the working directory of the shell to that directory. That won't work if the directory doesn't exist. You also don't need to be in the directory to echo to a file in the directory (unless something odd is going on here). Commented May 6, 2015 at 12:54
  • 1
    If you're using Python, wouldn't it be easier to just use the RPi.GPIO API that was written for this purpose rather than the sysfs? Commented May 6, 2015 at 12:55
  • Your second line would create a file called "export" containing the string "17". It does not do anything to create a directory, especially not one called "gpio17", and the file created would not have any relationship with the gpio pin. Commented May 6, 2015 at 12:56
  • @DanielRoseman It does, actually. /sys/class/gpio/export is not a regular file. Writing 17 to it will expose pin 17 through the sysfs. Commented May 6, 2015 at 12:57
  • Fixed the comand sample. Commented May 6, 2015 at 12:59

2 Answers 2

5

From your description you should simply need to write the required number to the file named /sys/class/gpio/export.

GPIO_EXPORT = '/sys/class/gpio/export'
PIN = 17
with open(GPIO_EXPORT, 'w') as export:
    export.write(str(PIN))
Sign up to request clarification or add additional context in comments.

Comments

0

there a re a lot of options. you can directly use bash syntax in your python code with os module (or more flexible - subprocess module).

start python interpreter in your working directory and,

>>> import os
>>> os.system("echo 18>import")
0
>>> exit()
$ ls -a
.  ..  import
$ cat import
18

or write file.

>>> with open("newport", "w") as newport:
...     newport.write("19")
... 
>>> exit()
$ ls -a
.  ..  import  newport
$ cat newport 
19

and so on. have a look up to original documentation

4 Comments

Yes, finally the first idea did it for me, using the os module worked. thanks!
the second one actually just created a folder and wrote 17 inside.
@apaupau folder or file?
created a file, pardon

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.