Menu

[r15]: / trunk / pi4j-1.0 / examples / ADS1015GpioExample.java  Maximize  Restore  History

Download this file

144 lines (115 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Examples
* FILENAME : ADS1015GpioExample.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2015 Pi4J
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import java.io.IOException;
import java.text.DecimalFormat;
import com.pi4j.gpio.extension.ads.ADS1015GpioProvider;
import com.pi4j.gpio.extension.ads.ADS1015Pin;
import com.pi4j.gpio.extension.ads.ADS1x15GpioProvider.ProgrammableGainAmplifierValue;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinAnalogInput;
import com.pi4j.io.gpio.event.GpioPinAnalogValueChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerAnalog;
import com.pi4j.io.i2c.I2CBus;
/**
* <p>
* This example code demonstrates how to use the ADS1015 Pi4J GPIO interface
* for analog input pins.
* </p>
*
* @author Robert Savage
*/
public class ADS1015GpioExample {
public static void main(String args[]) throws InterruptedException, IOException {
System.out.println("<--Pi4J--> ADS1015 GPIO Example ... started.");
// number formatters
final DecimalFormat df = new DecimalFormat("#.##");
final DecimalFormat pdf = new DecimalFormat("###.#");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// create custom ADS1015 GPIO provider
final ADS1015GpioProvider gpioProvider = new ADS1015GpioProvider(I2CBus.BUS_1, ADS1015GpioProvider.ADS1015_ADDRESS_0x48);
// provision gpio analog input pins from ADS1015
GpioPinAnalogInput myInputs[] = {
gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A0, "MyAnalogInput-A0"),
gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A1, "MyAnalogInput-A1"),
gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A2, "MyAnalogInput-A2"),
gpio.provisionAnalogInputPin(gpioProvider, ADS1015Pin.INPUT_A3, "MyAnalogInput-A3"),
};
// ATTENTION !!
// It is important to set the PGA (Programmable Gain Amplifier) for all analog input pins.
// (You can optionally set each input to a different value)
// You measured input voltage should never exceed this value!
//
// In my testing, I am using a Sharp IR Distance Sensor (GP2Y0A21YK0F) whose voltage never exceeds 3.3 VDC
// (http://www.adafruit.com/products/164)
//
// PGA value PGA_4_096V is a 1:1 scaled input,
// so the output values are in direct proportion to the detected voltage on the input pins
gpioProvider.setProgrammableGainAmplifier(ProgrammableGainAmplifierValue.PGA_4_096V, ADS1015Pin.ALL);
// Define a threshold value for each pin for analog value change events to be raised.
// It is important to set this threshold high enough so that you don't overwhelm your program with change events for insignificant changes
gpioProvider.setEventThreshold(500, ADS1015Pin.ALL);
// Define the monitoring thread refresh interval (in milliseconds).
// This governs the rate at which the monitoring thread will read input values from the ADC chip
// (a value less than 50 ms is not permitted)
gpioProvider.setMonitorInterval(100);
// create analog pin value change listener
GpioPinListenerAnalog listener = new GpioPinListenerAnalog()
{
@Override
public void handleGpioPinAnalogValueChangeEvent(GpioPinAnalogValueChangeEvent event)
{
// RAW value
double value = event.getValue();
// percentage
double percent = ((value * 100) / ADS1015GpioProvider.ADS1015_RANGE_MAX_VALUE);
// approximate voltage ( *scaled based on PGA setting )
double voltage = gpioProvider.getProgrammableGainAmplifier(event.getPin()).getVoltage() * (percent/100);
// display output
System.out.print("\r (" + event.getPin().getName() +") : VOLTS=" + df.format(voltage) + " | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + " ");
}
};
myInputs[0].addListener(listener);
myInputs[1].addListener(listener);
myInputs[2].addListener(listener);
myInputs[3].addListener(listener);
// keep program running for 10 minutes
for (int count = 0; count < 600; count++) {
// display output
//System.out.print("\r ANALOG VALUE (FOR INPUT A0) : VOLTS=" + df.format(voltage) + " | PERCENT=" + pdf.format(percent) + "% | RAW=" + value + " ");
Thread.sleep(1000);
}
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
System.out.print("");
System.out.print("");
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.