element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Atmel Xplained Boards
  • Products
  • Dev Tools
  • Atmel Xplained Boards
  • More
  • Cancel
Atmel Xplained Boards
Blog sama5d3 xplained violet - GPIO control using Python
  • Blog
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Atmel Xplained Boards requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
Author: violet
Date Created: 6 Oct 2014 9:42 AM
Views: 90
Likes: 0
Comments: 0
Related
Recommended

sama5d3 xplained violet - GPIO control using Python

violet
violet
6 Oct 2014

This blog post is about controlling the digital io pins of the xplained board using Python.

 

My previous blog showed how to control the pins using standard Bash commands in the system shell / terminal. The Python method works along exactly that principle, for the time being we are going to use the standard os library in Python and use os.system() to export Bash commands to run in a subshell. In short, we can just use the Bash commands that we used before directly in our Python scripts.

 

For the time being, I'm going to test the functionality in the live Python interpreter, those of you familiar with Python or have read up a getting started tutorial will know that later on we can save python scripts in files and run them as a normal program.

 

You need to be at the Xplained command prompt either using the usb method from the getting started guide or by following my SSH blog. Once there just type "python" and the interpreter should startup in live mode and the prompt changes to 3 greater than arrows ">>>".

 

Start by importing the os library.

import os

 

Then use the os library to issue a bash command in a subshell and get the directory listing of /sys/class/gpio

 

os.system("ls /sys/class/gpio")

 

If you followed the GPIO Bash blog that I did previously, you can see that we are issuing the same command, only passing it into the os.system function instead of writing it straight out. In this way, we can issue all of the commands just like before and control the direction and value of the IO pin.

 

We can also do something like os.system("python my_script.py") to execute another script which can be useful to us.

 

I'm in the process of creating a simple GPIO library that includes the basics aswell as creating state change watches, Much of the time, especially with button pushes, were really not too concerned with the state of an input pin, rather we just want something to happen when it's state changes from high to low or low to high depending. The library is eventually going to be able to watch pin states in a seperate thread and either run a callback function if the pin changes or execute another script altogether.

 

Once I'm happy with a basic library, I'll post a link for it to be downloadable and write a blog on how to use it. Then it's onto LCD control, I really want a splash screen once the Xplained is booted up!

Anonymous
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube