In my previous tutorial, I’ve shown you how to configure
digital IO as output. Now, I’ve just want to show you how to configure it
as input.
Following Arduino pin mux and mode settings table (table 3)
from Intel
Edison Kit for Arduino Hardware Guide, we can set digital IO as input. As
an example I’ll show you how to configure pin 12 as a signal input. Here are
the step:
- Set pin 214 to low
- export pin 260, 228 and 42
- Set pin 260 as input (low)
- Set pin 228 to high for pullup enable or low to disable
- Set mode 0 for pin 42 as digital IO
- Set pin 214 to high
Arduino kit have two level signal for GPIO (3.3v and 5v). You can select using jumper J9 (IOREF), by default it sets to 5v. Make sure you select the right level to avoid damage on your board.
Start remote your Intel Edison and logon using SSH tools and
try it:
echo low > /sys/class/gpio/gpio214/direction
echo –n “260” > /sys/class/gpio/export
echo –n “228” > /sys/class/gpio/export
echo –n “40” > /sys/class/gpio/export
echo low > /sys/class/gpio/gpio260/direction
echo low > /sys/class/gpio/gpio228/direction
echo mode0 > /sys/kernel/debug/gpio_debug/gpio40/current_pinmux
echo high > /sys/class/gpio/gpio214/direction
To read signal input value type:
cat /sys/class/gpio/gpio42/value
For configure and reading in java we need to add gpio reading function
(the rest function you’ll find in Intel
Edison Digital IO as Output with Java tutorial). Here is the function:
private static int
gpio_read(int iopin) {
try {
InputStream ioData =
new FileInputStream(_GPIO_IO_VALUE + Integer.toString(iopin) + "/value");
InputStreamReader ioDataRead = new
InputStreamReader(ioData);
return
(ioDataRead.read() ^ 48); // 48 = 0x30 in ascii table for filtering signal
input 1 or 0
}
catch (Exception ex) {
System.out.println("GPIO Read Error: " + ex.toString());
return -1;
}
}
Down below is the complete example by using GPIO pin 13 as
signal input for GPIO pin 12, to see the result link GPIO pin 13 with pin 12
using jumper cable:
package gpiotest;
import java.io.*;
public class Gpiotest {
final static String
_GPIO_EXPORT = "/sys/class/gpio/export";
final static String
_GPIO_UNEXPORT = "/sys/class/gpio/unexport";
final static String
_GPIO_IO_VALUE = "/sys/class/gpio/gpio";
final static String
_GPIO_MODE = "/sys/kernel/debug/gpio_debug/gpio";
final static int
_SHIELD_PIN12 = 42;
final static int
_SHIELD_PIN13 = 40;
public static void
main(String[] args) {
// TODO code application
logic here
int iOutValue = 0;
try {
gpio_write(214, 0);
// set IO pin 13 as
output
gpio_export(261);
gpio_export(229);
gpio_export(_SHIELD_PIN13);
gpio_write(261, 1);
// output
gpio_write(229, 1);
// pull up enable
gpio_digital(_SHIELD_PIN13);
// set IO pin 12 as
input
gpio_export(260);
gpio_export(228);
gpio_export(_SHIELD_PIN12);
gpio_write(260, 0);
// output
gpio_write(228, 0);
// pull up disable
gpio_digital(_SHIELD_PIN12);
gpio_write(214, 1);
while (true) {
if (iOutValue ==
0) {
gpio_write(_SHIELD_PIN13, iOutValue);
iOutValue =
1;
}
else {
gpio_write(_SHIELD_PIN13, iOutValue);
iOutValue =
0;
}
System.out.println("Read IO12: " +
Integer.toString(gpio_read(_SHIELD_PIN12)));
Thread.sleep(1000);
}
}
catch (Exception ex) {
System.out.println("Main App Error: " + ex.toString());
}
}
private static void
gpio_export(int iopin) {
try {
OutputStream ioExport
= new FileOutputStream(_GPIO_EXPORT);
OutputStreamWriter
ioExportWrite = new OutputStreamWriter(ioExport);
ioExportWrite.write(Integer.toString(iopin));
ioExportWrite.close();
}
catch (Exception ex) {
System.out.println("GPIO Export Error: " + ex.toString());
}
}
private static void
gpio_unexport(int iopin) {
try {
OutputStream ioExport
= new FileOutputStream(_GPIO_UNEXPORT);
OutputStreamWriter
ioExportWrite = new OutputStreamWriter(ioExport);
ioExportWrite.write(Integer.toString(iopin));
ioExportWrite.close();
}
catch (Exception ex) {
System.out.println("GPIO Unexport Error: " + ex.toString());
}
}
private static void
gpio_write(int iopin, int value) {
try {
OutputStream
ioDirection = new FileOutputStream(_GPIO_IO_VALUE + Integer.toString(iopin) +
"/direction");
OutputStreamWriter
ioDirectionWrite = new OutputStreamWriter(ioDirection);
if (value == 0) {
ioDirectionWrite.write("low");
}
else {
ioDirectionWrite.write("high");
}
ioDirectionWrite.close();
}
catch (Exception ex) {
System.out.println("GPIO Direction Error: " + ex.toString());
}
}
private static void
gpio_digital(int iopin) {
try {
OutputStream ioMode =
new FileOutputStream(_GPIO_MODE + Integer.toString(iopin) +
"/current_pinmux");
OutputStreamWriter
ioModeWrite = new OutputStreamWriter(ioMode);
ioModeWrite.write("mode0");
ioModeWrite.close();
}
catch (Exception ex) {
System.out.println("GPIO Mode Error: " + ex.toString());
}
}
private static int
gpio_read(int iopin) {
try {
InputStream ioData =
new FileInputStream(_GPIO_IO_VALUE + Integer.toString(iopin) +
"/value");
InputStreamReader
ioDataRead = new InputStreamReader(ioData);
return
(ioDataRead.read() ^ 48); //
48 = 0x30 in ascii table for filtering signal input 1 or 0
}
catch (Exception ex) {
System.out.println("GPIO Read Error: " + ex.toString());
return -1;
}
}
}