树莓派结合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 

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

 

 

发布者

yoyojacky

我是骑驴玩儿漂移, 喜欢玩儿电子,喜欢的编程语言, C, shell, python, 爱玩儿的开发板: 树莓派, arduino,STM32系列, 还有3D 打印机,四轴飞行器,业余时间也喜欢玩儿吉他,非洲鼓. 欢迎来光临我的小站~

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据