/* This example demonstrates how to take a standard 3-wire pressure transducer
* and read the analog signal, then convert the signal to a readable output and
* display it onto an LCD screen.
*
* Contact Tyler at [email protected] if you have any questions
*/
#include "Wire.h" //allows communication over i2c devices
#include "LiquidCrystal_I2C.h" //allows interfacing with LCD screens
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include <dht.h>
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const int pressureInput1 = A1; //select the analog input pin for the pressure transducer
const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds
float pressureValue = 0; //variable to store the value coming from the pressure transducer
LiquidCrystal_I2C u8g2(0x27, 20, 4); //sets the LCD I2C communication address; format(address, columns, rows)
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R2);
void setup() //setup routine, runs once when system turned on or reset
{
Serial.begin(baudRate); //initializes serial communication at set baud rate bits per second
u8g2.begin(20, 4); //initializes the LCD screen
}
void loop() //loop routine runs over and over again forever
{
pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
//Serial.print(pressureValue, 1); //prints value from previous line to serial
//Serial.println("psi"); //prints label to serial
u8g2.setCursor(0,0); //sets cursor to column 0, row 0
u8g2.print("Pressure:"); //prints label
u8g2.print(pressureValue, 1); //prints pressure value to lcd screen, 1 digit on float
u8g2.print("psi"); //prints label after value
u8g2.print(" "); //to clear the display after large values or negatives
delay(sensorreadDelay); //delay in milliseconds between read values
pressureValue = analogRead(pressureInput1); //reads value from input pin and assigns to variable
pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
//Serial.print(pressureValue, 1); //prints value from previous line to serial
//Serial.println("psi"); //prints label to serial
u8g2.setCursor(0,3); //sets cursor to column 0, row 0
u8g2.print("Pressure:"); //prints label
u8g2.print(pressureValue, 1); //prints pressure value to lcd screen, 1 digit on float
u8g2.print("psi"); //prints label after value
u8g2.print(" "); //to clear the display after large values or negatives
delay(sensorreadDelay); //delay in milliseconds between read values
int analogValue = analogRead(A2);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
u8g2.setCursor(0,2); //sets cursor to column 0, row 0
u8g2.print("Temperature:"); //prints label
u8g2.print(celsius, 1); //prints pressure value to lcd screen, 1 digit on float
u8g2.print("C"); //prints label after value
u8g2.print(" "); //to clear the display after large values or negatives
delay(1000);
}