Advertisement
CuriousScientist

Arduino code for ADS1115 with KPM12-J and 16x2 LCD

Nov 3rd, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.54 KB | None | 0 0
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to this tutorial video: https://youtu.be/UZ3PpUgbt-k
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Wire.h>
  5. #include <Adafruit_ADS1015.h>
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7.  
  8. Adafruit_ADS1115 ads;  
  9.  
  10. int cin1; //for reading the instructions from serial
  11. int samplingfreq; //sampling frequency [milliseconds]
  12. float distance = 0; //for the calibrated distance
  13. float adcRead; //for the raw data
  14.  
  15. void setup(void)
  16. {
  17.   Serial.begin(115200);
  18.  
  19.   //some info to know what is on the Arduino
  20.   Serial.println("KP12M-25mm Displacement sensor, LCD, Button.");
  21.   Serial.println("Getting differential reading from AIN0 (P) and AIN1 (N)");
  22.   Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  23.  
  24.   // The ADC input range (or gain) can be changed via the following
  25.   // functions, but be careful never to exceed VDD +0.3V max, or to
  26.   // exceed the upper and lower limits if you adjust the input range!
  27.   // Setting these values incorrectly may destroy your ADC!
  28.   //                                                                ADS1015  ADS1115
  29.   //                                                                -------  -------
  30.   // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  31.   // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  32.   // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  33.   // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  34.   // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  35.   // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  36.  
  37.  
  38.   //----Set default------
  39.   ads.setSPS(DR_920SPS); //Sampling speed
  40.  
  41.   ads.setGain(GAIN_TWOTHIRDS); // Gain (set to max now)
  42.   ads.begin();
  43.  
  44.   //-----------------Taking care of LCD-------------------
  45.   //NOTE: if you cannot see the text on the LCD, try to change the potmeter on the back of it.
  46.   //Sometimes you just have wrong contrast settings and nothing shows up on the screen because of it.
  47.   lcd.init();                      // initialize the lcd
  48.   lcd.init();
  49.   lcd.backlight(); //initialize backlight
  50.   //------------------------------------------------------
  51.   lcd.clear(); //clear the LCD
  52.   lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  53.   lcd.print("KP12M-25mm"); //some message
  54.   lcd.setCursor(0, 1); //Cursor is moved to the 2nd line of the LCD
  55.   lcd.print("Reading displ."); //You can write 16 Characters per line .
  56.   delay(3000); //wait 3 sec
  57.  
  58. }
  59.  
  60. void loop(void)
  61. {
  62.  
  63.   //--------------------------------------------------------------------------------------------------------
  64.   double startTime = millis(); //define the time NOW. This will be useful when we need some time readings
  65.  
  66.   if (Serial.available() > 0) {
  67.  
  68.     char cin = Serial.read();    
  69.  
  70.     switch (cin) //Our usuial SWITCH-CASE method
  71.     {
  72.       //-------------------------Changing SPS settings---------------------------------------------------
  73.       case 'd':
  74.         delay(50);
  75.         while (!Serial.available());
  76.  
  77.         SPSifchecker(cin1); //we change the SPS (sampling frequency) by sending and integer between 0-7 (Check the function below)
  78.         //Example: the message "d 2" sets the SPS to 2 which is: 490 samples per second
  79.         break;
  80.       //-------------------------------------------------------------------------------------------------
  81.       //-------------------------Changing PGA settings---------------------------------------------------
  82.       case 'w':
  83.         delay(50);
  84.         while (!Serial.available());
  85.  
  86.         PGAifchecker(cin1); //we change the PGA (onboard amplifier) by sending an integer between 0-6.(Check the function below)
  87.         //Example: the message "w 2" sets PGA to 2 which is: 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  88.         break;
  89.       //-------------------------------------------------------------------------------------------------
  90.       //--------------------------Reading data and sending to PC-----------------------------------------
  91.       case 's': //s: Start
  92.  
  93.         while (Serial.read() != 'N') { //While we don't interrupt it by sending 'N' to the Arduino
  94.          
  95.           double elapsedTime = millis() - startTime; //start another timer
  96.           adcRead = ads.readADC_Differential_0_1(); //read the ADC 0 and 1 pins in differential mode
  97.           distance = (-0.0009353088486627 * adcRead + 27.36432542127); //calibrated displacement
  98.           //------------Serial printout-----------------
  99.           Serial.print(elapsedTime); //elapsed time since this part of the code is running
  100.           Serial.print("\t"); //tab for separation
  101.           Serial.print(adcRead, 0); // raw data, 0 decimals
  102.           Serial.print("\t"); //tab for separation
  103.           Serial.print(distance, 6); //calibrated data (displacement), 6 decimals
  104.           Serial.print("\n"); //linebreak
  105.           //-------------------------------------------
  106.  
  107.           delay(200); //delay 200 ms
  108.         }
  109.         break;
  110.       //-------------------------------------------------------------------------------------------------
  111.       //------------------------------------Single Readout to PC-----------------------------------------
  112.       case 'r': //Read. Just a one shot reading.
  113.  
  114.         adcRead = ads.readADC_Differential_0_1();  //read the ADC 0 and 1 pins in differential mode
  115.         distance = (-0.0009353088486627 * adcRead + 27.36432542127); //calibrated displacement
  116.         //------------Serial printout-----------------
  117.         Serial.print(adcRead, 0); // raw data, 0 decimals
  118.         Serial.print("\t"); //tab for separation
  119.         Serial.print(distance, 6); //calibrated data (displacement), 6 decimals
  120.         Serial.print("\n"); //linebreak
  121.         //-------------------------------------------
  122.         delay(3000); //wait a bit more
  123.  
  124.         break;
  125.       //-------------------------------------------------------------------------------------------------
  126.       //------------------------------Using LCD----------------------------------------------------------
  127.       case 'l': //LCD data
  128.         // we read the sampling frequency from the serial. Lower number, higher sampling rate.
  129.         // this number is independent from the SPS settings. This controls the frequency of Serial.println()
  130.         samplingfreq = Serial.parseInt();
  131.  
  132.         while (Serial.read() != 'N') { //While we don't interrupt it by sending 'N' to the Arduino    
  133.          
  134.           double elapsedTime = millis() - startTime; //start another timer
  135.           adcRead = ads.readADC_Differential_0_1(); //read the ADC 0 and 1 pins in differential mode
  136.           distance = (-0.0009353088486627 * adcRead + 27.36432542127); //calibrated displacement
  137.           //------------Serial printout-----------------
  138.           Serial.print(elapsedTime); //elapsed time since this part of the code is running
  139.           Serial.print("\t"); //tab for separation
  140.           Serial.print(adcRead, 0); // raw data, 0 decimals
  141.           Serial.print("\t"); //tab for separation
  142.           Serial.print(distance, 6); //calibrated data (displacement), 6 decimals
  143.           Serial.print("\n"); //linebreak
  144.           //-------------------------------------------
  145.           //-------------LCD Printout------------------
  146.           lcd.clear(); //clear LCD
  147.           lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  148.           lcd.print("Displacement");
  149.           lcd.setCursor(0, 1); //Defining positon to write from second row,first column .
  150.           lcd.print(distance, 6); //You can write 16 Characters per line
  151.           //-------------------------------------------
  152.  
  153.           delay(samplingfreq); //we have to subtract the code's running time, to get the exact frequency.
  154.         }
  155.  
  156.         break;
  157.       //-------------------------------------------------------------------------------------------------
  158.       default:
  159.  
  160.         break;
  161.  
  162.     }
  163.   }
  164.  
  165.  
  166.  
  167.   //--------------------------------------------------------------------------------------------------------
  168. }
  169.  
  170. void PGAifchecker(int cin1)
  171. {
  172.   //the commented parts are for testing, but I left them in the code
  173.   cin1 = Serial.parseInt();
  174.   //Serial.println(cin1);
  175.   //Serial.println("first cin");
  176.   delay(50);
  177.   //Serial.println("after delay");
  178.  
  179.   if (cin1 < 1 )
  180.   {
  181.     ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default) //1
  182.     //Serial.println("test");
  183.     //Serial.println("PGA-0");
  184.   }
  185.   else if (cin1 < 2)
  186.   {
  187.     ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV // 1,5
  188.     //Serial.println("PGA-1");
  189.   }
  190.   else if (cin1 < 3)
  191.   {
  192.     ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV // 3
  193.     //Serial.println("PGA-2");
  194.   }
  195.   else if (cin1 < 4)
  196.   {
  197.     ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV // 6
  198.     //Serial.println("PGA-3");
  199.   }
  200.   else if (cin1 < 5)
  201.   {
  202.     ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV //12
  203.     //Serial.println("PGA-4");
  204.   }
  205.   else if (cin1 < 6)
  206.   {
  207.     ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV //24
  208.     //Serial.println("PGA-5");
  209.   }
  210.  
  211.   //Serial.println("The value of the PGA now is:");
  212.   //Serial.println(cin1);
  213.  
  214. }
  215.  
  216. void SPSifchecker(int cin1)
  217. {
  218.   //the commented parts are for testing, but I left them in the code
  219.   cin1 = Serial.parseInt();
  220.   //Serial.println(cin1);
  221.   //Serial.println("first cin");
  222.   delay(50);
  223.   //Serial.println("after delay");
  224.  
  225.   // try it with == 1, without ' ' or " "
  226.   if (cin1 < 1)
  227.   {
  228.     ads.setSPS(DR_128SPS);        // 128 samples per second
  229.     //Serial.println("128 SPS");
  230.   }
  231.   else if (cin1 < 2)
  232.   {
  233.     ads.setSPS(DR_250SPS);        // 250 samples per second
  234.   }
  235.   else if (cin1 < 3)
  236.   {
  237.     ads.setSPS(DR_490SPS);        /// 490 samples per second
  238.   }
  239.   else if (cin1 < 4)
  240.   {
  241.     ads.setSPS(DR_920SPS);       // 920 samples per second
  242.   }
  243.   else if (cin1 < 5)
  244.   {
  245.     ads.setSPS(DR_1600SPS);      // 1600 samples per second (default)
  246.   }
  247.   else if (cin1 < 6)
  248.   {
  249.     ads.setSPS(DR_2400SPS);    // 2400 samples per second
  250.     //Serial.println("2400 SPS");
  251.   }
  252.   else if (cin1 < 7)
  253.   {
  254.     ads.setSPS(DR_3300SPS);    // 3300 samples per second
  255.   }
  256.  
  257.   //Serial.println("The value of the PGA now is:");
  258.   //Serial.println(cin1);
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement