My Arduino Air Compressor Controller Project – Beginning of Coding!

Thanks for joining me again!

I have good news – the Arduino controller is almost all put together! All that is left for hooking it up is to hook up the 3 wires for the sensor, which won’t take very long, and screw the front cover in. It’s not perfect (I wish I would’ve got a slightly larger box), but it will work:

Air Compressor Controller - 1

For this blog, I would like to start getting into the programming code for this project. First, as I mentioned in my first blog post about Arduinos, you must download the Arduino IDE. The Arduino IDE (Integrated Development Environment) provides a simple platform for writing your code, even provides us with many different examples, and it is used to load the program onto the Arduino through USB. The program code is written in C, which I can provide more information about in the future. Arduino calls the program code a sketch.

There are other options for writing the programs, such as using Microsoft’s Visual Studio with the Visual Micro add-in (which is my personal favorite). Using Visual Studio is a quite a bit more complex to initially get setup and get projects started, so for this blog series I’m just sticking with the Arduino IDE.

The following is a screenshot of the Arduino IDE, with the Blink example, which is installed as the default sketch on all Arduino microcontrollers:

Arduino Blink Sketch

A couple of things to notice with this sketch:

  • We can include comments in our code to help with documentation and for instructional purposes. The compiler will ignore the comments as if they weren’t there, but they are still displayed in the IDE.
    • One line comment “//” – this can be used anywhere in the code, including after a statement, as shown towards the bottom of the sketch. Everything after the // will be ignored by the complier.
    • A multi-line comment begins with “/*” and ends with “*/” – everything in between the start/end designators will be ignored.
  • The Arduino has 2 key functions that it runs:
    • Setup – the Arduino runs this code when it is first turned on. It is used for setting various parameters to prepare the board for the main program
    • Loop – This is the main part of the program for the Arduino – it runs through this function in a continuous loop.

To start out our sketch, I am going to begin with a separate function to calculate the pressure – If you remember, on my post about the hardware I am using a 200 psi linear pressure transducer, which varies the output from 0.5 volts at 0 psi, to 4.5 volts at 200 psi. So, we just need to setup a simple calculation to make it easy to determine what the actual pressure is, based on the output voltage of the pressure transducer. However, to make it a little more complicated, the Arduino doesn’t give us the voltage, it gives us a numerical value from 0 at 0 volts, to 1023 at 5 volts.

So the first thing this code is going to need to do is convert the 0-1023 Arduino value to a voltage. This is pretty simple. For 5 volts, there is a range of 1024 values (Note: 0 is a value here, so it’s 1024, not 1023). Therefore, for each 1 value step of the Arduino’s analog value is equal to 5/1024 = 0.004883 volts. So 205 = 1 volt, 410 = 2 volts…

Next, we want to check for errors in our pressure reading – If something happened to our pressure transducer, and we aren’t getting a good reading we don’t want to keep the compressor running. This pressure transducer outputs a minimum of 0.5 – so if the voltage is less than 0.5 it is more than likely shorted out. On the high side, 5 volts are being applied to it, and its max output is 4.5 – so if it is reading above 4.5 it also means something is wrong and giving us a bad reading.

Lastly, is the voltage to pressure conversion: 4/200 = 50psi/volt (the voltage range is from 0.5 to 4.5 volts, which is only a range of 4). Therefore, for each 1 volt outputted from the pressure transducer above 0.5, is 50 pounds.

There are many different ways you could perform this conversion, but I have included the conversion part of my code for this project at the end of this blog post – please look at it, and provide comments and feedback. If you would like the actual sketch files, please reach out to me through one of the social media links.

Thanks for joining me again! Hopefully we will have my air compressor running again by next week! I’ll be working on the full code, and will be posting it later this week – Please check back in!

double psiconvt(double psi)


                bool olderror = psisenserror;

                double c1;

                double c2;

                double val = 0;

                c1 = psi * 0.004883;


                                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 outside of those parameters, throw an error


                if(c1 < 0.46)


                                psisenserror = true;


                else if(c1 > 4.54)


                                psisenserror = true;




                                psisenserror = false;


                c2 = c1 – 0.5;

                val = c2 * 50;

                return val;


1 thought on “My Arduino Air Compressor Controller Project – Beginning of Coding!

  1. Pingback: My Arduino Air Compressor Controller Project – Finally Functional! | John Kitchen

Leave a Reply

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

You are commenting using your 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