ads1x15: extend examples for both ads1015 and ads1115 and clarify which one is used where

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu 2017-02-07 16:20:13 -08:00
parent 70bcdfefa4
commit dbac88d225
10 changed files with 411 additions and 54 deletions

View File

@ -375,3 +375,4 @@ add_custom_example (mcp2515-txrx-example-cxx mcp2515-txrx.cxx mcp2515)
add_custom_example (ads1015-example-cxx ads1015.cxx ads1x15)
add_custom_example (le910-example-cxx le910.cxx uartat)
add_custom_example (speaker_pwm-example-cxx speaker_pwm.cxx speaker)
add_custom_example (ads1115-example-cxx ads1015.cxx ads1x15)

View File

@ -22,9 +22,10 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1015 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
/* This example demonstrates how to use one the ADS1015 ADC on the Grove Joule
* Shield or the Sparkfun ADC Block for Edison with devices that output a small
* differential voltage (e.g. geophones, piezoelectric bands or pads,
* thermocouples).
*/
#include <fstream>
@ -52,23 +53,19 @@ int main()
string fileName = "./ads1015.data"; // Output filename
ofstream f;
// Initialize and configure the ADS1015 for the SM-24 Geophone
// There are two ADS1015 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
// Initialize and configure the ADS1015
ADS1015 *ads1015 = new upm::ADS1015(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
// Put the ADC into differential mode for pins A0 and A1
ads1015->getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
// documentation for details
ads1015->setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 3300 samples per second (max) and turn on continuous
// sampling
// sampling
ads1015->setSPS(ADS1015::SPS_3300);
ads1015->setContinuous(true);

105
examples/c++/ads1115.cxx Normal file
View File

@ -0,0 +1,105 @@
/*
* Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1115 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
*/
#include <fstream>
#include <string>
#include <thread>
#include <unistd.h>
#include "ads1115.hpp"
using namespace std;
using namespace upm;
bool running = true; // Controls main read/write loop
// Thread function
void stop()
{
sleep(10);
running = false;
}
int main()
{
long id = 0; // Sample number
string fileName = "./ads1115.data"; // Output filename
ofstream f;
// Initialize and configure the ADS1115 for the SM-24 Geophone
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
ADS1115 *ads1115 = new upm::ADS1115(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
ads1115->getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1115->setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling
ads1115->setSPS(ADS1115::SPS_860);
ads1115->setContinuous(true);
// Enable exceptions from the output stream
f.exceptions(ofstream::failbit | ofstream::badbit);
// Open the file
try{
f.open(fileName);
// Output formatting
f.setf(ios_base::fixed);
f.precision(7);
// Start the thread that will stop logging after 10 seconds
thread timer (stop);
// Read sensor data and write it to the output file every ms
while(running){
f << id++ << " " << ads1115->getLastSample() << endl;
usleep(1000);
}
// Clean-up and exit
timer.join();
f.close();
delete ads1115;
} catch (ios_base::failure &e) {
cout << "Failed to write to file: " << e.what() << endl;
return 1;
}
cout << "Wrote " << id << " samples to file: " << fileName << endl;
return 0;
}

View File

@ -22,9 +22,10 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1015 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
/* This example demonstrates how to use one the ADS1015 ADC on the Grove Joule
* Shield or the Sparkfun ADC Block for Edison with devices that output a small
* differential voltage (e.g. geophones, piezoelectric bands or pads,
* thermocouples).
*/
import java.io.*;
import java.util.concurrent.*;
@ -35,7 +36,7 @@ public class Ads1015Sample
static boolean running = true;
static int id = 0; // Sample number
static String fileName = "./ads1015.data"; // Output filename
public static void main(String[] args) throws InterruptedException
{
// Open the output file
@ -48,22 +49,18 @@ public class Ads1015Sample
System.out.println("Failed to open output file for writing: " + e.toString());
System.exit(1);
}
// Initialize and configure the ADS1015 for the SM-24 Geophone
// There are two ADS1015 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
// Initialize and configure the ADS1015
ADS1015 ads1015 = new ADS1015(0, (short)0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
// Put the ADC into differential mode for pins A0 and A1
ads1015.getSample(ADS1X15.ADSMUXMODE.DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
// documentation for details
ads1015.setGain(ADS1X15.ADSGAIN.GAIN_TWO);
// Set the sample rate to 3300 samples per second (max) and turn on continuous
// sampling
ads1015.setSPS(ADS1015.ADSSAMPLERATE.SPS_3300);
@ -71,24 +68,24 @@ public class Ads1015Sample
// Schedule a task to stop logging after 10 seconds
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
@Override
@Override
public void run() {
running = false;
}
}, 10, TimeUnit.SECONDS);
// Read from sensor and write to file every ms
while(running){
try {
bw.write(id + " " + String.format("%.7f", ads1015.getLastSample()) + "\n");
} catch (IOException e) {
System.out.println("Failed to write sample " + id + " to file: "+ e.toString());
System.exit(1);
System.exit(1);
}
id++;
Thread.sleep(1);
}
// Close and exit
try {
bw.close();

View File

@ -0,0 +1,103 @@
/*
* Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1115 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
*/
import java.io.*;
import java.util.concurrent.*;
import upm_ads1x15.*;
public class Ads1115Sample
{
static boolean running = true;
static int id = 0; // Sample number
static String fileName = "./ads1115.data"; // Output filename
public static void main(String[] args) throws InterruptedException
{
// Open the output file
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
} catch (IOException e) {
System.out.println("Failed to open output file for writing: " + e.toString());
System.exit(1);
}
// Initialize and configure the ADS1115 for the SM-24 Geophone
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
ADS1115 ads1115 = new ADS1115(0, (short)0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
ads1115.getSample(ADS1X15.ADSMUXMODE.DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1115.setGain(ADS1X15.ADSGAIN.GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling
ads1115.setSPS(ADS1115.ADSDATARATE.SPS_860);
ads1115.setContinuous(true);
// Schedule a task to stop logging after 10 seconds
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
@Override
public void run() {
running = false;
}
}, 10, TimeUnit.SECONDS);
// Read from sensor and write to file every ms
while(running){
try {
bw.write(id + " " + String.format("%.7f", ads1115.getLastSample()) + "\n");
} catch (IOException e) {
System.out.println("Failed to write sample " + id + " to file: "+ e.toString());
System.exit(1);
}
id++;
Thread.sleep(1);
}
// Close and exit
try {
bw.close();
fw.close();
} catch (IOException e) {
System.out.println("Failed to close output file cleanly: " + e.toString());
System.exit(1);
}
System.out.println("Wrote " + id + " samples to file: " + fileName);
System.exit(0);
}
}

View File

@ -163,6 +163,7 @@ add_example(MB704X_Example mb704x)
add_example(MCP2515_Example mcp2515)
add_example(Ads1015Sample ads1x15)
add_example(MAX30100_Example max30100)
add_example(Ads1115Sample ads1x15)
add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd)
add_example_with_path(Jhd1313m1Sample lcd i2clcd)

View File

@ -22,9 +22,10 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1015 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
/* This example demonstrates how to use one the ADS1015 ADC on the Grove Joule
* Shield or the Sparkfun ADC Block for Edison with devices that output a small
* differential voltage (e.g. geophones, piezoelectric bands or pads,
* thermocouples).
*/
var mraa = require('mraa'); // Not used directly but required if jsupm_ads1x15 was installed from NPM
var upm = require('jsupm_ads1x15');
@ -33,21 +34,16 @@ var file = "./ads1015.data" // Output filename
var id = 0; // Sample number
var ival; // Interval function for sampling
// There are two ADS1015 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
// Initialize and configure the ADS1015
var ads1015 = new upm.ADS1015(0, 0x48);
// Configure the ADS1015 for the SM-24 Geophone
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
// Put the ADC into differential mode for pins A0 and A1
ads1015.getSample(upm.ADS1X15_MUX_DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1015.setGain(upm.ADS1X15.GAIN_TWO);
ads1015.setGain(upm.ADS1X15.GAIN_TWO);
// Set the sample rate to 3300 samples per second (max) and turn on continuous
// sampling

View File

@ -0,0 +1,74 @@
/*
* Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This example demonstrates how to use one of the ADS1115 ADCs on the
* DFRobot Joule Shield with devices that output a small differential
* voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
*/
var mraa = require('mraa'); // Not used directly but required if jsupm_ads1x15 was installed from NPM
var upm = require('jsupm_ads1x15');
var fs = require('fs'); // Values will be written to a file
var file = "./ads1115.data" // Output filename
var id = 0; // Sample number
var ival; // Interval function for sampling
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
var ads1115 = new upm.ADS1115(0, 0x48);
// Configure the ADS1115 for the SM-24 Geophone
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
ads1115.getSample(upm.ADS1X15_MUX_DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1115.setGain(upm.ADS1X15.GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling
ads1115.setSPS(upm.ADS1115.SPS_860);
ads1115.setContinuous(true);
// Open the output file and start reading data once open
// Will overwrite file if it already exists
var stream = fs.createWriteStream(file);
stream.once('open', function() {
ival = setInterval(function() {
// Write data pairs of ID + Value for easy plotting
stream.write(id + " " + ads1115.getLastSample().toFixed(7) + "\n");
id++;
}, 1); // Wait 1 ms between reads, (actuall call adds another ~1 ms overhead)
});
// Stop after 10 seconds and close the file
setTimeout(function() {
clearInterval(ival);
stream.end();
console.log("Wrote " + id + " samples to file: " + file);
}, 10000);

View File

@ -20,9 +20,10 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# This example demonstrates how to use one of the ADS1015 ADCs on the
# DFRobot Joule Shield with devices that output a small differential
# voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
# This example demonstrates how to use one the ADS1015 ADC on the Grove Joule
# Shield or the Sparkfun ADC Block for Edison with devices that output a small
# differential voltage (e.g. geophones, piezoelectric bands or pads,
# thermocouples).
from __future__ import print_function
from threading import Timer
@ -39,23 +40,19 @@ def main():
fileName = './ads1015.data' # Output filename
id = 0 # Sample number
# Initialize and configure the ADS1015 for the SM-24 Geophone
# There are two ADS1015 chips on the DFRobot Joule Shield on the same I2C bus
# - 0x48 gives access to pins A0 - A3
# - 0x49 gives access to pins A4 - A7
# Initialize and configure the ADS1015
ads1015 = upm.ADS1015(0, 0x48)
# Put the ADC into differential mode for pins A0 and A1,
# the SM-24 Geophone is connected to these pins
# Put the ADC into differential mode for pins A0 and A1
ads1015.getSample(upm.ADS1X15.DIFF_0_1)
# Set the gain based on expected VIN range to -/+ 2.048 V
# Can be adjusted based on application to as low as -/+ 0.256 V, see API
# documentation for details
# documentation for details
ads1015.setGain(upm.ADS1X15.GAIN_TWO)
# Set the sample rate to 3300 samples per second (max) and turn on continuous
# sampling
# sampling
ads1015.setSPS(upm.ADS1015.SPS_3300)
ads1015.setContinuous(True)

View File

@ -0,0 +1,86 @@
# Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
# Copyright (c) 2016 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# This example demonstrates how to use one of the ADS1115 ADCs on the
# DFRobot Joule Shield with devices that output a small differential
# voltage (e.g. geophones, piezoelectric bands or pads, thermocouples).
from __future__ import print_function
from threading import Timer
from time import sleep
from upm import pyupm_ads1x15 as upm
def stop():
global running
running = False
def main():
global running
running = True
fileName = './ads1115.data' # Output filename
id = 0 # Sample number
# Initialize and configure the ADS1115 for the SM-24 Geophone
# There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
# - 0x48 gives access to pins A0 - A3
# - 0x49 gives access to pins A4 - A7
ads1115 = upm.ADS1115(0, 0x48)
# Put the ADC into differential mode for pins A0 and A1,
# the SM-24 Geophone is connected to these pins
ads1115.getSample(upm.ADS1X15.DIFF_0_1)
# Set the gain based on expected VIN range to -/+ 2.048 V
# Can be adjusted based on application to as low as -/+ 0.256 V, see API
# documentation for details
ads1115.setGain(upm.ADS1X15.GAIN_TWO)
# Set the sample rate to 860 samples per second (max) and turn on continuous
# sampling
ads1115.setSPS(upm.ADS1115.SPS_860)
ads1115.setContinuous(True)
# Open the output file
try:
f = open(fileName, 'w')
except OSError as e:
print('Cannot open output file:', e)
return
# Setup a timer to stop logging after 10 seconds
t = Timer(10, stop)
t.start()
# Read sensor and write to file every 1 ms
while running:
f.write(str(id) + ' %.7f' % ads1115.getLastSample() + '\n')
id += 1
sleep(0.001)
# Close and exit
f.close()
print('Wrote', id, 'samples to file:', fileName)
return
if __name__ == '__main__':
main()