Help for first script for GPIO

Fabio
Fabio New Member Posts: 22
Hello
I create the my first file *.py but I'm not very practical about linux.
I'm looking at Raspberry using an idle program to write and start scripts from a graphical interface. How do I start my script * .py?
Thank you

Comments

  • eduncan911
    eduncan911 Administrator, Moderator Posts: 157 admin
    edited October 2017
    If you are trying to run a python script under linux, there's a few things you need to know.

    I'd first recommend going through some simple tutorials on how to install Python and test its installation, alongside your first python Hello World:

    https://realpython.com/learn/python-first-steps/

    That should get you up to speed on what the "python" command does, and how commands within the command line. There's also more advanced information down further on that page.

    Once that is confirmed and Python is working, then you can run your script with: $ python myapp.py

    But please go through the link first and learn differences of 2.7 and 3.5, and know which one you are using and coding for.

    Eric Duncan - UP Evangelist - My thoughts are of my own free will

    Answered? Please remember to mark the posted answered to highlight it for future visitors!

  • WereCatf
    WereCatf New Member Posts: 201
    ninoo, if you are using Ubilinux or any Debian/Ubuntu-based distro, you can install idle or idle3, either through a package-manager or from a terminal with the command
    sudo apt -y install idle
    
    or
    sudo apt -y install idle3
    
    if you wish to use Python 3.x. Idle works just the same as it works on the RPi.
  • Fabio
    Fabio New Member Posts: 22
    Thanks @eduncan911 and @WereCatf for information .@WereCatf was just the program I was looking for.
    I ask you experts but in Linux the GPIO can only be used using Python?
    I'm looking for the most easy and complete system to manage my up2 squared board GPIO with Pentium ™ N4200 2.5 GHz
    Using Windows I have seen that there are many drive issues and control is very limited.
    Up2 Squared board with Pentium ™ N4200 2.5 GHz has any limitations like GPIO?
  • WereCatf
    WereCatf New Member Posts: 201
    You can use GPIO with C/C++ via libmraa or libsoc, or you can use the sysfs-interface in shell-scripts and so on, ie. the GPIO can be used in a rather wide range of methods. As for limitations: you can't use internal pull-up/pull-down resistors, you'll have to use external ones. Also, the GPIO can only handle 8mA current, if my memory serves.
  • Fabio
    Fabio New Member Posts: 22
    ok thanks, but i do not understand linux and gpio very well.
    I did a basic test on wiki but mistaken me ....
    I know that for you is a trivial question, but for me it's all new
  • WereCatf
    WereCatf New Member Posts: 201
    I'm not trying to be condescending, I do understand that you're new to all this and it's frustrating when something doesn't work. That said, the picture you attached is too small, I can't read the error message you're getting.
  • Fabio
    Fabio New Member Posts: 22
    Hi @WereCatf, thank you for always answering my questions.
    I just want to understand how by entering a python script I can turn on a led. If I can do this then all the circuits follow them quietly.
    I have recreated the first example
    https://up-community.org/wiki/RPi.GPIO

    1)I have inserted this script into a py file extension.

    import RPi.GPIO as GPIO
    import time

    # Pin Definitons:
    ledPin = 4

    # Pin Setup:
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(ledPin, GPIO.OUT) # LED pin set as output

    print("Here we go! Press CTRL+C to exit")
    try:
    while 1:
    GPIO.output(ledPin, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(ledPin, GPIO.LOW)
    time.sleep(0.5)
    except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
    GPIO.cleanup() # cleanup all GPIO

    2)Created circuit with resistance and led
    3)Using the "Idle" program open the file with py extension
    4) Run--> Run Module

    Thanks for the support

    The result :
    Is error .
    1.jpg 68.3K
    2.jpg 79.8K
  • Drew
    Drew New Member Posts: 6
    Looks like you have a permissions error, you need to be root superuser to control GPIO. See your error "Unable to export GPIO. Try running as root"

    Before running your python, you can change to root user by entering this command : sudo -i

    As a possible alternative to Python, I tested GPIO on my UpSquared vis Sysfs, referencing this Intel doc for numbering: http://iotdk.intel.com/docs/master/mraa/up2.html

    Then executed the commands from the wiki, with pin 16 being Sysfs GPIO 471

    sudo -i
    cd /sys/class/gpio
    echo 471 > export
    cd gpio471
    echo "out" > direction
    watch -n 0.5 'echo 1 > value; sleep 0.5 ; echo 0 > value'

    This got my attached LED blinking, unfortunately I can't find a way to do this from Windows 10 , but that's another topic!
  • WereCatf
    WereCatf New Member Posts: 201
    ninoo, your problem is that, by default, access to the GPIO is only available to root user. It's a security-thing. It is totally possible to fix and I have written a guide on these forums on how to do it already two months ago and Emutex/Aaeon promised to put the info in their wiki, but... well, they seem not to be doing a lot of the stuff they promise. :angry: This fix is similar to how it's done on the Raspberry Pi, too, but there it's already done for you. With Ubilinux you have to do it yourself.

    Anyways, GPIO without root should help you get going. The files I reference there, like e.g. /etc/udev/rules/50-spi.rules can only be created by root, so you have to use either
    sudo nano /etc/udev/rules/50-spi.rules
    
    or create 50-spi.rules as normal user in your home-directory and copy the file to the correct place with
    sudo cp 50-spi.rules /etc/udev/rules/50-spi.rules
    
  • DCleri
    DCleri Administrator, AAEON Posts: 1,213 admin
    Hi WereCatf,

    I am sorry if we didn't address your request before, so thanks for the reminder and for your contributions!
    You can now edit and add pages in our UP Wiki so you could easily share your knowledge with the rest of the Community.

    Please keep us reminding stuff if that might happen in the future. We are doing our best to keep our promises ;)
  • GwynEaves
    GwynEaves New Member Posts: 1

    Hi...i am creating a cable continuity tester. I am sending 5v (I can add a regulator to slim that down to 3.3v) down one side and if it reaches the other end I want to change my LED light from red to green. Does that make more sense/Is that something I can do with a Raspberry Pi/Will I need an ADC?

Privacy Policy