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

Photoresistor

$
0
0

This demo will tell you to how to use photoresistor to control LED, if the environment is too dark, the LED will turn on.

  1. 96board
  2. Liner Base Mezzanine Card
  3. Photoresistor module of Linker Kit
  4. LED module of Linker kit
  5. 4-pin cable x 2

Connect

Connect the photoresistor module to ADC1 port on Linker Base Mezzanine Card using 4-pin cable. This let SIG interface of photoresistor module connect to A0 port. Then, connect LED module to D2 port.

7

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <linux/spi/spidev.h>

#include <linux/types.h>

#include <inttypes.h>

 

#include “libsoc_spi.h”

#include “libsoc_gpio.h”

#include “libsoc_debug.h”

 

#define GPIO_CS 18

#define GPIO_LED 13

 

static uint8_t tx[3],rx[3];

 

int main()

{

int adc_value;

gpio *gpio_cs,*gpio_led;

libsoc_set_debug(0);

gpio_cs = libsoc_gpio_request(GPIO_CS,LS_SHARED);

gpio_led = libsoc_gpio_request(GPIO_LED,LS_SHARED);

if((gpio_cs == NULL) || gpio_led == NULL)

{

goto fail;

}

libsoc_gpio_set_direction(gpio_cs,OUTPUT);

libsoc_gpio_set_direction(gpio_led,OUTPUT);

if((libsoc_gpio_get_direction(gpio_cs) != OUTPUT)

|| libsoc_gpio_get_direction(gpio_led) != OUTPUT)

{

printf(“Failed to set direction to OUTPUT!\n”);

goto fail;

}

spi *spi_dev = libsoc_spi_init(32766,0);

 

if(!spi_dev)

{

printf(“Failed to get spidev device!\n”);

return EXIT_FAILURE;

}

libsoc_spi_set_mode(spi_dev,MODE_0);

libsoc_spi_get_mode(spi_dev);

libsoc_spi_set_speed(spi_dev,10000);

libsoc_spi_get_speed(spi_dev);

libsoc_spi_set_bits_per_word(spi_dev,BITS_8);

libsoc_spi_get_bits_per_word(spi_dev);

 

tx[0] = 0x01;

tx[1] = 0x80;

tx[2] = 0x00;

while(1)

{

libsoc_gpio_set_level(gpio_cs,HIGH);

usleep(10);

libsoc_gpio_set_level(gpio_cs,LOW);

libsoc_spi_rw(spi_dev,tx,rx,3);

libsoc_gpio_set_level(gpio_cs,HIGH);

 

adc_value = (rx[1]<<8)&0b1100000000;

adc_value |= (rx[2]&0xff);

printf(“adc_value:%d\n”,adc_value);

if(adc_value == 0)

{

libsoc_gpio_set_level(gpio_led,HIGH);

}

if(adc_value <= 1023)

{

libsoc_gpio_set_level(gpio_led,LOW);

}

sleep(1);

}

free:

libsoc_spi_free(spi_dev);

fail:

if(gpio_cs)

{

libsoc_gpio_free(gpio_cs);

libsoc_gpio_free(gpio_led);

}

return EXIT_SUCCESS;

}

gcc spi_test.c -o spi_test -lsoc
sudo ./spi_test

The program will read the voltage of photoresistor and display on the terminal, if the value change to 0 when you cover photoresistor with something, the LED will light.7-1 7-2

 

 

 

 


Viewing all articles
Browse latest Browse all 562

Trending Articles