Samsung ARTIK 530 GPIO PIN programming

本文介绍如何使用ARTIK530板载GPIO接口实现LED闪烁及通过开关控制LED的亮灭。文中提供了详细的步骤说明及C语言代码示例,包括设置GPIO为输出模式来控制LED、读取开关状态并据此改变LED状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 devicesysfs MappingNotes
SW403GPIO 30nearest board edge, next to red LED
SW404GPIO 32next to blue LED
Red LEDGPIO 28
Blue LEDGPIO 38

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

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<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,

src="https://www.youtube.com/embed/Ohg8U74f5uU" allowfullscreen="" width="560" height="315">

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,

src="https://www.youtube.com/embed/mAZByDFkiKk" allowfullscreen="" width="560" height="315">

Reference

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值