使用Raspberry Pi读取输出模拟信号
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)工厂自动化和过程控制
根据以上可以得到,我们一个I2C接口单层级联,可以同时连接4个ADS1115,也就是单层一个I2C接口可以同时测量16路16位的模拟电压量。
VDD - 5V
GND - GND
SCL - SCL
SDA - SDA
ADDR - GND
A0 - MQ 传感器的 A0
1.ssh方式登录树莓派;
2.执行sudo su
切换成root用户,如果你是root用户登录,请忽略这一步;
3.执行sudo raspi-config
,出现如下界面,键盘上下键选择第5个,然后按回车键进入;
4.继续按键盘上下键,还是第5个,进入IIC;
5.选择是
sudo apt-get install i2c-tools
sudo i2cdetect -y 1
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
出现结果