0

I have been working on a project and everything is working. I just want a bit more insight on what every line of code does. It's one thing to write it, it's completely different to understand it.

import RPi.GPIO as GPIO # I know the import code
import time

# I AM MORE KEEN TO FIND OUT THE NEXT FEW LINES MEANINGS AND WHAT THEY DO,

GPIO.setmode(GPIO.BCM) 
GPIO.setup(4, GPIO.OUT)

GPIO.output(4, GPIO.LOW)
time.sleep(5) # I KNOW THIS 
GPIO.output(4, GPIO.HIGH)
GPIO.cleanup()

print "Done!" # I KNOW THIS
  • It's one thing to write it, it's completely different to understand it. - begs the question "How did you know what to write, if you didn't know what you were doing?" – Greenonline Jul 27 '18 at 0:11
  • this is code that was given as a example that i tested and i understand most of it, just the pi stuff that i did not really understand as that was not explained in detail. – BigBear Aug 15 '18 at 14:44
  • Ah, that makes sense :-) – Greenonline Aug 15 '18 at 14:53
1

The setmode() determines the GPIO numbering scheme to be used in the script (there are at least three different numbering schemes in common usage).

GPIO (General Purpose Input Output) may be selected as inputs or outputs (as well as other more specialised functions). The setup() says GPIO 4 is to be used as an output.

The output() sets the level of the GPIO to on or off (also known as high/low, 1/0, True/False etc.).

The cleanup() switches all GPIO you used back to being inputs. This is thought by some to be safer than leaving them as outputs.

See https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/

| improve this answer | |
  • Thanks Joan, it makes sense now that you explained. i had code with high and low, input and output and it did not make sense. Thanks again. – BigBear Jul 26 '18 at 15:16
1

To see the documentation to these functions you can run

pydoc RPi.GPIO

or import RPi.GPIO and then help(RPi.GPIO).

To actually see what the functions do in the code, from C source code of RPi.GPIO:

  • module_setup(...) is described on L113
  • setup(...) is described on L209
  • setmode(...) is described on L361

  • cleanup(...) is described on L139

  • output(...) is described on L258, L283 and L302

All commands are described on L428:

{"setup", (PyCFunction)py_setup_channel, METH_VARARGS | METH_KEYWORDS, "Set up the GPIO channel, direction and (optional) pull/up down control\nchannel    - Either: RPi board pin number (not BCM GPIO 00..nn number).  Pins start from 1\n                or     : BCM GPIO number\ndirection - INPUT or OUTPUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN\n[initial]        - Initial value for an output channel"},
{"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program\nto INPUT with no pullup/pulldown and no event detection"},
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel"},
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel"},
{"setmode", setmode, METH_VARARGS, "Set up numbering mode to use for channels.\nBOARD - Use Raspberry Pi board numbers\nBCM    - Use Broadcom GPIO 00..nn numbers"},
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.