树莓派结合ADC做个光敏感应灯
前言
其实没有什么想法,就是想把ADC用熟练了,这些小案例都是自己杜撰的,实际上没有那么多场景需要用,但是最近真的用在了智能浇花设备上,土壤湿度采样的传感器是模拟的,所以,可以用ADC秀一波操作。
操作步骤
- 步骤1:从https://www.raspberrypi.org/downloads/下载最新镜像,然后选择Raspbian。
- 步骤2:烧录镜像然后启动树莓派。
- 步骤3:通过在终端中键入以下命令连接到Internet并更新系统:
sudo apt-get update sudo apt-get upgrade
- 步骤4:将所有东西连接在一起然后打开你的树莓派,不知道为啥网上给树莓派起名字叫覆盆子,太tm难听了。raspberry就改成树莓不行么?
手绘电路图,看懂了就过,看不懂就自己学习一下再过。
接驳示意图,ADC的AIN1 通道采样。中间抽头给树莓派,采集信号信息。
C语言编程
下面的内容就是开始编程了,如果你喜欢用C,那么就这样,写个源码:
#include <stdio.h> #include <stdlib.h> #include <linux/i2c-dev.h> #include <sys/ioctl.h> #include <fcntl.h> #include <wiringPi.h> 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
注意:
- gcc是编译工具,-o表示定义输出文件名,-lwiringPi表示需要使用wiringPi的库来完成编译代码。
- 编译后,将在工作目录中获得名为sensor的二进制文件,只需使用此命令执行它:
while true do ./adc done
Python编程
- 如果你喜欢用python好吧,那么就更简单了。直接用adafruit的ads1x15的代码改改就能用。
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: Jacky.Li # License: Public Domain import time import Adafruit_ADS1x15 import os adc = Adafruit_ADS1x15.ADS1115() # Choose a gain of 1 for reading voltages from 0 to 4.09V. # Or pick a different gain to change the range of voltages that are read: # - 2/3 = +/-6.144V # - 1 = +/-4.096V # - 2 = +/-2.048V # - 4 = +/-1.024V # - 8 = +/-0.512V # - 16 = +/-0.256V # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain. GAIN = 1 print("Reading ADS1115 values, press Ctrl-C to quit...") # Main loop. while True: # Read all the ADC channel values in a list. values = [0]*4 for i in range(4): # Read the specified ADC channel using the previously set gain value. values[i] = adc.read_adc(i, gain=GAIN) # Note you can also pass in an optional data_rate parameter that controls # the ADC conversion time (in samples/second). Each chip has a different # set of allowed data rate values, see datasheet Table 9 config register # DR bit values. #values[i] = adc.read_adc(i, gain=GAIN, data_rate=128) # Each value will be a 12 or 16 bit signed integer value depending on the # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit). # Print the ADC values. print(values[1]) # print analog data which detected via ADC AIN1 port. if values[1] < 3000: os.system("gpio mode 29 out") os.system("gpio write 29 1") else: os.system("gpio mode 29 in") os.system("gpio write 29 0") time.sleep(0.25)</span>
运行看看效果
python sensor.py
嗯,就这样吧,开开脑洞就可以玩儿得更愉快。。哈哈, 白了个白~