Skip to content

Instantly share code, notes, and snippets.

@MarsTechHAN
Last active August 20, 2020 10:49
Show Gist options
  • Save MarsTechHAN/6f7e0f9f1711b958f4218395738d4b96 to your computer and use it in GitHub Desktop.
Save MarsTechHAN/6f7e0f9f1711b958f4218395738d4b96 to your computer and use it in GitHub Desktop.
// I2C device class (I2Cdev) demonstration Arduino sketch for ADS1115 class
// Example of reading two differential inputs of the ADS1115 and showing the value in mV
// 06 May 2013 by Frederick Farzanegan ([email protected])
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-05-13 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
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.
===============================================
Wiring the ADS1115 Module to an Arduino UNO
ADS1115 --> UNO
VDD 5V
GND GND
SCL A5 (or SCL)
SDA A4 (or SDA)
ALRT 2
*/
#include <M5StickC.h>
#include "ADS1115.h"
ADS1115 adc0(ADS1115_ADDRESS_ADDR_VDD);
void setup() {
M5.begin();
Wire.begin(); // join I2C bus
Serial.begin(115200); // initialize serial communication
Serial.println("Initializing I2C devices...");
adc0.initialize(); // initialize ADS1115 16 bit A/D chip
Serial.println("Testing device connections...");
Serial.println(adc0.testConnection() ? "ADS1115 connection successful" : "ADS1115 connection failed");
// To get output from this method, you'll need to turn on the
//#define ADS1115_SERIAL_DEBUG // in the ADS1115.h file
adc0.showConfigRegister();
// We're going to do continuous sampling
adc0.setMode(ADS1115_MODE_SINGLESHOT);
adc0.setMultiplexer(ADS1115_MUX_P0_N1);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setRotation(1);
M5.update();
}
float data_storge[160] = {0};
uint8_t pos = 0;
uint8_t from_last_rms_update = 60;
float vrms = 0.0;
void loop() {
adc0.setRate(ADS1115_RATE_860);
adc0.setGain(ADS1115_PGA_0P512);
//int sensorOneCounts=adc0.getConversionP0N1(); // counts up to 16-bits
for(uint8_t i=0; i<160; i++){
data_storge[i] = - adc0.getMilliVolts() * 10;;
}
M5.Lcd.fillScreen(TFT_BLACK);
float max_num = -10000;
float min_num = 10000;
for(uint8_t i=0; i<160; i++){
if(data_storge[i] > max_num)
max_num = data_storge[i];
if(data_storge[i] < min_num)
min_num = data_storge[i];
}
float range_factor = (80 * 0.85) / (max_num - min_num);
for(uint8_t i=1; i<159; i++){
//M5.Lcd.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
M5.Lcd.drawLine(i-1, (80 - int(range_factor * (data_storge[i-1] - min_num))-6),i, (80 - int(range_factor * (data_storge[i] - min_num)) - 6), TFT_DARKCYAN);
}
float vsqured = 0;
for(uint8_t i=0; i<160; i++){
vsqured += data_storge[i] * data_storge[i];
}
vsqured = vsqured / 160.0;
vrms = sqrt(vsqured);
from_last_rms_update = 0;
M5.Lcd.setCursor(32, 32);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("%.02fmA", vrms);
M5.Lcd.setCursor(6, 6);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("MAX: %.02fmA", max_num);
M5.Lcd.setCursor(6, 80-12);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("Min: %.02fmA", min_num);
// Serial.printf("%.03f\n", current);
M5.update();
// delay(20);
}
// I2C device class (I2Cdev) demonstration Arduino sketch for ADS1115 class
// Example of reading two differential inputs of the ADS1115 and showing the value in mV
// 06 May 2013 by Frederick Farzanegan ([email protected])
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-05-13 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
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.
===============================================
Wiring the ADS1115 Module to an Arduino UNO
ADS1115 --> UNO
VDD 5V
GND GND
SCL A5 (or SCL)
SDA A4 (or SDA)
ALRT 2
*/
#include <M5StickC.h>
#include "ADS1115.h"
ADS1115 adc0(ADS1115_ADDRESS_ADDR_VDD);
void setup() {
M5.begin();
Wire.begin(); // join I2C bus
Serial.begin(115200); // initialize serial communication
Serial.println("Initializing I2C devices...");
adc0.initialize(); // initialize ADS1115 16 bit A/D chip
Serial.println("Testing device connections...");
Serial.println(adc0.testConnection() ? "ADS1115 connection successful" : "ADS1115 connection failed");
// To get output from this method, you'll need to turn on the
//#define ADS1115_SERIAL_DEBUG // in the ADS1115.h file
adc0.showConfigRegister();
// We're going to do continuous sampling
adc0.setRate(ADS1115_RATE_475);
adc0.setMode(ADS1115_MODE_SINGLESHOT);
adc0.setGain(ADS1115_PGA_0P512);
adc0.setMultiplexer(ADS1115_MUX_P0_N1);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setRotation(1);
M5.update();
}
float data_storge[160] = {0};
uint8_t pos = 0;
void loop() {
//int sensorOneCounts=adc0.getConversionP0N1(); // counts up to 16-bits
float current = - adc0.getMilliVolts() * 10;
if(current > 5120 || current < -5120)
return;
data_storge[pos] = current;
pos++;
M5.Lcd.fillScreen(TFT_BLACK);
float max_num = -10000;
float min_num = 10000;
for(uint8_t i=0; i<160; i++){
if(data_storge[i] > max_num)
max_num = data_storge[i];
if(data_storge[i] < min_num)
min_num = data_storge[i];
}
float range_factor = (80 * 0.85) / (max_num - min_num);
for(uint8_t i=1; i<159; i++){
//M5.Lcd.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
M5.Lcd.drawLine(i-1, (80 - int(range_factor * (data_storge[i-1] - min_num))-6),i, (80 - int(range_factor * (data_storge[i] - min_num)) - 6), TFT_DARKCYAN);
}
M5.Lcd.setCursor(32, 32);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("%.03fmA", current);
M5.Lcd.setCursor(6, 6);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("MAX: %.03fmA", max_num);
M5.Lcd.setCursor(6, 80-12);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("Min: %.03fmA", min_num);
if(pos == 160){
// for(uint8_t i=0; i<160; i++){
// Serial.printf("%f,", data_storge[i]);
// }
// Serial.println();
for(uint8_t i=0; i<159; i++){
data_storge[i] = data_storge[i+1];
}
// for(uint8_t i=0; i<160; i++){
// Serial.printf("%f,", data_storge[i]);
// }
// Serial.println();
pos = 159;
}
// Serial.printf("%.03f\n", current);
M5.update();
// delay(20);
}
// I2C device class (I2Cdev) demonstration Arduino sketch for ADS1115 class
// Example of reading two differential inputs of the ADS1115 and showing the value in mV
// 06 May 2013 by Frederick Farzanegan ([email protected])
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-05-13 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
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.
===============================================
Wiring the ADS1115 Module to an Arduino UNO
ADS1115 --> UNO
VDD 5V
GND GND
SCL A5 (or SCL)
SDA A4 (or SDA)
ALRT 2
*/
#include <M5StickC.h>
#include "ADS1115.h"
ADS1115 adc0(ADS1115_ADDRESS_ADDR_VDD);
void setup() {
M5.begin();
Wire.begin(); // join I2C bus
Serial.begin(115200); // initialize serial communication
Serial.println("Initializing I2C devices...");
adc0.initialize(); // initialize ADS1115 16 bit A/D chip
Serial.println("Testing device connections...");
Serial.println(adc0.testConnection() ? "ADS1115 connection successful" : "ADS1115 connection failed");
// To get output from this method, you'll need to turn on the
//#define ADS1115_SERIAL_DEBUG // in the ADS1115.h file
adc0.showConfigRegister();
// We're going to do continuous sampling
adc0.setRate(ADS1115_RATE_8);
adc0.setMode(ADS1115_MODE_SINGLESHOT);
adc0.setGain(ADS1115_PGA_1P024);
adc0.setMultiplexer(ADS1115_MUX_P0_N1);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setRotation(1);
M5.update();
}
float data_storge[160] = {0};
uint8_t pos = 0;
void loop() {
adc0.triggerConversion();
int32_t sensorCounts=adc0.getConversionP0N1();
float sensorVoltage=(sensorCounts -21)*adc0.getMvPerCount();
float inVoltage = sensorVoltage * 25.4073854 / 1000 ;
data_storge[pos] = inVoltage;
pos++;
M5.Lcd.fillScreen(TFT_BLACK);
float max_num = -10000;
float min_num = 10000;
for(uint8_t i=0; i<160; i++){
if(data_storge[i] > max_num)
max_num = data_storge[i];
if(data_storge[i] < min_num)
min_num = data_storge[i];
}
float range_factor = (80 * 0.85) / (max_num - min_num);
for(uint8_t i=1; i<159; i++){
//M5.Lcd.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
M5.Lcd.drawLine(i-1, (80 - int(range_factor * (data_storge[i-1] - min_num))-6),i, (80 - int(range_factor * (data_storge[i] - min_num)) - 6), TFT_DARKCYAN);
}
M5.Lcd.setCursor(32, 32);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("%.04fV", inVoltage);
M5.Lcd.setCursor(6, 6);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("MAX: %.04fV", max_num);
M5.Lcd.setCursor(6, 80-12);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("Min: %.04fV", min_num);
if(pos == 160){
// for(uint8_t i=0; i<160; i++){
// Serial.printf("%f,", data_storge[i]);
// }
// Serial.println();
for(uint8_t i=0; i<159; i++){
data_storge[i] = data_storge[i+1];
}
// for(uint8_t i=0; i<160; i++){
// Serial.printf("%f,", data_storge[i]);
// }
// Serial.println();
pos = 159;
}
// Serial.printf("%.03f\n", current);
M5.update();
// delay(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment