Advertisement
CuriousScientist

OLED - Pressure dial

Sep 7th, 2021
1,738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h> //Arduino Nano; SDA = A4, SCL = A5
  4.  
  5. //U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //1.3 inch display (SH1106)
  6. U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE);   //0.96 inch display (SSD1306)
  7.  
  8.  
  9. float Current = 0; //4-20 mA (mapped)
  10. float rawADCValue = 0; //0-1023
  11. float ADCVoltage = 0; //0-5 V
  12. float pressure = 0; //0-250 bar (mapped)
  13. float metricTon = 0;
  14. int ADC_Pin = A1;
  15.  
  16. void setup()
  17. {
  18.   Serial.begin(115200); //If you use the display "standalone" and you don't need to receive the numbers on the PC, remove all serial-related lines! (comment them)
  19.  
  20.   u8g2.begin();    
  21.   u8g2.setFont(u8g2_font_inb24_mr);  
  22. }
  23.  
  24. void loop()
  25. {
  26.   readADC();
  27.   printLCD();
  28. }
  29.  
  30. void readADC()
  31. {
  32.   rawADCValue = 0; //Reset the value before summation
  33.  
  34.   for (int i = 0; i < 100; i++) //Do 100 readings
  35.   {
  36.     rawADCValue += analogRead(ADC_Pin);
  37.     delay(5); //"settling time" for the ADC
  38.     //Sum the results 100 times
  39.   }
  40.  
  41.   ADCVoltage = (float)(rawADCValue / 100.0) * (4630 / 1023.0); //The average is converted into voltage (5000 mV = 5 V)
  42.   //measure the real 5 V of your board and substitute the result (mine was 4630 mV)
  43.   Serial.print("RAW: ");
  44.   Serial.println((rawADCValue / 100.0));
  45.   Serial.print("Voltage: ");
  46.   Serial.println(ADCVoltage);
  47.  
  48.   //For 220 Ohm: 4 mA * 220 Ohm = 880 mV, 20 mA * 220 Ohm = 4400 mV
  49.   //For 240 Ohm: 4 mA * 240 Ohm = 960 mV, 20 mA * 240 Ohm = 4800 mV
  50.   //For 250 Ohm: 4 mA * 250 Ohm = 1000 mV, 20 mA * 250 Ohm = 5000 mV
  51.   //Note: measure the 220 (or 250) resistor, and calculate the actual voltages
  52.   Current = mapfloat(ADCVoltage, 960, 4800, 4, 20);
  53.   // Voltage between 880-4400 mV is distributed as current 4-20 mA
  54.   //2640 mV should be 12 mA equivalent
  55.   Serial.print("Current: ");
  56.   Serial.println(Current);
  57.  
  58.   pressure = mapfloat(Current, 4, 20, 0, 250);
  59.   Serial.print("Pressure: ");
  60.   Serial.println(pressure);
  61.   //Current between 4-20 mA is distributed as pressure between 0-250 bar.
  62.   //12 mA should be 125 bar equivalent
  63.  
  64.   metricTon = pressure * 3.1415 * ( (100*100) / 4) / 98066.5; //(100*100) piston diameter in mm units, pressure is in bar, 9806.65 is in N
  65.  
  66.   Serial.print("Metric Ton: ");
  67.   Serial.println(metricTon);
  68. }
  69.  
  70. void printLCD()
  71. {
  72.   u8g2.firstPage();
  73.  
  74.   do {
  75.    u8g2.setFont(u8g2_font_inb24_mr);  //Large font for the number
  76.    u8g2.setCursor(0, 30); //Cursor for the number
  77.    u8g2.print(pressure,0); //pressure without decimals ("XX")
  78.    
  79.    u8g2.setFont(u8g2_font_inb16_mr ); //smaller font for the units  
  80.    u8g2.setCursor(88, 30); //Cursor for the text  
  81.    u8g2.print("Bar");
  82.    
  83.    u8g2.setFont(u8g2_font_inb24_mr);  
  84.    u8g2.setCursor(0, 60);
  85.    u8g2.print(metricTon,1); //metric ton with 1 decimal ("XX.Y")
  86.    
  87.    u8g2.setFont(u8g2_font_inb16_mr );  
  88.    u8g2.setCursor(88, 60);
  89.    u8g2.print("Ton");
  90.    
  91.    } while ( u8g2.nextPage() );
  92.    
  93. }
  94.  
  95. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  96. {
  97.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement