Home › Forums › Environmental Sensors › Bizarre eTape Sensor Behaviour › Reply To: Bizarre eTape Sensor Behaviour
From the link you provided, it doesn’t appear that there’s any sort of voltage divider included in that interface device. It specifically says it’s a 0-5 volt module, so there doesn’t appear to be any way to force it to adjust its output voltage range. It also says it needs 6-30v excitation, so I’m curious what you’re using to power it. I’m guessing there are other resistance-to-voltage modules out there that would operate just fine on 3.3v excitation and have a 0-2.5v or 0-3.3v output, or even better – a digital output like I2C or SPI. You can also have your Mayfly do the resistance to voltage measurement with just a little bit of hardware and some code.
But if you want to continue using the 0-5v module, you’ll need to lower it to a safer range in order for the Mayfly to read it properly. When I have a sensor with a 0-5v output, I put two perfectly matched 100k resistors (check the resistance with a precision ohm-meter of about a dozen 10% precision resistors and you’ll find a couple that are the same) in series between the sensor output and ground, and then read the voltage in the middle of the two resistors and you’ll get exactly half. So for a sensor with a 0-5v output, you’ll now get a range of 0-2.5v. If you really wanted to maximize your resolution, you could use a 2/3 resistor divider to give it a range of 0-3.3v, but with the ADS1115, we’re already getting resolution finer than a fraction of a millivolt, so it’s doubtful you’d see much improvement between using a 1/2 divider when compared to a 2/3 divider.
As for your code, I have a few suggestions. Line 52 should be removed. The Pin 2 you mentioned in that pinMode statement is actually the 1284p processor’s pin 2, which is the secondary UART RX pin, so if you were using something like a Bee module, that could cause problems. In line 38, you declared the variable eTapePin was defined as 2, and because that’s the channel number of the aux ADS1115, there’s no reason to set the pinMode for it. You only have to use the variable name when making the readADC statement like you did in line 64. And filling an array in order to find the median uses more memory and takes lots of complicated code when compared to a simple averaging loop, especially if you later want to increase the number of readings.
Here’s a sketch I used recently for taking 100 readings (10ms apart) from the ADS1115 aux analog pin and then averaging them and displaying the result to the serial monitor. If the signal you’re measuring is 3.3v or less, no change is necessary. If you’re measuring a voltage like 5v, use a 50/50 voltage divider to cut the voltage in half and then use line 76 to calculate the average voltage instead of line 74.
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 |
//Takes lots of readings from the EnviroDIY Mayfly's ADS1115 Aux Analog ADC and averages them, prints to screen #include <SPI.h> #include <Wire.h> #include <Adafruit_ADS1015.h> Adafruit_ADS1115 ads; // The Mayfly's ADS1115 Aux ADC is 16-bit version float average_volts; //variable to hold the calculated average int State8 = LOW; int State9 = LOW; int aux_channel = 2; //channel number of whatever pin you've got your input connected to for the ADS1115 Aux Analog chip (AA0 = 0, AA1 = 1, AA2 = 2, AA3 = 3) int readcount = 100; //the number of readings you want to take for each sample int time_between_readings = 10; //number of milliseconds between each reading in a sample void setup() { Serial.begin(57600); //Start the serial connection with the computer ads.begin(); //begin adafruit ADS1015 pinMode(8, OUTPUT); // define the red and green LEDs as outputs pinMode(9, OUTPUT); digitalWrite(8, HIGH); //turn on one of the LEDs to start with digitalWrite(9, LOW); pinMode(22, OUTPUT); //switched power pin declared as output digitalWrite(22, HIGH); //turn on the Switched sensor voltage Serial.println("EnviroDIY Mayfly Aux-Analog Averaging Sketch "); Serial.print("Aux ADC Channel: "); Serial.print(aux_channel); Serial.print("(AA"); Serial.print(aux_channel); Serial.print(") Number of readings per sample: "); Serial.println(readcount); Serial.println("-------------------------------------------------------------"); delay(2000); //wait 2 seconds } void loop() { auxanalog_sample(); //go take a measurement with the ADS1115 //blink the red and green LEDs in alternating fashion on each loop if (State8 == LOW) { State8 = HIGH; } else { State8 = LOW; } digitalWrite(8, State8); State9 = !State8; digitalWrite(9, State9); Serial.print("Averaged analog voltage (volts): "); //print the results to the serial monitor Serial.println(average_volts,5); delay(100); //wait a little bit between loops, adjust as you want } //end loop void auxanalog_sample() // function that takes reading from ADS1115 Aux Analog chip on Mayfly { int16_t adc_bits = 0; //int to hold the value of the bits that get read float bit_sum = 0.0; //sum of all the bits read for (int i = 0; i < readcount; i++){ adc_bits = ads.readADC_SingleEnded(aux_channel); //take a single-ended reading with the ADS1115 bit_sum += adc_bits; //add the latest reading to the sum delay(time_between_readings); //wait x milliseconds between readings, adjust as you want } float average_bits = bit_sum / readcount; //find the average by dividing the bit_sum by readcount //now convert bits into volts average_volts = (average_bits * 3.3)/17585.0; // use this next line instead of the previous line if you're using a voltage divider to reduce an external sensor's voltage // average_volts = ((average_bits * 3.3)/17585.0) * 2.0; //multiply by 2 if you used a 50/50 divider } |