This is first tutorial on using the Python programming language on the Intel Edison and Galileo.
In this introductory tutorial, I'll explain what infrastructure is available (libraries for interfacing with the peripherals) followed by an obligatory"hello world"of embedded system which happens to be blinking an LED.
I am assuming here that you have gone through my previous recipes that deal with the getting started guides for the Edison and Galileo as well as setting up (my preferred) development environment using the SSH file mount for writing your applications.
There are two libraries available on the Intel Galileo/Edison for developing applications written in python that need to use sensors, actuators, LEDs etc
If you are using the Galileo, these libraries are by default installed in the “IoT Linux” image.
We are going to blink an LED connected to the port D5 on the Galileo/Edison. Connect the longer of the two pins on the LED to D5 through a current limiting resistor (470 ohms) and the shorter one to the “GND”.
If you are using the Grove kit, then take one of the connectors and connect the one end to the connector labeled “D5” and the other end to the “LED socket” module from the Grove kit. Insert the longer pin of the LED into the connector labeled “+” and the other pin into connector labeled “-” (GND).
Once you have access to the console, you can start your coding. Using your favorite editor (mine is emacs and hence we will go through editing the files with that first).
Fire up the emacs and issue open file chord (ctrl-x ctrl-f) and type the following:
The above syntax is for accessing the remote files (the file that we want to create and edit has to be on Galileo). The emacs is running on your host PC.
or if you want to use vi editor which is installed in the Galileo itself, you need to type “vi ” at the command prompt of the Galileo console and press key ‘i’ to start editing.
Or you can refer my previous recipe on how to mount your board over SSH as a filesystem after that you can use any editor that you want to create and edit your files.
and type in the following code:
#!/usr/bin/pythonimport mraa # For accessing the GPIOimport time # For sleeping between blinks"""This script demonstrates the usage of mraa library for controlling a GPIO as outputsetup:The LED is connected port D5Demo:start the application in the command line by using following command:python blink.pyYou should see LED blink You can exit this demo by hitting ctrl+cLink for this tutorial:https://navinbhaskar.wordpress.com/2015/03/20/python-on-intel-galileoedison-part-1/"""LED_GPIO = 5 # we are using D5 pinblinkLed = mraa.Gpio(LED_GPIO) # Get the LED pin objectblinkLed.dir(mraa.DIR_OUT) # Set the direction as outputledState = False # LED is off to begin withblinkLed.write(0)# One infinite loop coming upwhile True: if ledState == False: # LED is off, turn it on blinkLed.write(1) ledState = True # LED is on else: blinkLed.write(0) ledState = False print"LED is on? \nAns: %s"%(ledState) # Wait for some time time.sleep(1)
To run the code, type in the following in the Galileo/Edison console:
If you have connected the LED to D5, you should nowsee the LED blink. To exit the script any time, hit"ctrl+c"
Now you know what the code does, it is time to know how it does that.
The first thing is to bring in the “mraa” module in. This library let’s us control the GPIO. Next we import the “time” module that will provide us with the “sleep” method to be used for blinking effect.
import mraa # For accessing the GPIOimport time # For sleeping between blinks
Next, we create an instance of the D5 GPIO pin that provides us with the methods to control the selected GPIO pin. In this example we will be using the methods “dir” and “write”
LED_GPIO = 5 # we are using D5 pinblinkLed = mraa.Gpio(LED_GPIO) # Get the LED pin object
Set the direction of the GPIO port we want to control as “OUT” since we want to OUTput the voltage rather than INputing it. The voltage that we output is applied across the LED and this controls the state of that LED.
blinkLed.dir(mraa.DIR_OUT) # Set the direction as output
Then in an infinite loop, the LED is turned on and off depending on the state of the flag “ledState” which is toggled every cycle. To turn on the led the “write” method is used. To “write”, if argument sent is “1” then the high logic level is applied at the selected pin causing the LED to turn on and writing “0” causes the LED to switch off.
while True: if ledState == False: # LED is off, turn it on blinkLed.write(1) ledState = True # LED is on else: blinkLed.write(0) ledState = False
You can check out this git repo for this and other scripts that use Python on Galileo.