삼성 아틱 조도센서를 이용한 LED 밝기 조정하기

메이커 2018-09-11 (화) 09:23 5년전 2983  

조도센서를 이용한 LED 밝기 조정하기


본 튜토리얼의 전체 PDF는 다음의 링크에서 참고하실 수 있습니다. 

 

https://goo.gl/JVMp9q 

 


아두이노 IDE 사용

1.    OTG, Debug 케이블 연결 후

2.    VCC-5V, GND-GND, SIG-ADC1

3.    Putty 접속 후, 터미널에서 /root/watcher 입력

4.    아두이노에서 삼성 아틱의 COM포트 설정

5.    소스코드 업로드

 

 

#include <DebugSerial.h>

int sensorpin = 1;

void setup()

{

   DebugSerial.begin(115200); 

}

 

void loop()

{

   DebugSerial.println(analogRead(sensorpin));

   delay(1000);

}

 

 

6.    Putty로 모니터링

1f7c3ee5d7e718d5cf0b1a77c77892dc_1536644
 

 

C언어 사용시

 


 

 #include <stdio.h>

 

#include <stdlib.h>

 

#include <stdbool.h>

 

 

 

#define HIGH            1

 

#define LOW 0

 

 

 

#define ANALOG_PIN          0 // A0

 

#define PWM_PIN 0

 

 

 

#define PERIOD 1000000

 

 

 

bool pwmPin(int pin, int period, int duty_cycle, int enable){

 

    FILE * fd;

 

    char fName[128];

 

 

 

    if(( fd = fopen("/sys/class/pwm/pwmchip0/export", "w")) == NULL) {

 

        printf("Error: unable to export pin\n");

 

        return false;

 

    }

 

    fprintf(fd, "%d\n", pin);

 

    fclose(fd);

 

 

 

    sprintf(fName, "/sys/class/pwm/pwmchip0/pwm%d/period", pin);

 

    if((fd = fopen(fName, "w")) == NULL) {

 

        printf("Error: can't open pin period\n");

 

        return false;

 

    }

 

    fprintf(fd, "%d\n", period);

 

    fclose(fd);

 

 

 

    sprintf(fName, "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pin);

 

    if((fd = fopen(fName, "w")) == NULL) {

 

        printf("Error: can't open pin duty_cycle\n");

 

        return false;

 

    }

 

    fprintf(fd, "%d\n", duty_cycle);

 

    fclose(fd);

 

          

 

    sprintf(fName, "/sys/class/pwm/pwmchip0/pwm%d/enable", pin);

 

    if((fd = fopen(fName, "w")) == NULL) {

 

        printf("Error: can't open pin enable\n");

 

        return false;

 

    }

 

    if(enable == HIGH) {

 

        fprintf(fd, "1\n");

 

    }

 

    else {

 

        fprintf(fd, "0\n");

 

    }

 

    fclose(fd);

 

 

 

    return true;

 

}

 

 

 

int analogRead(int pin) {

 

           FILE * fd;

 

           char fName[64];

 

           char val[8];

 

 

 

           // open value file

 

           sprintf(fName, "/sys/devices/12c0000.adc/iio:device0/in_voltage%d_raw", pin);

 

 

 

           if((fd = fopen(fName, "r")) == NULL) {

 

                       printf("Error: can't open analog voltage value\n");  

 

                       return 0;

 

           }         

 

 

 

           fgets(val, 8, fd);

 

           fclose(fd);

 

 

 

           return atoi(val);

 

}

 

 

 

int Con_duty(int duty) {

 

           pwmPin(PWM_PIN, PERIOD, duty, 1);

 

}

 

 

 

int main(void)

 

{

 

    long SenVal = 0;

 

 

 

    while(1)

 

    {

 

        SenVal = analogRead(ANALOG_PIN);

 

        printf("val : %d\n", SenVal);

 

                      

 

                       if (SenVal > 500) Con_duty(100000); // LED ON

 

                       else if (SenVal <= 500) Con_duty(0); // LED OFF

 

                       sleep(1);

 

    }

 

           return 0;

 

}

 

파이썬 사용시

 

1f7c3ee5d7e718d5cf0b1a77c77892dc_1536644
 

 


 from time import sleep

 

OUTPUT = 'out'

INPUT = 'in'

HIGH = 1

LOW = 0

 

def export(pin):

    try:

        exp = open('/sys/class/gpio/export','wb',0)

        exp.write(str(pin))

        print('Registion Successful: pin%d' %pin)

    except IOError as e:

        print('Error: %d - %s' %(e.errno,e.strerror))

 

def unexport(pin):

    try:

        uexp = open('/sys/class/gpio/unexport','wb',0)

        uexp.write(str(pin))

        print('Unregistion Successful: pin%d' %pin)

        uexp.close()

    except IOError as e:

        print('Error: %d - %s' %(e.errno,e.strerror))

 

def pinMode(pin, mode):

    try:

        direction = open('/sys/class/gpio/gpio%d/direction' %pin,'wb',0)

        direction.write('out' if mode == OUTPUT else 'in')

        direction.close()

    except IOError as e:

        print('Error: %d - %s' %(e.errno,e.strerror))

 

def digitalWrite(pin,value):

    try:

        val = open('/sys/class/gpio/gpio%d/value' %pin,'wb',0)

        val.write(str(value))

        val.close()

    except IOError as e:

        print('Error: %d - %s' %(e.errno,e.strerror))

 

def analogRead(pin):

    filePath = '/sys/devices/platform/c0000000.soc/c0053000.adc/iio:device0/in_voltage%d_raw' %pin

    try:

        file = open(filePath,"rb",0)

        value = file.read()

        file.close()

        return int(value)

    except IOError as e:

        print('Error: %d - %s' %(e.errno, e.strerror))

        return 0

 

LEDpin = 128

LightSensor = 0

 

export(LEDpin)

pinMode(LEDpin,OUTPUT)

 

try:

while True:

raw = analogRead(LightSensor)

volt = raw * 1.8/4095

print("Raw value: %d" %raw)

print("Voltage: %0.2f" %volt)

if raw <= 1024:

digitalWrite(LEDpin, HIGH)

else:

digitalWrite(LEDpin, LOW)

sleep(0.5)

except KeyboardInterrupt:

unexport(LEDpin)

print("Stop!!!")






메이크쉐어 PC버전 로그인
로그인 해주세요.

회원가입

쪽지

포인트

스크랩
기초강좌(배우고)
부품사용법(익히고)
프로젝트(공유하고)
메이크쉐어 PC버전 로그인