Vehicle ride height controller

#include <OneButton.h>
#include <Potentiometer.h>
#include <EEPROM.h>

/Program to have 2 independent potentiometers control and level the ride height of the vehicle. 3 customizable preset buttons and lcd/

// Variables that won’t change
const int Address = 0; //Starting location to save information
const int ledPin13 = 13; // the number of the LED pin

// RGB led
const int redLed = 2; // red at pin 2
const int greenLed = 3; //green at pin 3
const int blueLed = 4; //blue at pin 4

// Potentiometers
const int potPinfront = {A0}; // front potentiometer pin
const int potPinrear = {A1}; // rear potentiometer pin
const int buttonPin = {A2}; //button pins

// Solenoid valves = 12v outputs
const int frontUp = 9; //front axle up air valve open 12v output
const int frontDown = 8; //front axle down exhaust open 12v output
const int rearUp = 7; //rear axle up air valve open 12v output
const int rearDown = 6; //rear axle down exhaust open 12v output
const int numReads = 10;

// Variables that will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int front = 0; //declare front as the
int rear = 0;

void setup() {
//Declare the board
pinMode(potPinfront, INPUT); //set potPin to be an input
pinMode(potPinrear, INPUT); //set potPin to be an input
pinMode(frontUp, OUTPUT); //set pin 9 as an output
digitalWrite(frontUp, LOW); //valve is closed
pinMode(frontDown, OUTPUT); //set pin 8 as an output
digitalWrite(frontDown, LOW); //valve is closed
pinMode(rearUp, OUTPUT); //set pin 7 as an output
digitalWrite(rearUp, LOW); //valve is closed
pinMode(rearDown, OUTPUT); //set pin 6 as an output
digitalWrite(rearDown, LOW); //valve is closed
pinMode(redLed, OUTPUT); //declare output
pinMode(greenLed, OUTPUT); //declared output
pinMode(blueLed, OUTPUT); //declared output
pinMode(buttonPin, INPUT); //initialize the button pin as a input:
pinMode(ledPin13, OUTPUT); //initialize the button pin as a OUTPUT:
Serial.begin(9600); // turn on Serial Port


void loop(){

buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) { // turn ledPin13 on:
digitalWrite(ledPin13, HIGH); }

else digitalWrite(ledPin13, LOW); // turn ledPin13 off

if (buttonState == HIGH);

front = analogRead(potPinfront); //Read potPinfront
front = map(front, 0, 1023, 0, 255); //Map input to analog
delay(1000); //Small delay

int frontAvg = 0; //average and print front pin readings
for(int i=0; i<numReads; i++)
frontAvg += front;
frontAvg /= numReads;
Serial.print("AVG: ");

if (frontAvg <127){ //print low and light red led
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
else if ((frontAvg >127)&& (frontAvg <137)){ //print high and light green led
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(blueLed, LOW);
else if (frontAvg >137){
digitalWrite(blueLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, LOW);


Yep, I dont need active suspension control, I am hoping to have the system auto level and store 4 preset heights. Nothing fancy. I have mapped the potPin to 0-255

How do you aim get your level reference?
The preset heights will only be good for that vehicle location, as it moves the terrain will move up and down independently on each wheel.
Using those presets will only keep the suspension extensions constant, not the level of the vehicle.
So you are only setting ride height/clearance.
Tom... :slight_smile:

Thanks Tom, I imagine that the system will monitor the current settings, if its not within a certain tolerance of the preset, it will air up or down until it reads within the tolerance. Id like it to keep the vehicle at or near the current settings saved by the preset averages. As the truck is driving it will obviously bounce around but stay within the tolerable parameters.

Does that make sense?

Yes you are looking for constant ground clearance as the load comes on and off the suspension.

As you develop the system, you may need to add an IMU or the like so you don't get into a position where constant ride height and undulating terrain results in c.of.g moving to where you get a turn over or instability.

Tom... :slight_smile:

As the truck is driving it will obviously bounce around but stay within the tolerable parameters.

Up to this point I have been assuming that when you said "ride height control" that you wanted the option of being able to raise and lower the floor of the truck relative the ground - perhaps for aesthetic reasons. This seems reasonably straightforward as it could be achieved by moving the springs relative to the truck body.

However what I have quoted makes me wonder if what you are trying to do is keep the truck floor at its normal distance from the ground even if there is a heavy load on the truck? This seems to me a more complex task as it effectively requires the springs to be made stiffer.