Automotive Gauges using Arduino

I'm trying to create a gauge cluster using Arduino. Last night I was trying to calibrate temp (f) into analog numbers. My readings were.
120(f)- 1012
130(f)- 1015
140(f)- 1016
150(f)- 1017
160(f)- 1018
170(f)- 1019
180(f)- 1020
200(f)- 1022.

I'm wondering if there is a way to rescale this so I can get a range from 100(f) to 250(f). Or do I have to buy a new sensor to accomplish this?

Welcome to the forum

If you posted your sketch it would help but in its absence (HINT), what you are looking for may be the map() function

Without seeing your code, it's difficult to say, but it looks like you would need the map function which does just that, maps the numbers to a different range of your choice. See details of the function here: Arduino Map() function

BTW, how are you representing your Automotive Gauges? are you using CSS gauges?

Nobody ever needs the map() function. It is a fancy wrapper on the simple linear equation
y = mx+b
that most of us learned in grade school.

The function is purely a convenience, often a simple linear scaling will be enough, and so more straightforward and easier to understand.

Your question is purely mathematical. Get your hands on a book of simple linear algebra and the solution will appear to you very quickly. Or, look for help on a math forum.

You may have a bad data point, which means the data can't be rescaled in a reasonable way. See below. If you delete that point, then a linear fit works very well.
Capture

Capture2

Right now I'm not using any gauges just the sensor. Eventually I'd like to be using a touch screen display

Please do not post 'photos of code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Apologies. I will correct it

Here is the code. My goal is to rescale it so I can read higher temps without maxing out the analog input. If that is possible.

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
int value;
}

void loop() {
  // put your main code here, to run repeatedly:
int value = analogRead(A0);
Serial.println("Analog value : ");
Serial.println(value);
delay(250);
}

Got it.
You may want to take a look at this if your interested in building a dashboard with gauges:
Dashboard using Arduino
I went through the course myself and learned a lot....

You might also want to look at CANBUS , if you car has an OBD2 socket much data such as water temperature , battery voltage, boost etc etc is available from that

its a 74, no computers on board.

1 Like

Im stuck on getting the arduino to see the index.htm file. any ideas?


#include <SD.h>                                               // The SD library allows for reading from and writing to SD cards
#include <SPI.h>
#include <Ethernet.h>

#define REQ_BUF_SZ   50                                       // size of buffer used to capture HTTP requests

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };          // MAC address from Ethernet shield sticker under board (if present)
IPAddress ip(192, 168, 0, 106);                              // Enter your IP address found using IP address finder in a previous lecture
EthernetServer miniServer(80);

File SdCardFile;                                              // the web page file on the SD card
boolean SWITCH_state[4] = {0};                                // stores the states of the Switches
char HTTP_request[REQ_BUF_SZ] = {0};                          // buffered HTTP request stored as null terminated string
char request_index = 0;                                       // index into HTTP_req buffer

void setup()
{
    // For the Ethernet shield to work properly, you must make pin 10 an output and high before initializing the SD card as
    // both the SD card and Ethernet controller cannot use the SPI bus simultaneously. 
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);

    pinMode(5, OUTPUT);     // Switch #1
    pinMode(6, OUTPUT);     // Switch #2
    pinMode(7, OUTPUT);     // Switch #3
    pinMode(8, OUTPUT);     // Switch #4
    // Initialize Switches 
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    Serial.begin(9600);                                       // for debugging only
    Ethernet.begin(mac, ip);                                  // initialize Ethernet device
    miniServer.begin();
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - Bad or missing SD card");
        return;    
    }
    Serial.println("SUCCESS - SD card found");
    
    if (!SD.exists("fts"))  {                           // check the file system to see if their is a file named index.htm
        Serial.println("ERROR - Can't find fts file!");
        return;                                             
    }
    Serial.println("SUCCESS - Found fts file.");
}

void loop()
{
    // listen for incoming clients
    EthernetClient client = miniServer.available();           
    if (client) {                                         
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {                         // Returns the number of bytes available for reading  
                char c = client.read();                       // read 1 byte (character) from client
                if (request_index < (REQ_BUF_SZ - 1)) {       // buffer first part of HTTP request in HTTP_req array 
                    HTTP_request[request_index] = c;          // save HTTP request character
                    request_index++;
                }
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so you can send a reply
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    // remainder of header follows below, depending on if
                    // web page or XML page is requested
                    // Ajax request - send XML file
                    if (StrContains(HTTP_request, "ajax_inputs")) {
                        // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send XML file containing input states
                        SetSWITCHES();
                        XML_response(client);
                    }
                    else  {
                        // web page request
                        // send rest of HTTP header 
                        client.println("Content-Type: text/html");
                        client.println("Connection: close");
                        client.println();
                        // send web page
                        SdCardFile = SD.open("index.htm");        // open web page file
                        if (SdCardFile) {
                          while(SdCardFile.available()) {
                            client.write(SdCardFile.read());      // send web page to client
                          }
                        SdCardFile.close();
                      }
                    }
                    // display received HTTP request on serial port
                    Serial.println(HTTP_request);
                    // reset buffer index and all buffer elements to 0
                    request_index = 0;
                    StrClear(HTTP_request, REQ_BUF_SZ);
                    break;
                }
                if (c == '\n') {
                    // you're starting a new line
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            } // end of client.available
        } // end of client.connected
        delay(1); // give the web browser time to receive the data
        client.stop(); // close the connection
    } 
}

char StrContains(char *str, char *sfind)
// searches for the string sfind in the string str, returns 1 if string found or returns 0 if string not found.
{
    char found = 0;
    char index = 0;
    char len;
    len = strlen(str);
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }
    return 0;
}

void XML_response(EthernetClient cl)
{
    // Sending Gauge Values to webclient 
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
    // Sending SWITCH states to dashboard 
    // SWITCH #1
    cl.print("<SW>");           
    if (SWITCH_state[0]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
    cl.println("</SW>");
    // SWITCH #2
    cl.print("<SW>");
    if (SWITCH_state[1]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
    cl.println("</SW>");    
    // SWITCH #3
    cl.print("<SW>");
    if (SWITCH_state[2]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
    cl.println("</SW>");
    // SWITCH #4
    cl.print("<SW>");
    if (SWITCH_state[3]) {
        cl.print("checked");
    }
    else {
        cl.print("unchecked");
    }
    cl.println("</SW>");
    cl.print("</inputs>"); 
}

void SetSWITCHES(void)
// checks if received HTTP request is switching any of our switches
{
    // SWITCH #1
    if (StrContains(HTTP_request, "SW1=1")) {
        SWITCH_state[0] = 1;  // save SWITCH state
        digitalWrite(5, LOW);
    }
    else if (StrContains(HTTP_request, "SW1=0")) {
        SWITCH_state[0] = 0;  // save SWITCH state
        digitalWrite(5, HIGH);
    }
    // SWITCH #2
    if (StrContains(HTTP_request, "SW2=1")) {
        SWITCH_state[1] = 1;  // save SWITCH state
        digitalWrite(6, LOW);
    }
    else if (StrContains(HTTP_request, "SW2=0")) {
        SWITCH_state[1] = 0;  // save SWITCH state
        digitalWrite(6, HIGH);
    }
    // SWITCH #3 (
    if (StrContains(HTTP_request, "SW3=1")) {
        SWITCH_state[2] = 1;  // save SWITCH state
        digitalWrite(7, LOW);
    }
    else if (StrContains(HTTP_request, "SW3=0")) {
        SWITCH_state[2] = 0;  // save SWITCH state
        digitalWrite(7, HIGH);
    }
    // SWITCH #4 
    if (StrContains(HTTP_request, "SW4=1")) {
        SWITCH_state[3] = 1;  // save SWITCH state
        digitalWrite(8, LOW);
    }
    else if (StrContains(HTTP_request, "SW4=0")) {
        SWITCH_state[3] = 0;  // save SWITCH state
        digitalWrite(8, HIGH);
    }
}

void StrClear(char *str, char length)
// sets every element of str to 0 (clears array)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

I don’t understand the web html stuff - if this is dashboard for your car Won’t you just read the sensors , scale them then present on a display on the dashboard ?

yes I will be doing just that. This is more how to get the sensors to interact with an analog world. I know very little about the programming so this is good.

Are you displaying the sensor values on a web page, then displaying the web page on your

This is in car tho , so can’t see why you would do it that way . I would just use serial to something like a Nextion display or one of the I2C type connected displays .

Sensors side

A number of the sensors , temperature and pressure are used to control the current through the display meter - the upshot of which is they tend to be low resistance sensors ( fuel gauge could be say 0-300ohms) making potential dividers for Arduino use in efficient .
They need to be powered from a constant voltage source( say 9v) ( or current source ) and use internal voltage references , so you measure in the range of 0-1v . That was you can get a “reasonable” value for resistors in a potential divider divider .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.