My Arduino Air Compressor Controller Project – Finally Functional!

Good news: The controller is finally functional!!!

Air Compressor Controller

As of now, the controller is reading the tank pressure, and the temperature and humidity inside the box, and displaying on the LCD.

Compressor Display

The temperature is reading close to the local current weather, but the humidity is reading low – 24% compared to the online weather info of 50%.  The device is is located in my garage, so I expect it to be somewhat different than the actual weather, but I currently do not have anything else to compare the humidity reading to.

Current Weather

I picked up a good, manual pressure gage for verifying the electronic pressure transducer.  The pressure is consistently reading about 3 psi low throughout its entire range, which may be caused by increasing the length of wire from the sensor (causing a very small voltage drop).  Since the pressure difference is the same, instead of changing the conversion constant in the calculation from my previous post, I am just going to add 3 psi to its reading.

Pressure Gage Reading

Currently, I just have the compressor set to start at 100 psi and stop at 130 psi, without being able to change it.  I have also included a shutdown function that ensures the compressor is shut off if there is an error detected from the pressure transducer (a low or high reading out of range).  I have posted all of the current code at the bottom of this page.

I still have more improvements to add in the near future:

  • Creating a menu to:
    • Change the start/stop pressure settings
    • Change the fan start/stop temperature settings
    • Change the duration allowed for the compressor to run
    • Change the standby time & pressure set points
    • Change the LCD update time
  • Add the ability to cycle the fan on/off at temperature set points (right now it just stays running)
  • Add a time shutoff to prevent the compressor from running continuously in the event of a line rupture or other issue when no one is present
  • Add a standby feature when the compressor is not needed – reduced pressure set points and turns off the LCD backlight
  • Log the running hours in the Flash memory (memory that is not lost when power is lost)

Thanks for joining me in this series again!  Please leave comments and check back again soon for some more code updates and download links for all the files (coming soon)!

Current Code:

/*

       Analog Resolution is 0-1023 (204.8 per volt or 0.004883v per resolution)

       Pressure sensor is linear:

              0 psi = 0.5v = 102.4

              200 psi = 4.5v = 921.6

 

 

       Pinsetup:

             Analog:

                     A2 – PSI Sensor

             Digital:

                     D4 – Menu Switch

                     D6 – Up Switch

                     D7 – Down Switch

                     D8 – DH11 Temperature

                     D9 – Compressor Relay

                     D10 – Fan

                     D20 – 2004 Display-SDA

                     D21 – 2004 Display-SCL

*/

 

 

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display

 

#include “DHT.h”

#define DHTPIN 8     // what pin we’re connected to

#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

 

double psiconvt(double);

 

const double psiconversion = 0.004883;

const int PsiSensorPin = A2;

const int menuswitch = 4;

const int upswitch = 6;

const int downswitch = 7;

const int comprelay = 9;

const int fanswitch = 10;

const long debounceDelay = 50;   // the debounce time; increase if the output flickers

 

int reading;

int lastMenuState = LOW;

int lastUpState = LOW;

int lastDownState = LOW;

int MenuPosition = 0;

 

// these variables are for the settings that can be changed

int backlightdelay = 1800000; // 1,800,000 is 30 minutes

int lcddelay = 2000;   // delay between updating the LCD – minimizes flickering

int maxruntime = 600000; // 600,000 = 10 minutes – minimizes the chance for overheating

int pausetime = 300000; // 300,000 = 5 minutes – provides cool down time

int compressorlow[2] = {100, 100}; // temperature to start the compressor

int compressorhigh[2] = {130, 130}; // temperature to stop the compressor

int fanlow[2] = {90, 90}; // temperature to shut the fan off

int fanhigh[2] = {100, 100}; // temperature to start the fan

 

// these variables are for storing the readings and data

int temp[2] = {0, 0};

int humid = 0;

int sensorValue[2] = {0, 0};

double psireading = 0;

bool psisenserror = false;

 

// these long variables are for timing the delays

long lastlcddelaytime = 0;

long lastMDebounceTime = 0;

long lastUDebounceTime = 0;

long lastDDebounceTime = 0;

long menuTime = 0;

 

 

void setup()

{

       lcd.init();

       lcd.backlight();

       dht.begin();

       pinMode(fanswitch, OUTPUT);

       pinMode(comprelay, OUTPUT);

       pinMode(13, OUTPUT);

 

       digitalWrite(fanswitch, HIGH);

       digitalWrite(comprelay, LOW);

 

       digitalWrite(13, LOW);

       Serial.begin(9600);

       delay(500);

}

 

 

void loop()

{

 

       if(millis() > menuTime)

       {

              MenuPosition = 0;

       }

 

       sensorValue[1] = analogRead(PsiSensorPin);

       psireading = psiconvt(sensorValue[1]);

       temp[1] = dht.readTemperature() * 1.8 + 32.0;

       humid = dht.readHumidity();

 

       // send the sensor data to the computer, through serial, if connected

       Serial.print(“SensorValue: “);

       Serial.println(sensorValue[1]);

       Serial.print(“PSI: “);

       Serial.println(psireading);

       Serial.print(“Temp: “);

       Serial.println(temp[1]);

       Serial.print(“Humidity: “);

       Serial.println(humid);

       if(psisenserror==true)

       {

              /*

                     There’s an error with the PSI transducer – shutdown the compressor

                                  – gives error message and counts down 20 seconds

                           go to the label ENDOFMAIN to bypass the rest of the code and restart the loop

              */

 

              shutdown();

              //goto ENDOFMAIN;

       }

 

       if(psireading > compressorhigh[1])

       {

              digitalWrite(comprelay, LOW);

       }

 

       if(psireading < compressorlow[1])

       {

              digitalWrite(comprelay, HIGH);

       }

       lcdprint();

       //ENDOFMAIN:;

}

 

 

void lcdprint()

{

       lcd.clear();

       lcd.print(“Current: “);

       lcd.print(psireading);

       lcd.print(” psi”);

       lcd.setCursor(0,1);

       lcd.print(“On SP: “);

       lcd.print(compressorlow[1]);

       lcd.setCursor(0,2);

       lcd.print(“Off SP: “);                         

        lcd.print(compressorhigh[1]);

 

        lcd.setCursor(0,3);

        lcd.print(“Temp: “);

        lcd.print(temp[1]);

        lcd.print(” Humid: “);

        lcd.print(humid);

 

       sensorValue[2] = sensorValue[1];

       temp[2] = temp[1];

 

}

 

 

double psiconvt(double psi)

{

       bool olderror = psisenserror;

       double c1;

       double c2; 

       double val = 0; 

       c1 = psi * psiconversion; 

       /*

              The psi sensor starts at 0.5V – anything less is an error with the sensor

              The psi sensor ends at 4.5V – anything more is an error with the sensor 

              Therefore, if it is out side of those parameters, throw an error

       */

 

       if(c1 < 0.46)

       {

              psisenserror = true;

       }

       else if(c1 > 4.54)

       {

              psisenserror = true;

       }

       else

       {

              psisenserror = false;

              if(olderror==true)

              {

                     lcd.clear();

              }

       }

 

       c2 = c1 – 0.47;

       val = c2 * 50;

 

val == val + 3;

       return val;

}

 

 

void shutdown()

{

       digitalWrite(comprelay, LOW);

       long delaytime = millis() + 20000;

       int secnds;

       int psecnds = 0;

 

       lcd.clear();

       lcd.print(“Error!”);

       lcd.setCursor(0,1);

       lcd.print(“Wait 20 seconds”);

 

       while(delaytime > millis())

       {

              secnds = (delaytime – millis()) / 1000;

              if(secnds!=psecnds)

              {

                     lcd.clear();

                     lcd.print(“Error!”);

                     lcd.setCursor(0,1);

                     lcd.print(“Wait “);

                     lcd.print(secnds);

                     lcd.print(” seconds”);

              }

              psecnds = secnds;

       }

 }

2 thoughts on “My Arduino Air Compressor Controller Project – Finally Functional!

  1. Trenton

    I was wondering if you had any update on this project and your finished code? I am looking at doing a similar project where I control the air pressure in my trucks air helper springs. I wanted to have a menu system like you wrote about in this blog to have different pressure setting for loaded and unloaded, as well as the ability to have a compressor on to fill tires and such if need be.

    This would be my first Ardunio project and i know it is very ambitious, but with the help of your code in this blog it will make it much easier.

    Thank you,
    Trenton

    Liked by 1 person

    Reply
    1. 1johnkitchen Post author

      Trenton, I appreciate you looking at my project and modeling a similar project after it.

      Unfortunately, I have not completed it yet, but plan on continuing it soon. I used this blog was a part of masters program, which I completed in August. Since then I’ve been busy job hunting, landed a job, moved 2000 miles and I’m still getting settled in – all my stuff (including my servers and drives, where the project files) are in storage with the moving company.

      I’ll get back on it soon, and send you some Ideas for the menu later.

      Thanks
      John

      Like

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s