| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,107 @@ | |||
| 1 | + import processing.io.I2C; | ||
| 2 | + | ||
| 3 | + // ADS1015 and ADS1115 are Analog-to-Digital converters using I2C | ||
| 4 | + // they have four channels and 12 and 16 bits of resolution respectively | ||
| 5 | + // datasheets: http://www.ti.com/lit/ds/symlink/ads1015.pdf | ||
| 6 | + // http://www.ti.com/lit/ds/symlink/ads1115.pdf | ||
| 7 | + | ||
| 8 | + class ADS1015 extends ADS1X15 { | ||
| 9 | + ADS1015(String dev, int address) { | ||
| 10 | + super(dev, address); | ||
| 11 | + bitShift = 4; | ||
| 12 | + conversionDelay = 1; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + // returns a number between -1.0 and 1.0 | ||
| 16 | + float analogRead(int channel) { | ||
| 17 | + return readSingleEnded(channel) / 2047.0; | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + class ADS1115 extends ADS1X15 { | ||
| 22 | + ADS1115(String dev, int address) { | ||
| 23 | + super(dev, address); | ||
| 24 | + bitShift = 0; | ||
| 25 | + conversionDelay = 8; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + // returns a number between -1.0 and 1.0 | ||
| 29 | + float analogRead(int channel) { | ||
| 30 | + return readSingleEnded(channel) / 32767.0; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + class ADS1X15 extends I2C { | ||
| 36 | + int address; | ||
| 37 | + int bitShift; // bits to shift the result to the right | ||
| 38 | + int conversionDelay; // in ms | ||
| 39 | + int channel; // last channel used | ||
| 40 | + int range; // see below | ||
| 41 | + | ||
| 42 | + // possible voltage ranges | ||
| 43 | + static final int INTERNAL_6V144 = 0; // +/- 6.144V | ||
| 44 | + static final int INTERNAL_4V096 = 1; // +/- 4.096V (library default) | ||
| 45 | + static final int INTERNAL_2V048 = 2; // +/- 2.048V | ||
| 46 | + static final int INTERNAL_1V024 = 3; // +/- 1.024V | ||
| 47 | + static final int INTERNAL_0V512 = 4; // +/- 0.512V | ||
| 48 | + static final int INTERNAL_0V256 = 5; // +/- 0.256V | ||
| 49 | + | ||
| 50 | + ADS1X15(String dev, int address) { | ||
| 51 | + super(dev); | ||
| 52 | + this.address = address; | ||
| 53 | + this.channel = -1; | ||
| 54 | + this.range = INTERNAL_4V096; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + // be careful not to make the input voltage exceed VCC + 0.3V | ||
| 58 | + // this is regardless of the selected input range | ||
| 59 | + void analogReference(int type) { | ||
| 60 | + if (type < 0 || 7 < type) { | ||
| 61 | + throw new RuntimeException("Invalid range setting"); | ||
| 62 | + } | ||
| 63 | + range = type; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + int readSingleEnded(int channel) { | ||
| 67 | + if (channel < 0 || 3 < channel) { | ||
| 68 | + System.err.println("The channel needs to be from 0 to 3"); | ||
| 69 | + throw new IllegalArgumentException("Unexpected channel"); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + if (channel != this.channel) { | ||
| 73 | + int config = 0x0183; // start with the default value from datasheet | ||
| 74 | + config &= ~0x100; // enable continuous readings | ||
| 75 | + config |= (range << 9); // set selected range (gain) | ||
| 76 | + config |= (1 << 14) | (channel << 12); // set single-ended and channel | ||
| 77 | + config |= (1 << 15); // start a single conversion | ||
| 78 | + writeRegister(0x01, config); // write to the configuration register at 0x01 | ||
| 79 | + | ||
| 80 | + // when the channel switched we need to wait for the upcoming | ||
| 81 | + // conversion to finish | ||
| 82 | + delay(conversionDelay); | ||
| 83 | + | ||
| 84 | + // save the channel so that we don't need to do the same for | ||
| 85 | + // subsequent reads from the same channel | ||
| 86 | + this.channel = channel; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + return readS16(0x00) >> bitShift; // read from the conversion register at 0x00 | ||
| 90 | + // the ADS1015 will have its 12-bit result in the upper bits, shift those right by four | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + protected void writeRegister(int register, int value) { | ||
| 94 | + beginTransmission(address); | ||
| 95 | + write(register); | ||
| 96 | + write(value >> 8); | ||
| 97 | + write(value & 0xFF); | ||
| 98 | + endTransmission(); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + protected int readS16(int register) { | ||
| 102 | + beginTransmission(address); | ||
| 103 | + write(register); | ||
| 104 | + byte[] in = read(2); | ||
| 105 | + return (in[0] << 8) | in[1]; | ||
| 106 | + } | ||
| 107 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + import processing.io.*; | ||
| 2 | + ADS1015 adc; | ||
| 3 | + // or, alternatively: | ||
| 4 | + // ADS1115 adc; | ||
| 5 | + | ||
| 6 | + // see setup.png in the sketch folder for wiring details | ||
| 7 | + | ||
| 8 | + void setup() { | ||
| 9 | + //printArray(I2C.list()); | ||
| 10 | + | ||
| 11 | + adc = new ADS1015("i2c-1", 0x48); | ||
| 12 | + //adc = new ADS1115("i2c-1", 0x48); | ||
| 13 | + | ||
| 14 | + // this sets the measuring range to +/- 4.096 Volts | ||
| 15 | + // other ranges supported by this chip: | ||
| 16 | + // INTERNAL_6V144, INTERNAL_2V048, INTERNAL_1V024, | ||
| 17 | + // INTERNAL_0V512, INTERNAL_0V256 | ||
| 18 | + adc.analogReference(ADS1X15.INTERNAL_4V096); | ||
| 19 | + | ||
| 20 | + // Important: do not attempt to measure voltages higher than | ||
| 21 | + // the supply voltage (VCC) + 0.3V, meaning that 3.6V is the | ||
| 22 | + // absolut maximum voltage on the Raspberry Pi. This is | ||
| 23 | + // irrespective of the analogReference() setting above. | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + void draw() { | ||
| 27 | + // this will return a number between 0 and 1 | ||
| 28 | + // (as long as your voltage is positive) | ||
| 29 | + float measured = adc.analogRead(0); | ||
| 30 | + | ||
| 31 | + // multiply with the selected range to get the absolut voltage | ||
| 32 | + float volts = measured * 4.096; | ||
| 33 | + println("Analog Input 0 is " + volts + "V"); | ||
| 34 | + | ||
| 35 | + background(255); | ||
| 36 | + fill(measured * 255); | ||
| 37 | + ellipse(width/2, height/2, width * 0.75, width * 0.75); | ||
| 38 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments