树莓派结合ADC做个光敏感应的灯

其实没有什么想法,就是想把ADC用熟练了,这些小案例都是自己杜撰的,实际上没有那么多场景需要用,但是最近真的用在了智能浇花设备上,土壤湿度采样的传感器是模拟的,所以,可以用ADC秀一波操作。

步骤1:从https://www.raspberrypi.org/downloads/下载最新镜像,然后选择Raspbian。

步骤2:烧录镜像然后启动树莓派。 

步骤3:通过在终端中键入以下命令连接到Internet并更新系统:

sudo apt-get update 

sudo apt-get upgrade

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

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

接驳示意图,ADC的AIN1 通道采样。 

中间抽头给树莓派,采集信号信息。

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

#include <stdio.h>      //include  system standard io header file

#include <stdlib.h>     // include system standard libraries header file

#include <linux/i2c-dev.h>   //include  i2c-dev header file

#include <sys/ioctl.h>          // include ioctl  header file 

#include <fcntl.h>                // include fcntl header file

#include <wiringPi.h>  // It's very important when you control GPIO, wiringPi header file

void main()

{

         wiringPiSetup();   // initializing the wiringPi's function

         // Define the pin's mode ,pinMode(PIN, DIRECTION); PIN refer to the GPIO name in wPi when you typing "gpio readall" you can find out the pin's name, usring the wPi column's data, direction can be INPUT and OUTPUT and so on, more information please read the /usr/include/wiringPi.h file.

         pinMode(29, OUTPUT);  // Physical Pin = 40, name is GPIO.29 and wPi name is 29, BCM 21.

       // Create I2C bus
        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 LEDn");
                         digitalWrite(29, LOW);  // turn on the LED 
                     }
                 else
                     {
                         printf("Turn off LEDn");
                         digitalWrite(29, HIGH);  //turn off the LED
                      }
             }

}

然后编译和测试:

gcc -o sensor   -lwiringPi  sensor.c 

注意:gcc是编译工具,-o表示定义输出文件名,-lwiringPi表示需要使用wiringPi的库来完成编译代码。

编译后,您将在工作目录中获得名为sensor的二进制文件。 

只需使用此命令执行它:

 

while true

do

     ./sensor 

done

如果你喜欢用python好吧,那么就更简单了。

直接用adafruit的ads1x15的代码改改就能用。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Jacky.Li
# License: Public Domain

# Import the time module.
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15

# import the OS module
import os


# Create an ADS1115 ADC (16-bit) instance.
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's AIN1 port.

    if values[1] < 3000:
        os.system("gpio mode 29 out") # Change pin's direction as output.
        os.system("gpio write 29 1") # make pin 29 high level
    else:
        os.system("gpio mode 29 in") # Change pin's direction as input.
        os.system("gpio write 29 0") # make pin 29 low level
# Pause for half a second.

    time.sleep(0.25)

注意缩进。

运行看看效果;

python sensor.py 

嗯,就这样吧,开开脑洞就可以玩儿得更愉快。。哈哈, 白了个白~

 

 

树莓派ADC采样光敏电阻来做智能家居控制雏形

又是ADC,还是ADS1115的模块,16bit采样率,很适合做模拟采样。接驳方式:

原理图:

然后,刻录最新的raspbian镜像,可以从官方下载:https://www.raspberrypi.org/downloads/ 并将TF卡插回Raspberry Pi并打开电源。

请记住打开ssh服务并将你的Pi连接到互联网,以便我们可以下载并安装必要的软件包。

打开终端编辑一个文件:

sudo  vim.tiny   adc.c 

然后用下面的代码:

#include <stdio.h>                      // some header file, offer the function libs.

#include <stdlib.h>

#include <linux/i2c-dev.h>

#include <sys/ioctl.h>

#include <fcntl.h>

#include <unistd.h>        // very important, offer "read() and "write()" function

 

void main()               

{

    // Create I2C bus

    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 = AIN1 and AINN = GND, +/- 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("Data: %d n", raw_adc);

       }


保存退出后编译:

gcc  -o adc  adc.c  -O3   &&  ./adc

执行后可以读取一次,用shell脚本简单调用一下,间隔0.2秒刷新,看看数据状态。

然后简单写个脚本检测抓拍并上传到我的博客后台的服务器,当然了,现在已经关闭了,怕不法分子看到乱上传东西,就这么个意思吧,你们理解了操作步骤就好。

然后把光敏电阻放在门夹缝的位置上,如果你没有回家,你家人没有回家,但是光敏电阻值发生了变化, 你就会收到一份邮件,拍到一个尝试盗窃的贼? 或者是一个不明飞行物?

有兴趣就试试看吧,白了个白~