Advertisement
CuriousScientist

Arduino code for ADS1115

Jul 14th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial videos:
  3. //Part 1: https://youtu.be/imcAITmccLQ
  4. //Part 2: https://youtu.be/2JsQ76EohDE
  5.  
  6.  
  7. /*PINS:
  8.  * SDA: A4
  9.  * SCL: A5
  10.  * ADR: GND
  11.  */
  12.  
  13. #include <Wire.h>
  14. #include <Adafruit_ADS1015.h>
  15. Adafruit_ADS1115 ads;  
  16.  
  17. //-------Variables-------------
  18. int16_t channel_1, channel_2;
  19.  
  20. float voltage_1, voltage_2;
  21.  
  22. float distance_1, distance_2;
  23.  
  24. float percentage_1, percentage_2;
  25.  
  26. //-----------------------------
  27.  
  28. void setup() {
  29.  
  30.   Serial.begin(115200);
  31.  
  32.   //----Set default------
  33.   ads.setSPS(DR_128SPS); //Sampling speed 128 SPS
  34.   ads.setGain(GAIN_TWO); //PGA settings +/- 2.048 V
  35.  
  36.   ads.begin();
  37.   //------------------------------------------------------
  38.  
  39.   Serial.println("ADS1115 linear potmeter");
  40.  
  41. }
  42.  
  43. void loop()
  44. {
  45.  readPotmeter_Diff();
  46.  //readPotmeter_Sing();
  47.  delay(1000);
  48.  
  49. }
  50.  
  51. void readPotmeter_Diff()
  52. {
  53.   channel_1 = ads.readADC_Differential_0_1(); //A0 + A1
  54.   Serial.print("D= "); Serial.println(channel_1);
  55.   channel_2 = ads.readADC_Differential_2_3(); //A2 + A3
  56.  
  57.   percentage_1 = (channel_1 / 327.68);
  58.  
  59.   Serial.print("p= ");  Serial.print(percentage_1,3); Serial.println(" %");
  60.  
  61.   voltage_1 = (channel_1 * 0.0625)/1000;
  62.   Serial.print("U= "); Serial.print(voltage_1,6); Serial.println(" V");
  63.   voltage_2 = (channel_2 * 0.0625)/1000;
  64.  
  65.   distance_1 = 0;
  66.  
  67.   distance_2 = 0;
  68.  
  69. }
  70.  
  71. void readPotmeter_Sing()
  72. {
  73.   channel_1 = ads.readADC_SingleEnded(0); //A0
  74.   Serial.println(channel_1);
  75.   channel_2 = ads.readADC_SingleEnded(1); //A1
  76.  
  77.   percentage_1 = 100 * (channel_1 / 32768);
  78.  
  79.   Serial.println(percentage_1, 2);
  80.  
  81.   voltage_1 = (channel_1 * 0.125)/1000;
  82.   Serial.println(voltage_1,6);
  83.   voltage_2 = (channel_2 * 0.125)/1000;
  84.  
  85.   distance_1 = 0;
  86.  
  87.   distance_2 = 0;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement