ads1115与树莓派

Raspberry Pi 读取模拟信号

急需求解:

使用Raspberry Pi读取输出模拟信号

由于Raspberry Pi 3B内部没有集成ADC,因此需要外接 ADC 来进行模数转换。外接 ADC 选用的是 ADS1115, I2C 的通讯方式实现起来相对容易些。

简单介绍一下ADS模拟-数字转换器

ads1115与树莓派_第1张图片

特点:

1)超小型QFN封装:2mm1.5mm0.4mm
2)宽电源电压范围:2.0V-5.5V
3)低消耗电流:连续模式:只有150u单次模式,自动关闭
4)可编成数据速率:8SPS到860SPS
5)内部底漂移电压基准
6)内部振荡器
7)内部PGA
8)I2C接口:引脚可选择的地址
9)四个单端或两个差分输入(ADS1115)
10)可编成比较器(ADS1114和ADS1115)
11)工作温度:-40摄氏度到+140摄氏度

应用

1)便携式仪表
2)消费品
3)电池监控
4)温度测量
5)工厂自动化和过程控制

引脚与地址访问

ads1115与树莓派_第2张图片
ads1115与树莓派_第3张图片

根据ADDR引脚连接不到不同的引脚上,其I2C的访问地址是不同的:

ads1115与树莓派_第4张图片

  • 当ADDR引脚连接到GND时,地址为:0x48(0100, 1000B)
  • 当ADDR引脚连接到VDD时,地址为:0x49(0100, 1001B)
  • 当ADDR引脚连接到SDA时,地址为:0x4A(0100, 1010B)
  • 当ADDR引脚连接到SCL时,地址为:0x4B(0100, 1011B)

根据以上可以得到,我们一个I2C接口单层级联,可以同时连接4个ADS1115,也就是单层一个I2C接口可以同时测量16路16位的模拟电压量。

接线

ads1115与树莓派_第5张图片

VDD - 5V

GND - GND

SCL - SCL

SDA - SDA

ADDR - GND

A0 - MQ 传感器的 A0

树莓派IIC接口配置

1.ssh方式登录树莓派;
2.执行sudo su 切换成root用户,如果你是root用户登录,请忽略这一步;
3.执行sudo raspi-config,出现如下界面,键盘上下键选择第5个,然后按回车键进入;
ads1115与树莓派_第6张图片
4.继续按键盘上下键,还是第5个,进入IIC;
ads1115与树莓派_第7张图片
5.选择是

ads1115与树莓派_第8张图片
6.检查I2C设备是否在线

sudo apt-get install i2c-tools
sudo i2cdetect -y 1

ads1115与树莓派_第9张图片

贴上代码实现功能

ads1115.c

#include 
#include 
#include "ads1115.h"

/* ADS1115 analogRead function
*===================================*/

static int myAnalogRead(struct wiringPiNodeStruct *node, int pin) {
  int chan = pin - node->pinBase;
  int data[2];
  int value;

  // Start with default values
  int config = ADS1015_REG_CONFIG_CQUE_NONE    | // Disable the comparator (default val)
                    ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
                    ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
                    ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
                    ADS1115_REG_CONFIG_DR_860SPS   | // 860 samples per second (max)
                    ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)
                    //ADS1015_REG_CONFIG_MODE_CONTIN;   // Continuous mode (doesn't work with more than one channel)
  // Set PGA/voltage range
  config |= ADS1015_REG_CONFIG_PGA_4_096V;

  // Set single-ended input chan
  switch (chan)
  {
    case (0):
      config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
      break;
    case (1):
      config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
      break;
    case (2):
      config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
      break;
    case (3):
      config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
      break;
  }

  // Set 'start single-conversion' bit
  config |= ADS1015_REG_CONFIG_OS_SINGLE;
  
  // Sent the config data in the right order
  config = ((config >> 8) & 0x00FF) | ((config << 8) & 0xFF00);
  wiringPiI2CWriteReg16(node->fd, ADS1015_REG_POINTER_CONFIG, config);
 
  // Wait for conversion to complete
  delay(2); // (1/SPS rounded up)

  wiringPiI2CWrite(node->fd, ADS1015_REG_POINTER_CONVERT);
  data[0] = wiringPiI2CRead(node->fd);
  data[1] = wiringPiI2CRead(node->fd);
  value = ((data[0] << 8) & 0xFF00) | data[1];

  // wiringPi doesn't include stdint so everything is an int (int32), this should account for this
  if (value > 0x7FFF) {
    return (value - 0xFFFF);
  } else {
    return value;
  }
}

/* ADS1115 ADC setup:
 *    create ADS1115 device.
 *    id is the address of the chip (0x48 default)
*===============================================*/

int ads1115Setup(const int pinBase, int id) {
  struct wiringPiNodeStruct *node;

  node = wiringPiNewNode(pinBase,4);

  node->fd = wiringPiI2CSetup(id);
  node->analogRead = myAnalogRead;

  if (node->fd < 0) {
    return -1;
  } else {
    return 0;
  }
}

ads1115.h

/*=========================================================================
    I2C ADDRESS/BITS
    -----------------------------------------------------------------------*/
    #define ADS1015_ADDRESS                 (0x48)    // 1001 000 (ADDR = GND)
/*=========================================================================*/

/*=========================================================================
    CONVERSION DELAY (in mS)
    -----------------------------------------------------------------------*/
    #define ADS1015_CONVERSIONDELAY         (1)
    #define ADS1115_CONVERSIONDELAY         (8)
/*=========================================================================*/

/*=========================================================================
    POINTER REGISTER
    -----------------------------------------------------------------------*/
    #define ADS1015_REG_POINTER_MASK        (0x03)
    #define ADS1015_REG_POINTER_CONVERT     (0x00)
    #define ADS1015_REG_POINTER_CONFIG      (0x01)
    #define ADS1015_REG_POINTER_LOWTHRESH   (0x02)
    #define ADS1015_REG_POINTER_HITHRESH    (0x03)
/*=========================================================================*/

/*=========================================================================
    CONFIG REGISTER
    -----------------------------------------------------------------------*/
    #define ADS1015_REG_CONFIG_OS_MASK      (0x8000)
    #define ADS1015_REG_CONFIG_OS_SINGLE    (0x8000)  // Write: Set to start a single-conversion
    #define ADS1015_REG_CONFIG_OS_BUSY      (0x0000)  // Read: Bit = 0 when conversion is in progress
    #define ADS1015_REG_CONFIG_OS_NOTBUSY   (0x8000)  // Read: Bit = 1 when device is not performing a conversion

    #define ADS1015_REG_CONFIG_MUX_MASK     (0x7000)
    #define ADS1015_REG_CONFIG_MUX_DIFF_0_1 (0x0000)  // Differential P = AIN0, N = AIN1 (default)
    #define ADS1015_REG_CONFIG_MUX_DIFF_0_3 (0x1000)  // Differential P = AIN0, N = AIN3
    #define ADS1015_REG_CONFIG_MUX_DIFF_1_3 (0x2000)  // Differential P = AIN1, N = AIN3
    #define ADS1015_REG_CONFIG_MUX_DIFF_2_3 (0x3000)  // Differential P = AIN2, N = AIN3
    #define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000)  // Single-ended AIN0
    #define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000)  // Single-ended AIN1
    #define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000)  // Single-ended AIN2
    #define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000)  // Single-ended AIN3

    #define ADS1015_REG_CONFIG_PGA_MASK     (0x0E00)
    #define ADS1015_REG_CONFIG_PGA_6_144V   (0x0000)  // +/-6.144V range = Gain 2/3
    #define ADS1015_REG_CONFIG_PGA_4_096V   (0x0200)  // +/-4.096V range = Gain 1
    #define ADS1015_REG_CONFIG_PGA_2_048V   (0x0400)  // +/-2.048V range = Gain 2 (default)
    #define ADS1015_REG_CONFIG_PGA_1_024V   (0x0600)  // +/-1.024V range = Gain 4
    #define ADS1015_REG_CONFIG_PGA_0_512V   (0x0800)  // +/-0.512V range = Gain 8
    #define ADS1015_REG_CONFIG_PGA_0_256V   (0x0A00)  // +/-0.256V range = Gain 16

    #define ADS1015_REG_CONFIG_MODE_MASK    (0x0100)
    #define ADS1015_REG_CONFIG_MODE_CONTIN  (0x0000)  // Continuous conversion mode
    #define ADS1015_REG_CONFIG_MODE_SINGLE  (0x0100)  // Power-down single-shot mode (default)

    #define ADS1015_REG_CONFIG_DR_MASK      (0x00E0)  
    #define ADS1015_REG_CONFIG_DR_128SPS    (0x0000)  // 128 samples per second
    #define ADS1015_REG_CONFIG_DR_250SPS    (0x0020)  // 250 samples per second
    #define ADS1015_REG_CONFIG_DR_490SPS    (0x0040)  // 490 samples per second
    #define ADS1015_REG_CONFIG_DR_920SPS    (0x0060)  // 920 samples per second
    #define ADS1015_REG_CONFIG_DR_1600SPS   (0x0080)  // 1600 samples per second (default)
    #define ADS1015_REG_CONFIG_DR_2400SPS   (0x00A0)  // 2400 samples per second
    #define ADS1015_REG_CONFIG_DR_3300SPS   (0x00C0)  // 3300 samples per second

    #define ADS1115_REG_CONFIG_DR_8SPS       ( 0x0000 )  // 8 samples per second
    #define ADS1115_REG_CONFIG_DR_16SPS      ( 0x0020 )  // 16 samples per second
    #define ADS1115_REG_CONFIG_DR_32SPS      ( 0x0040 )  // 32 samples per second
    #define ADS1115_REG_CONFIG_DR_64SPS      ( 0x0060 )  // 64 samples per second
    #define ADS1115_REG_CONFIG_DR_128SPS     ( 0x0080 )  // 128 samples per second
    #define ADS1115_REG_CONFIG_DR_250SPS     ( 0x00A0 )  // 250 samples per second (default)
    #define ADS1115_REG_CONFIG_DR_475SPS     ( 0x00C0 )  // 475 samples per second
    #define ADS1115_REG_CONFIG_DR_860SPS     ( 0x00E0 )  // 860 samples per second

    #define ADS1015_REG_CONFIG_CMODE_MASK   (0x0010)
    #define ADS1015_REG_CONFIG_CMODE_TRAD   (0x0000)  // Traditional comparator with hysteresis (default)
    #define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010)  // Window comparator

    #define ADS1015_REG_CONFIG_CPOL_MASK    (0x0008)
    #define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000)  // ALERT/RDY pin is low when active (default)
    #define ADS1015_REG_CONFIG_CPOL_ACTVHI  (0x0008)  // ALERT/RDY pin is high when active

    #define ADS1015_REG_CONFIG_CLAT_MASK    (0x0004)  // Determines if ALERT/RDY pin latches once asserted
    #define ADS1015_REG_CONFIG_CLAT_NONLAT  (0x0000)  // Non-latching comparator (default)
    #define ADS1015_REG_CONFIG_CLAT_LATCH   (0x0004)  // Latching comparator

    #define ADS1015_REG_CONFIG_CQUE_MASK    (0x0003)
    #define ADS1015_REG_CONFIG_CQUE_1CONV   (0x0000)  // Assert ALERT/RDY after one conversions
    #define ADS1015_REG_CONFIG_CQUE_2CONV   (0x0001)  // Assert ALERT/RDY after two conversions
    #define ADS1015_REG_CONFIG_CQUE_4CONV   (0x0002)  // Assert ALERT/RDY after four conversions
    #define ADS1015_REG_CONFIG_CQUE_NONE    (0x0003)  // Disable the comparator and put ALERT/RDY in high state (default)
/*=========================================================================*/

#ifdef __cplusplus
extern "c" {
#endif

extern int ads1115Setup(int pinBase, int id);

#ifdef __cplusplus
}
#endif

ads1115_read.c

#include 
#include 
#include 
#include 

int main(int argc, char *argv[]) 
{
  
	int16_t value;
  
	double voltage;

	ads1115Setup(100,0x48);

	for (;;) 
	{
    
		value = (int16_t) analogRead(100);
    
		voltage = value * (4.096 / 32768);
		
		printf("ADS1115 Reading: %d\n\r",value);
    
		printf("ADS1115 Voltage: %g\n\r",voltage);

		delay(1000);
  
	} 
	return 0;
}

执行gcc ads1115.c ads1115.h ads1115_read.c -lwiringPi -o ads运行程序
执行./ads出现结果

ads1115与树莓派_第10张图片
完成!!!

你可能感兴趣的:(树莓派)