4. Intel Edison I/O Programming using Python
4.3 Analog I/O - Reading Analog Input
We can read analog input using mraa library. For illustration, we use the same scenario from section 3.5 which uses Potentiometer as analog input source.
Connect Potentiometer pins to VCC and Vout to Analog input A0 on Intel Edison board.
The following is hardware implementation.
Mraa library provide a class, Aio, to manipulate analog processing. To read analog input, we can use Aio.read() with specific analog pin.
For illustration, we create a file, called analog_read.py, and write this code.
import time import mraa
a0 = mraa.Aio(0) a0.setBit(10) try:
while True:
val = a0.read()
print "analog val: " + str(val) time.sleep(2)
except KeyboardInterrupt:
print "done"
Save this code and try to execute by typing the following command.
$ python analog_read.py
If success, you can see analog value in Terminal. Try to change Potentiometer value so you get a new analog value from Intel Edison board.
4.4 UART
In this section we learn how to access UART on Intel Edison board. By default, Inte Edison has six UART on the board. You can check them by typing the following command.
$ ls /dev/tty*
A sample output of command response can be seen in Figure below.
The following is a list of Intel Edison UART which we can use.
/dev/ttyGS0 is be connected to Micro USB Client which is connected to PC
/dev/ttyMFD1 is be connected to Arduino pins, digital pin 0 (Rx) and digital pin 1( Tx) /dev/ttyMFD2 is be connected to Micro USB Client which is connected to PC for debugging
For accessing UART on Arduino pins from Linux, I used pyserial library, http://pyserial.sourceforge.net . Furthermore, you can install it via easy_install. The following is a command for installing pyserial.
$ easy_install -U pyserial
If you don't install easy_install yet, you can install by typing the folllowing command.
$ wget https://bootstrap.pypa.io/ez_setup.py --no-check-certificate -O - | python
A sample output of installation of pyserial can be seen in Figure below.
For testing, I use a UART on /dev/ttyMFD2. We need a micro USB cable which is connected to a computer.
Next step is to write program using Python. Create a file, called uart.py, and write this code.
import serial import time
serialport = '/dev/ttyMFD2' print 'open ' + serialport
ser = serial.Serial(serialport, 115200) counter = 0
while True:
try:
print 'writing: ' + str(counter) ser.write(str(counter))
print(counter) counter += 1 if counter > 10:
counter = 1 time.sleep(1)
except (KeyboardInterrupt, SystemExit):
ser.close() raise
print "done"
To write data to UART, we can use serial.write() from pyserial library. Otherwise, we can use serial.read() to read incoming data from UART. Save this code and try to execute it to Intel Edison terminal.
$ python uart.py
This program will send data from 0 to 10 and back to 1. On local computer, we use PuTTY to read data on Serial. Firstly, check which a serial port is used via Device Manager if you are working on Windows platform, for instance, COM6.
Then, you put COM6 to PuTTY.
If you are working on Linux, you can use miniterm. You can see a sample output in Figure below.
On Intel Edison, the following is program output.
4.5 SPI
We can verify installed SPI on Intel Edison by the following command.
$ ls /dev/spi*
You can see my SPI on Intel Edison is installed on /dev/spidev1.5 .
For illustration, we build a program to write and read data to/from Intel Edison SPI. We build a SPI loopback which digital pin 11 (MOSI) is be connected to digial pin 12 (MISO). Hardware
implementation can be seen in Figure below.
We will use mraa library to access SPI. To write data to SPI, we can use Spi.write(). Then, we receive incoming data from returning Spi.write().
Create a file, called spi.py, and write this code.
import time import mraa
spi = mraa.Spi(5) counter = 100 while True:
try:
n = len(str(counter))
recv = spi.write(str(counter)) print recv[:n]
time.sleep(1) counter += 1
if counter > 110:
break
except (KeyboardInterrupt, SystemExit):
raise print "done"
This code will send counter data which is started from 100. If it reaches counter 110+, it will be stopped.
Now you can run it by typing the following command.
$ python spi.py
A sample output can be seen in Figure below.
4.6 I2C/TWI
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s to allow easy
communication between components which reside on the same circuit board. TWI stands for Two Wire Interface and for most marts this bus is identical to I²C. The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues related to I²C.
Intel Edison has two I2C interfaces which are connected via I2C1 on Analog A4 (SDA) and A5 (SCL) pins and I2C6 on Digital SDA and SCL pins. We can obtain a list of installed I2C on Intel Edison using i2cdetect with passing -l parameter.
Typing the following command.
$ i2cdetect -l
If success, you see a list of installed I2C as below.
Analog A4 (SDA) and A5 (SCL) pins are represented as i2c-1. For illustration, we will build I2C application. We need an Arduino board as I2C source. I use Arduino Uno.
For wiring scenario, we implement the following hardware schema:
Arduino A4 (SDA) to Intel Edison A4 (SDA) Arduino A5 (SCL) to Intel Edison A5 (SCL) Arduino GND to Intel Edison GND
You can see the hardware implementation in Figure below.
The first step is to build a program for Arduino Uno. We define I2C address 0x15. Arduino will send a random number to requester on I2C channel.
We create a program for Arduino, called arduino_i2c_write, and write this code.
#include <Wire.h>
const byte SLAVE_ADDRESS = 0x15;
int led = 13;
byte x;
// source:
// http://forum.arduino.cc/index.php?topic=197633.0 byte randomDigit() {
unsigned long t = micros();
byte r = (t % 10) + 1;
for (byte i = 1; i <= 4; i++) { t /= 10;
r *= ((t % 10) + 1);
r %= 11;
}
return (r - 1);
}
void sendData(){
byte data = randomDigit();
Wire.write(data);
Serial.print("send: ");
Serial.println(data,DEC);
}
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(sendData);
}
void loop() { delay(100);
}
In this program, we use Wire library, http://arduino.cc/en/reference/wire, to implement I2C on Arduino.
Now you compile and deploy it to Arduino board. Then, you can connect your Arduino to Intel Edison via I2C wiring.
The next step is to write a program for Intel Edison using Python. We will read data on I2C channel with address 0x15. The data will generated by Arduino.
Create a file, called i2c.py, and write the following code.
import time import mraa
i2c = mraa.I2c(1)
i2c.address(0x15) # change it based on I2C on Arduinop counter = 0
while True:
try:
data = i2c.read()
print ">>> " + str(data) time.sleep(1)
counter += 1 if counter > 10:
break
except (KeyboardInterrupt, SystemExit):
raise print "done"
Save this code and try to run on Intel Edison Terminal.
$ python i2c.py
If success, you can see the data from I2C channel.
You also can verify on Arduino using Serial Monitor. Make sure your Arduino is connected to PC so you can see data using Serial Monitor tool.