Quantcast
Channel: LinkSprite Learning Center
Viewing all articles
Browse latest Browse all 562

Relay

$
0
0

This demo is used to control relay, when turn on the delay, a LED on the module will light on; when turn off relay,  LED will also light off.

  1. 96board
  2. Liner Base Mezzanine Card
  3. Relay module of Linker kit
  4. 4-pin cable x 2

Connect

Connect relay module to D2 port.

6

 

#include <stdio.h>
#include <stdlib.h>

#include "libsoc_gpio.h"
#include "libsoc_debug.h"

#define GPIO_CS 13

int main()
{
    gpio *gpio_cs;  
    libsoc_set_debug(1);
    gpio_cs = libsoc_gpio_request(GPIO_CS,LS_SHARED);
    if(gpio_cs == NULL)
    {
	goto fail;
    }
    libsoc_gpio_set_direction(gpio_cs,OUTPUT);
    if(libsoc_gpio_get_direction(gpio_cs) != OUTPUT)
    {
	printf("Failed to set direction to OUTPUT\n");
	goto fail;
    }
    while(1)
   {
	libsoc_gpio_set_level(gpio_cs,HIGH);
 	usleep(1000000);
	libsoc_gpio_set_level(gpio_cs,LOW);
	usleep(1000000);
   }
   fail:
   if(gpio_cs)
   {
	libsoc_gpio_free(gpio_cs);
   }
   return EXIT_SUCCESS;
}
gcc gpio_test.c -o gpio_test -lsoc
sudo ./gpio_test

<img class="alignnone" src="http://learn.linksprite.com/wp-content/uploads/2015/12/6-1.png" alt="6-1" />

 

6-2

 

Every second the relay will change its state, also the LED will blink.

 

6-3

 

 

 

 


Viewing all articles
Browse latest Browse all 562

Trending Articles