Building an boostcontroller using an mpx4250

Hello, I new to arduino and using openai to program arduino. By changing the code I'm learning more and more.

The problem I'm facing is that the value of boost doenst change. Only if I put the data pin direct 5V it changes. Can some one see if the code is correct and should give an proper value?

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
    // Initialize the display
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
    {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;)
            ;
    }

    // Initialize serial communication
    Serial.begin(9600);

    // Clear the display buffer
    display.clearDisplay();
}

void loop()
{
    // Clear the previous contents of the display buffer
    display.clearDisplay();

    // Read analog input from pin A0 (MPX4250 sensor)
    int sensorValue = analogRead(A0);

    // Convert sensor reading to voltage (0V to 5V)
    float voltage = sensorValue * (5.0 / 1023.0);

    // Map the voltage to the boost level (0.0 to 1.5)
    float boostLevel = map(voltage, 0.0, 5.0, 0, 150) / 100.0;

    // Display the boost level as text
    display.setTextSize(2);                                      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);                         // Draw white text
    display.setCursor(8, SCREEN_HEIGHT - 15);                     // Move cursor above the bar graph
    display.print(boostLevel, 1);                                 // Print the boost level with one decimal place
    display.setTextSize(1);                                      // Change text size to 1
    display.setCursor(44, SCREEN_HEIGHT - 8);                    // Move cursor to add "bar"
    display.print("Bar");                                         // Print "bar" after boost level

    // Draw the boost bar graph
    int boostBarWidth = linearInterpolation(0.0, 1.5, 0, 61, boostLevel); // Adjusted width for boost bar graph
    int barHeight = 7; // Adjust the height of the bar graph
    display.fillRect(0, SCREEN_HEIGHT - 25, boostBarWidth, barHeight, SSD1306_WHITE); // Adjusted coordinates

    // Draw the TPS window outline
    int windowWidth = 128;
    int windowX = 0;
    int windowY = SCREEN_HEIGHT - 32;
    int windowHeight = 3;
    display.drawRect(windowX, windowY, windowWidth, windowHeight, SSD1306_WHITE);

    // Draw vertical lines at 25%, 50%, and 75% TPS
    int lineY1 = windowY;
    int lineY2 = windowY + windowHeight + 2;
    int line25 = windowX + windowWidth / 4;
    int line50 = windowX + windowWidth / 2;
    int line75 = windowX + windowWidth * 3 / 4;
    display.drawFastVLine(line25, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 25% TPS
    display.drawFastVLine(line50, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 50% TPS
    display.drawFastVLine(line75, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 75% TPS

    // Draw a shorter vertical line at the middle of the screen
    int lineMiddleX = SCREEN_WIDTH / 2;
    int lineMiddleLength = 64;
    int lineMiddleY1 = windowY + 6;
    int lineMiddleY2 = lineMiddleY1 + lineMiddleLength;
    display.drawFastVLine(lineMiddleX, lineMiddleY1, lineMiddleY2 - lineMiddleY1, SSD1306_WHITE); // Draw the line

    // Display the AFR value as text (for testing purposes)
    float afrValue = 14.7; // Example AFR value
    display.setTextSize(2);      // Normal 1:1 pixel scale for smaller text
    display.setCursor(80, SCREEN_HEIGHT - 15); // Same height as boost level
    display.print(afrValue, 1); // Print the AFR value with one decimal place

    // Draw the text "AFR" below the AFR value (for testing purposes)
    display.setTextSize(1);      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(95, SCREEN_HEIGHT - 26); // Move cursor below the AFR value
    display.print("AFR"); // Print "AFR"

    // Update the display
    display.display();

    // Send voltage over serial communication
    Serial.print("Voltage: ");
    Serial.println(voltage, 2); // Print voltage with 2 decimal places

    // Optional delay
    delay(100);
}

// Function for linear interpolation
int linearInterpolation(float x0, float x1, int y0, int y1, float x)
{
    return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
}

float boostLevel = map(voltage, 0.0, 5.0, 0, 150) / 100.0;

It is probably not related to your problem but note that map() works with integer values and will not return an integer

Hi Bob, can you explain? I really novice to this.

Openai gave this back. Is this correct now?

float boostLevel = (voltage - 0.0) * (1.5 - 0.0) / (5.0 - 0.0);

You appear to want boostLevel to be a float but the map() function will return an integer value even though you have used float values as its parameters.

    display.print(boostLevel, 1);                                 // Print the boost level with one decimal place

because it is an integer, printing it with 1 decimal place makes no sense

To track down your problem of the value not changing I suggest that you print the intermediate values used in the calculation of boostLevel, ie sensorValue and voltage. Are they what you expect and are they changing ?

Hi Bob,

I just checked the voltage with a multimeter and it is correct. Also the difference between the pins.

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
    // Initialize the display
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
    {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;)
            ;
    }

    // Clear the display buffer
    display.clearDisplay();

    // Initialize serial communication
    Serial.begin(9600);
}

void loop()
{
    // Clear the previous contents of the display buffer
    display.clearDisplay();

    // Read analog input from pin A0 (MPX4250 sensor)
    int sensorValue = analogRead(A0);

    // Convert sensor value to voltage (0-5V range)
    float voltage = sensorValue * (5.0 / 1023.0);

    // Calculate boost level based on voltage (0.0 to 1.5)
    float boostLevel = (voltage - 0.0) * (1.5 - 0.0) / (5.0 - 0.0);

    // Display the boost level as text
    display.setTextSize(2);                                      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);                         // Draw white text
    display.setCursor(8, SCREEN_HEIGHT - 15);                     // Move cursor above the bar graph
    display.print(boostLevel, 1);                                 // Print the boost level with one decimal place
    display.setTextSize(1);                                      // Change text size to 1
    display.setCursor(44, SCREEN_HEIGHT - 8);                     // Move cursor to add "bar"
    display.print("Bar");                                         // Print "bar" after boost level

    // Draw the boost bar graph
    int boostBarWidth = linearInterpolation(0.0, 1.5, 0, 61, boostLevel); // Adjusted width for boost bar graph
    int barHeight = 7; // Adjust the height of the bar graph
    display.fillRect(0, SCREEN_HEIGHT - 25, boostBarWidth, barHeight, SSD1306_WHITE); // Adjusted coordinates

    // Draw the TPS window outline
    int windowWidth = 128;
    int windowX = 0;
    int windowY = SCREEN_HEIGHT - 32;
    int windowHeight = 3;
    display.drawRect(windowX, windowY, windowWidth, windowHeight, SSD1306_WHITE);

    // Draw vertical lines at 25%, 50%, and 75% TPS
    int lineY1 = windowY;
    int lineY2 = windowY + windowHeight + 2;
    int line25 = windowX + windowWidth / 4;
    int line50 = windowX + windowWidth / 2;
    int line75 = windowX + windowWidth * 3 / 4;
    display.drawFastVLine(line25, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 25% TPS
    display.drawFastVLine(line50, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 50% TPS
    display.drawFastVLine(line75, lineY1, lineY2 - lineY1, SSD1306_WHITE); // Draw the line at 75% TPS

    // Draw a shorter vertical line at the middle of the screen
    int lineMiddleX = SCREEN_WIDTH / 2;
    int lineMiddleLength = 64;
    int lineMiddleY1 = windowY + 6;
    int lineMiddleY2 = lineMiddleY1 + lineMiddleLength;
    display.drawFastVLine(lineMiddleX, lineMiddleY1, lineMiddleY2 - lineMiddleY1, SSD1306_WHITE); // Draw the line

    // Display the AFR value as text (for testing purposes)
    float afrValue = 14.7; // Example AFR value
    display.setTextSize(2);      // Normal 1:1 pixel scale for smaller text
    display.setCursor(80, SCREEN_HEIGHT - 15); // Same height as boost level
    display.print(afrValue, 1); // Print the AFR value with one decimal place

    // Draw the text "AFR" below the AFR value (for testing purposes)
    display.setTextSize(1);      // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE); // Draw white text
    display.setCursor(95, SCREEN_HEIGHT - 26); // Move cursor below the AFR value
    display.print("AFR"); // Print "AFR"

    // Update the display
    display.display();

    // Serial output
    Serial.print("Voltage: ");
    Serial.print(voltage, 3); // Print voltage with 3 decimal places
    Serial.print(" V, Boost Level: ");
    Serial.println(boostLevel, 3); // Print boost level with 3 decimal places

    // Optional delay
    delay(100);
}

// Function for linear interpolation
float linearInterpolation(float x0, float x1, int y0, int y1, float x)
{
    return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
}

What does the Serial output show you is happening ? Are the values varying as you expect ?

Hi Bob,

Based on the datasheet
https://www.farnell.com/datasheets/3360923.pdf

The value at sealevel should be 1.8 volt.

The serial monitor gives me this.

The value is really off. When I put pressure on it with an bikepump the value doenst change.

So I am thinking I have an faulty sensor?

Whatever the problem is I would go no further until you get the correct voltage using a multimeter

Have you got the decoupling circuit in place ?

Hi Bob,

I havent got that in place.

I need to look that up and order the parts I guess.

I doubt that it will make any difference unless the electrical environment is very noisy as it would be in a vehicle. It was just a thought on my part

I am sorry but I cannot offer any more help unless/until the raw output voltage is correct and changes when you change the input pressure. I assume that VCC of the sensor is correct. Which Arduino board are you using ?

I am using an arduino Mega 2560.

I'm still in an prototyping fase. So everything is still on a breadboard.

The Vcc is 4.2 Volt.

That is out of spec for the sensor. According to the datasheet (page 4) the minimum VCC is 4.85

How are you powering the sensor ?

By the 5v from the mega directly in an breadboard.

Well, for some reason the sensor is getting only 4.2V. What is the output voltage of the Mega 5V pin being used to power the sensor when measured at the Mega ? Have you got another source of 5V that you can power the sensor with ?

Are the breadboard connections secure ?

The voltage from the mega is 4.6 to 4.7 volt so it is not high enough for the sensor. I ordered an 12v to 5v volt controller for in the car. I do have an 12v adapter at home already so I can try that out when it arrives home.

Thank you for your help with the troubleshooting Bob. I ll post again as soon I have a good 5v source.

I look forward to hearing the results of using the external power supply. Don't forget to connect the GND of the external supply to GND of the Mega when you try it

If you have a 5V 'phone charger or a power bank you could use that to test the ESC

I tried an powerbank and somehow the mega only delivers 4.6. I still have a uno laying aroundmight give that an try as an power supply.

I was thinking more of using the powerbank to power the sensor directly rather than through teh Mega

I used an genuine arduino UNO and used that to supply the voltage to the sensor. Now the voltage 4.9 volt but the voltage output is stil not correct. it is still around 0.4 volt.