树莓派 c语言 光敏,树莓派结合ADC做个光敏感应的灯

步骤4:将所有东西连接在一起然后打开你的树莓派,不知道为啥网上给树莓派起名字叫覆盆子,太tm难听了。raspberry就改成树莓不行么?

0aba0ddc0111486b521742717a3df09d.png

手绘电路图,看懂了就过,看不懂就自己学习一下再过。

226a91aac31e15e90d22ce242b1cd5ff.png

接驳示意图,ADC的AIN1 通道采样。中间抽头给树莓派,采集信号信息。

C语言编程

下面的内容就是开始编程了,如果你喜欢用C,那么就这样,写个源码:

#include

#include

#include

#include

#include

#include

void main()

{

wiringPiSetup();

//Physical Pin = 40, name is GPIO.29 and wPi name is 29, BCM 21.

pinMode(29, OUTPUT);

int file;

char *bus = "/dev/i2c-1";

if ((file = open(bus, O_RDWR)) < 0)

{

printf("Failed to open the bus.\n");

exit(1);

}

// Get I2C device, ADS1115 I2C address is 0x48(72)

ioctl(file, I2C_SLAVE, 0x48)

// Select configuration register(0x01)

// AINP = AIN0 and AINN = AIN1, +/- 2.048V

// Continuous conversion mode, 128 SPS(0x84, 0x83)

char config[3] = {0};

config[0] = 0x01;

config[1] = 0xD4;

config[2] = 0x83;

write(file, config, 3);

sleep(1);

// Read 2 bytes of data from register(0x00)

// raw_adc msb, raw_adc lsb

char reg[1] = {0x00};

write(file, reg, 1);

char data[2]={0};

if(read(file, data, 2) != 2)

{

printf("Error : Input/Output Error\n");

}

else

{

// Convert the data

int raw_adc = (data[0] * 256 + data[1]);

if (raw_adc > 32767)

{

raw_adc -= 65535;

}

// Output data to screen

printf("Analog Data is: %d \n", raw_adc);

if ( raw_adc > 3200 )

{

printf("Turn on LED\n");

digitalWrite(29, LOW); // turn on the LED

}

else {

printf("Turn off LED\n");

digitalWrite(29, HIGH); //turn off the LED

}

}

}

编译和测试:

gcc -o adc -lwiringPi adc.c

注意:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值