Skip to content

Instantly share code, notes, and snippets.

@X3msnake
Last active July 2, 2024 01:08
Show Gist options
  • Save X3msnake/fb41607939b62009c573a504d0f9bb94 to your computer and use it in GitHub Desktop.
Save X3msnake/fb41607939b62009c573a504d0f9bb94 to your computer and use it in GitHub Desktop.
Dodge charger - fuel gauge conversion

Dodge Charger - Fuel gauge conversion

image

Car Fuel Gauge converter (1)

image

https://www.tinkercad.com/things/bXyl6mdHUN8-car-fuel-gauge-converter/


String author = "Designed by X3msnake 240701_0042"; 
String prjdoc = "https://gist.github.com/X3msnake/fb41607939b62009c573a504d0f9bb94";

int analogPin = 0;
int transitorPin = 9;
int raw = 0;
int Vin = 5;
float Vout = 0;
float R1 = 100; // Ohm meter reference resistor
float R2 = 0;
float buffer = 0;
int pwmValue = 0;
const int numReadings = 50; // Number of readings to average

// Define the resistance and corresponding PWM values
// float resistanceValues[] = {25, 101.25, 126.25, 202.50, 330}; // 25-330
float resistanceValues[] = {10, 26.25, 42.5, 58.75, 75};   // 10-75 
int pwmValues[] = {60, 100, 137, 175, 255};

void setup(){
  
  Serial.begin(9600);
  
  pinMode(analogPin, INPUT);
  pinMode(transitorPin, OUTPUT);
  
  Serial.println(author);
  Serial.println(prjdoc);
  Serial.println();
  
  for(int i = 0; i < 5; i++) {
    analogWrite(transitorPin, pwmValues[i]);
    Serial.println(pwmValues[i]);
    delay(750);
  }
}

void loop(){
  raw = averageAnalogRead(analogPin);
  Serial.print("Raw: ");
  Serial.println(raw);

  if(raw){
    buffer = raw * Vin;
    Vout = buffer / 1024.0;
    buffer = (Vin / Vout) - 1;
    R2 = R1 * buffer;

    Serial.print("Vout: ");
    Serial.println(Vout);
    Serial.print("R2: ");
    Serial.println(R2);

    pwmValue = mapResistanceToPWM(R2);
    analogWrite(transitorPin, pwmValue); // Write PWM value to pin 9

    Serial.print("PWM: ");
    Serial.println(pwmValue);

    delay(100);
  }
}

int mapResistanceToPWM(float resistance){
  if (resistance < resistanceValues[0]) return 0;
  if (resistance > resistanceValues[4]) return 255;

  // Interpolate between the resistance values
  for (int i = 0; i < 4; i++) {
    if (resistance >= resistanceValues[i] && resistance <= resistanceValues[i+1]) {
      return map(resistance, resistanceValues[i], resistanceValues[i+1], pwmValues[i], pwmValues[i+1]);
    }
  }

  return 0; // Should never reach here
}

int averageAnalogRead(int pin) {
  long sum = 0;
  for (int i = 0; i < numReadings; i++) {
    sum += analogRead(pin);
    delay(10); // Small delay between readings
  }
  return sum / numReadings;
}

PHOTO ALBUM

https://photos.app.goo.gl/4hJsAgdXVPYZ6TrR6

CHATGPT INQUIRES

" I have a module that i am using to power a arduino nano via vin and ground, but when i power the nano from usb without the module connected to 12v the ams1117 regulator starts to heat up "

https://chatgpt.com/share/2114ba5e-6cb2-4879-b10e-861f4e516b3e

" I have a 12v fuel gauge that has +12, ground and signal and expects from the signal to ground 330ohms full at 25ohms how can i drive it from a arduino using a N2222 and pwm pin"

https://chatgpt.com/share/93da4825-1185-4c89-abd7-e3a12ddae324

Reverse engineering the original needle and rebuilding new needles

image image image

@X3msnake
Copy link
Author

X3msnake commented Jun 30, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment