Skip to content

Commit bf6e6b9

Browse files
Adrien ChapeletAdrien Chapelet
authored andcommitted
Init commit
0 parents  commit bf6e6b9

File tree

7 files changed

+384
-0
lines changed

7 files changed

+384
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 IoTech
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Iotech Arduino Library for
2+
Texas Instruments ADS1118 Breakout
3+
==============================================================
4+
5+
Here is all the code you need to use this sensor
6+
7+
Feel free to fork, or submit any improvements via a pull request
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <Wire.h>
2+
#include "IO_ADS1118.h"
3+
4+
IO_ADS1118 ads(10);
5+
6+
void setup()
7+
{
8+
Serial.begin(115200);
9+
Serial.println("IoTech ADS1118 Arduino Test");
10+
11+
ads.begin();
12+
ads.setGain(G2_048);
13+
}
14+
15+
void loop()
16+
{
17+
int16_t adc_in0 = ads.adsRead(ads.AIN0);
18+
int16_t adc_in1 = ads.adsRead(ads.AIN1);
19+
int16_t adc_in2 = ads.adsRead(ads.AIN2);
20+
int16_t adc_in3 = ads.adsRead(ads.AIN3);
21+
22+
Serial.println(adc_in0);
23+
Serial.println(adc_in1);
24+
Serial.println(adc_in2);
25+
Serial.println(adc_in3);
26+
Serial.println();
27+
delay(100);
28+
}

keywords.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
IO_ADS1118 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
adsRead KEYWORD2
17+
readTemp KEYWORD2
18+
setGain KEYWORD2
19+
self_test KEYWORD2
20+
update_config KEYWORD2
21+
adsReadRaw KEYWORD2
22+
convToFloat KEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Iotech ADS1118 Library
2+
version=1.0.0
3+
author=Adrien Chapelet for IoTech
4+
maintainer=Adrien Chapelet <[email protected]>
5+
sentence=Arduino library for ADS1118 sensors.
6+
paragraph=Arduino library for ADS1118 precision ADC sensors.
7+
category=Sensors
8+
url=https://github.com/adrien3d
9+
architectures=*

src/IO_ADS1118.cpp

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/***************************************************************************
2+
This is a library for the IO_ADS1118 precision ADC
3+
4+
Designed to work with all kinds of IO_ADS1118 Breakout
5+
6+
These sensors use I2C to communicate, 2 pins are required
7+
to interface.
8+
9+
Written by Adrien Chapelet for Iotech
10+
***************************************************************************/
11+
12+
#include "Arduino.h"
13+
#include "IO_ADS1118.h"
14+
15+
//#define DEBUG
16+
17+
#ifdef DEBUG
18+
#define DEBUG_PRINT(x) Serial.println(x)
19+
#else
20+
#define DEBUG_PRINT(x)
21+
#endif
22+
23+
IO_ADS1118::IO_ADS1118(int CS_pin)
24+
{
25+
/* Constructor for IO_ADS1118 class. */
26+
pinMode(CS_pin, OUTPUT);
27+
_cs = CS_pin;
28+
}
29+
30+
void IO_ADS1118::begin()
31+
{
32+
/* Initializes SPI parameters.
33+
Clock Polarity 0, Clock Phase 1 -> SPI mode 1. MSB first.
34+
Page 25 on datasheet
35+
*/
36+
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
37+
SPI.begin();
38+
digitalWrite(_cs, HIGH); // Do not begin transactions yet
39+
if(self_test()){
40+
//Error
41+
DEBUG_PRINT("An error has occurred during self test");
42+
}
43+
}
44+
45+
byte IO_ADS1118::self_test()
46+
{
47+
/* This self test returns 1 if there is an error communicating with
48+
the IO_ADS1118 chip. This may be due to a wiring problem or other.
49+
*/
50+
CURRENT_CONFIG = CONFIG_DEFAULT;
51+
if(CONFIG_DEFAULT != update_config(CONFIG_DEFAULT)){
52+
return 1;
53+
}
54+
return 0;
55+
}
56+
57+
word IO_ADS1118::update_config(uint16_t new_config)
58+
{
59+
/* This function executes a 32-bit transaction.
60+
61+
Input: a word new_config (see datasheet)
62+
Output: a word specifying the configuration register. Should be
63+
identical to new_config if config updated correctly.
64+
*/
65+
word readConfig; //this is the return config
66+
67+
digitalWrite(_cs, LOW);
68+
delayMicroseconds(1);
69+
// The following transfer results in garbage data:
70+
SPI.transfer16(new_config);
71+
// We capture the following transfer to read back the configuration
72+
readConfig = SPI.transfer16(new_config);
73+
digitalWrite(_cs, HIGH); // Done writing
74+
75+
76+
CURRENT_CONFIG = new_config; // Update global configuration variable
77+
Serial.print("Current_config is:");
78+
Serial.println(CURRENT_CONFIG, HEX);
79+
return readConfig;
80+
}
81+
82+
word IO_ADS1118::adsReadRaw(word port)
83+
{
84+
/* This method executes a 16 bit transaction if the current config
85+
is already correct, or 2 32-bit transactions otherwise, updating
86+
the config as necessary.
87+
88+
Input: A port specifying the pin selection (see header file)
89+
Output: Raw requested data from sensor
90+
*/
91+
word read;
92+
byte dummy, MSB, LSB;
93+
94+
#ifdef DEBUG
95+
Serial.print("Current config is: ");
96+
Serial.println(CURRENT_CONFIG, HEX);
97+
Serial.print("Updating configuration to: ");
98+
Serial.println(port | (CURRENT_CONFIG & ~(PIN_BITMASK)), HEX);
99+
Serial.print("CURRENT_CONFIG & ~PIN_BITMASK = ");
100+
Serial.println((CURRENT_CONFIG & ~PIN_BITMASK), HEX);
101+
#endif
102+
103+
//Only update the configuration *if necessary*
104+
if((CURRENT_CONFIG & PIN_BITMASK) != port){
105+
//Updates the configuration to the proper pin:
106+
update_config((port | (CURRENT_CONFIG & ~(PIN_BITMASK))));
107+
}
108+
109+
#ifdef DEBUG
110+
Serial.print("Input mux: 0x");
111+
Serial.println(PIN_BITMASK & CURRENT_CONFIG, HEX);
112+
Serial.print("MOSI: ");
113+
Serial.println(CURRENT_CONFIG, HEX);
114+
#endif
115+
116+
digitalWrite(_cs, LOW);
117+
delayMicroseconds(1);
118+
read = SPI.transfer16(CURRENT_CONFIG);
119+
digitalWrite(_cs, HIGH);
120+
return read;
121+
}
122+
123+
double IO_ADS1118::convToFloat(word read)
124+
{
125+
/* This method returns a double (float) signifying the input signal
126+
Vin for the proper FS (scaling).
127+
128+
Input: a binary word from the sensor
129+
Output: the floating point representation of the sensor value.
130+
*/
131+
132+
// See page 26 of datasheet detailing the 6 gain modes.
133+
float gain[6] = {6.144, 4.096, 2.048, 1.024, 0.512, 0.256};
134+
135+
// Find the correct gain index using bitshift and bitmask
136+
float myGain = gain[(((PGA_BITMASK & CURRENT_CONFIG) >> 8) / 2)];
137+
138+
#ifdef DEBUG
139+
Serial.print("Gain = ");
140+
Serial.println(myGain);
141+
#endif
142+
143+
return (float)((int)read)*((float)myGain)/ ((float)32768);
144+
}
145+
146+
147+
bool IO_ADS1118::setGain(uint16_t GainSet){
148+
//Temp variable
149+
uint16_t newConfig = CURRENT_CONFIG;
150+
151+
//Clear the corresponding bits in current config
152+
newConfig &= ~(0b111 << 9);
153+
154+
//Set the corresponding pins in current config
155+
newConfig |= (GainSet << 9);
156+
157+
//Send update to the ADC
158+
if (update_config(newConfig) == newConfig){
159+
return true;
160+
}
161+
else {
162+
return false;
163+
}
164+
}
165+
166+
double IO_ADS1118::adsRead(word port)
167+
{
168+
/* This method returns the floating point representation of the desired
169+
sensor operation.
170+
171+
Input: a port selection (see header file)
172+
Output: the floating point representation of desired operation.
173+
*/
174+
return convToFloat(adsReadRaw(port)); // Reads from port; converts to float
175+
}
176+
177+
double IO_ADS1118::readTemp()
178+
{
179+
/* This function is still being debug and may or may not work.
180+
*/
181+
182+
/* This method loads the temperature configuration, and returns the
183+
floating point representation of the temperature in degrees Celcius.
184+
*/
185+
if(update_config(CONFIG_TEMPERATURE) != CONFIG_TEMPERATURE){
186+
DEBUG_PRINT("Error: not updating to temp config");
187+
}
188+
word read;
189+
byte MSB, LSB;
190+
digitalWrite(_cs, LOW);
191+
delayMicroseconds(500);
192+
/*
193+
MSB = SPI.transfer((CURRENT_CONFIG >> 8) & 0xFF);
194+
LSB = SPI.transfer(CURRENT_CONFIG & 0xFF);
195+
digitalWrite(_cs, HIGH);
196+
delayMicroseconds(500);
197+
digitalWrite(_cs, LOW);
198+
delayMicroseconds(500);
199+
*/
200+
#ifdef DEBUG
201+
Serial.print("Sending: ");
202+
Serial.println((CURRENT_CONFIG >> 8), HEX);
203+
#endif
204+
MSB = SPI.transfer((CURRENT_CONFIG >> 8) & 0xFF);
205+
#ifdef DEBUG
206+
Serial.print("Sending: ");
207+
Serial.println((CURRENT_CONFIG & 0xFF), HEX);
208+
#endif
209+
LSB = SPI.transfer(CURRENT_CONFIG & 0xFF);
210+
delayMicroseconds(50);
211+
digitalWrite(_cs, HIGH);
212+
read = (MSB << 6) | (LSB >> 2);
213+
#ifdef DEBUG
214+
Serial.print("Raw MSB =");
215+
Serial.println(MSB, BIN);
216+
Serial.print("Raw LSB =");
217+
Serial.println(LSB, BIN);
218+
Serial.print("Raw read =");
219+
Serial.println(read, BIN);
220+
#endif
221+
if((0x8000 & read) == 0){
222+
return (double)read*0.03125; // datasheet page 18
223+
}
224+
read = ~(read - 1);
225+
return (double)read*-0.03125;
226+
}
227+

src/IO_ADS1118.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/***************************************************************************
2+
This is a library for the ADS1118 precision ADC
3+
4+
Designed to work with all kinds of ADS1118 Breakout
5+
6+
These sensors use I2C to communicate, 2 pins are required
7+
to interface.
8+
9+
Written by Adrien Chapelet for Iotech
10+
***************************************************************************/
11+
12+
#ifndef IO_ADS1118_h
13+
#define IO_ADS1118_h
14+
15+
#define G6_144 (0b000)
16+
#define G4_096 (0b001)
17+
#define G2_048 (0b010)
18+
#define G1_024 (0b011)
19+
#define G0_512 (0b100)
20+
#define G0_256 (0b101)
21+
22+
#include "Arduino.h"
23+
#include <SPI.h>
24+
25+
class IO_ADS1118
26+
{
27+
public:
28+
IO_ADS1118(int CS_pin);
29+
void begin();
30+
double adsRead(word port);
31+
double readTemp();
32+
bool setGain(uint16_t GainSet);
33+
34+
byte self_test();
35+
word update_config(uint16_t new_config);
36+
word adsReadRaw(word port);
37+
double convToFloat(word read);
38+
39+
//These are defined input pins, diff and single ended.
40+
const uint16_t DIF01 = 0x0000;
41+
const uint16_t DIF23 = 0x3000;
42+
const word AIN0 = 0x4000;
43+
const word AIN1 = 0x5000;
44+
const word AIN2 = 0x6000;
45+
const word AIN3 = 0x7000;
46+
47+
private:
48+
int _cs; // chip select
49+
word CURRENT_CONFIG; // Global variable holding configuration
50+
const word CONFIG_DEFAULT = 0x008B;
51+
const word CONFIG_TEMPERATURE = 0x049B;
52+
53+
// Bit masks
54+
const word PIN_BITMASK = 0x7000;
55+
const word PGA_BITMASK = 0x0E00;
56+
57+
// Gain selections
58+
// const word G_6144 = 0x0000;
59+
// const word G_4096 = 0x0200;
60+
// const word G_2048 = 0x0400;
61+
// const word G_1024 = 0x0600;
62+
// const word G_0512 = 0x0800;
63+
//const word G_0256 = 0x0E00;
64+
};
65+
66+
#endif

0 commit comments

Comments
 (0)