in IOT, Linux

Samsung ARTIK 530 GPIO PIN programming

Summary

Tested ARTIK 530 on the GPIO function, implemented the switch to control the LED light on and light off, with successful result.

ARTIK 530 LED and Switch GPIO

For ARTIK 520, the board already implement an LED circuit, so no need to prepare the switch and LED, the GPIO number shows as below,

on-board device sysfs Mapping Notes
SW403 GPIO 30 nearest board edge, next to red LED
SW404 GPIO 32 next to blue LED
Red LED GPIO 28 n.a.
Blue LED GPIO 38 n.a.

On the Artick 530 board Switch and LED looks like below,
GPIO ARTIK 530

Blink LED

Follow this Link, you can use Arduino IDE, Python or C to control the LED.
Below shell command can shows controlling the blue LED (GPIO 38). Blue light will light on and off.

[root@artik ~]# echo 38 > /sys/class/gpio/export
[root@artik ~]# echo out > /sys/class/gpio/gpio38/direction
[root@artik ~]# echo 1 > /sys/class/gpio/gpio38/value
[root@artik ~]# echo 0 > /sys/class/gpio/gpio38/value
[root@artik ~]# echo 38 > /sys/class/gpio/unexport
[root@artik ~]# 

Python code can be downloaded, tested and it's working properly, here I am using c code below to control the Blue light on and off every one second.

/*
 ============================================================================
 Name        : main.c
 Author      : Xiong HuiLin
 Version     :
 Copyright   : No
 Description : Hello World in C
 ============================================================================
 */

#include <stdio .h>

#include <string .h>
#include <stdlib .h>
#include <unistd .h>

#define HIGH 1
#define LOW 0
#define INPUT 1
#define OUTPUT 0

FILE *fp;
FILE *p=NULL;
int pin_input = 38;


/*
 *
 * Print a greeting message on standard output and exit.
 *
 * On embedded platforms this might require semi-hosting or similar.
 *
 * For example, for toolchains derived from GNU Tools for Embedded,
 * to enable semi-hosting, the following was added to the linker:
 *
 * --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lc -lm -lrdimon -Wl,--end-group
 *
 * Adjust it for other toolchains.
 *
 */

int
main(void)
{
  printf("Hello ARM World!" "\n");

  char fName[128];
  //linux equivalent code "echo 139 > export" to export the port
  if ((fp = fopen("/sys/class/gpio/export", "w")) == NULL){
          printf("Cannot open export file.\n");
          exit(1);
      }
      fprintf( fp, "%d", pin_input );
      fclose(fp);
    //  linux equivalent code "echo low > direction" to set the port as an input
        sprintf(fName, "/sys/class/gpio/gpio%d/direction", pin_input);
      if ((fp = fopen(fName, "w")) == NULL){
          printf("Cannot open direction file.\n");
          exit(1);
      }
      fprintf(fp, "out");
      fclose(fp);

    for(int i=0;i&lt;100;i++)
    {
        sprintf(fName, "/sys/class/gpio/gpio%d/value", pin_input);
        p = fopen(fName,"w");
        fprintf(p,"%d",1);
        sleep(1);
        fclose(p);
        sprintf(fName, "/sys/class/gpio/gpio%d/value", pin_input);
        p = fopen(fName,"w");
        fprintf(p,"%d",0);
        sleep(1);
        fclose(p);
    }

    p = fopen("/sys/class/gpio/unexport","w");
    fprintf(p,"%d",pin_input);
    fclose(p);


  printf("End1!!!" "\n");
  return 0;
}

The result shows as below video,

Use Switch to Control LED

Follow this Link to read the Button / Switch. Below c code to read Switch 403 (pressed status) to light on the Red LED, and read Switch 404 (pressed status) to light off the Red LED.

/*
 ============================================================================
 Name        : main.c
 Author      : Xiong HuiLin
 Version     :
 Copyright   : No
 Description : Hello World in C
 ============================================================================
 */

#include <stdio .h>

#include <string .h>
#include <stdlib .h>
#include <unistd .h>
#include <stdbool .h>

#define HIGH 1
#define LOW 0
#define INPUT 1
#define OUTPUT 0

FILE *fp;
FILE *p=NULL;
int pin_input = 38;


/*
 *
 * Print a greeting message on standard output and exit.
 *
 * On embedded platforms this might require semi-hosting or similar.
 *
 * For example, for toolchains derived from GNU Tools for Embedded,
 * to enable semi-hosting, the following was added to the linker:
 *
 * --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lc -lm -lrdimon -Wl,--end-group
 *
 * Adjust it for other toolchains.
 *
 */


bool digitalPinMode(int pin, int dir){
  FILE * fd;
  char fName[128];

  //Exporting the pin to be used
  if(( fd = fopen("/sys/class/gpio/export", "w")) == NULL) {
    printf("Error: unable to export pin\n");
    return false;
  }

  fprintf(fd, "%d\n", pin);
  fclose(fd);   // Setting direction of the pin
  sprintf(fName, "/sys/class/gpio/gpio%d/direction", pin);
  if((fd = fopen(fName, "w")) == NULL) {
    printf("Error: can't open pin direction\n");
    return false;
  }

  if(dir == OUTPUT) {
    fprintf(fd, "out\n");
  } else {
    fprintf(fd, "in\n");
  }

  fclose(fd);
  return true;
}


int digitalRead(int pin) {
  FILE * fd;
  char fName[128];
  char val[2];

  //Open pin value file
  sprintf(fName, "/sys/class/gpio/gpio%d/value", pin);
  if((fd = fopen(fName, "r")) == NULL) {
     printf("Error: can't open pin value\n");
     return false;
  }

  fgets(val, 2, fd);
  fclose(fd);

  return atoi(val);
}


bool digitalWrite(int pin, int val) {
  FILE * fd;
  char fName[128];

  // Open pin value file
  sprintf(fName, "/sys/class/gpio/gpio%d/value", pin);
  if((fd = fopen(fName, "w")) == NULL) {
    printf("Error: can't open pin value\n");
    return false;
  }

  if(val == HIGH) {
    fprintf(fd, "1\n");
  } else {
    fprintf(fd, "0\n");
  }

  fclose(fd);
  return true;
}


int
main(void)
{
  printf("Hello ARM World!" "\n");

  //LED
  //– Red LED: sysfs GPIO 28 on ARTIK 530/710
  //– Blue LED: sysfs GPIO 38 on ARTIK 530/710
  //sysfs GPIO 30 for SW403 (next to board edge, alongside Red LED)
  //sysfs GPIO 32 for SW404 (alongside Blue LED)

  do {
      digitalPinMode(30,INPUT); //INPUT = 1
      printf("digitalPinMode(30,INPUT)!" "\n");
      if (!digitalRead(30))
      {
          digitalPinMode(28,OUTPUT); //OUTPUT = 0
          digitalWrite(28,HIGH); //HIGH = 1
      }

      printf("11111111111111111111111111" "\n");
      digitalPinMode(32,INPUT);
      if (!digitalRead(32))
      {
          digitalPinMode(28,OUTPUT); //OUTPUT = 0
          digitalWrite(28,LOW); //HIGH = 1
      }
      printf("22222222222222222222222222222" "\n");

  }
  while(true);

  printf("End1!!!" "\n");
  return 0;
}

The result is as below video shows,

Reference

Samsung ARTIK Pin Programming
Samsung ARTIK Blink an LED
Samsung ARTIK Reading a Button
Samsung ARTIK Programmable Pins
Samsung ARTIK GPIO Header Maps


Write a Comment

Comment