Digital potentiometer help fuel gauge

2 fuel send
fuel send

i need help wiring up a digital potentiometer to the above circuit.
my code outputs a value of between 4 and 1676 which is the voltage low and high,

[code]
void decodevolts() {
  /* decode Volts
      id = 0x0789
      dlc = 8
      example data[] = 05 62 B0 42 06 8C 00 00
     data byte values are 06 and 8C
       24 bit value
  */
  uint32_t battvolt =  (uint32_t) canMsg.data[canMsg.can_dlc - 4] * 256  + (uint32_t) canMsg.data[canMsg.can_dlc - 3];
  tft.fillRect(50, 240, 140, 40, BLACK);
  tft.setCursor(100, 245);
  tft.setTextSize(4);
  tft.setTextColor(ORANGE, BLACK);
  tft.print(battvolt / 4);
  tft.fillRect(100, 290, 285, 30, BLACK);
  tft.fillRect(100, 290, 10, 30, RED);
  tft.fillRect(375, 290, 10, 30, GREEN);
  tft.fillRect(240, 290, 10, 30, ORANGE);
  if (battvolt >= 8 && battvolt < 1676) {
    tft.fillRect(110, 300, battvolt / 8, 10, WHITE);
  } else if (battvolt == 1676)
    tft.fillRect(110, 300, 265, 10, WHITE);
}

[/code]

i need help writing the code to divide the value by 2 and affect the digital pot to output a resistance over pins 28 and 29 with the input of pin 27.

can anyone help?

Enclose the entire line after battVolt = in parentesis, excluding the semicolon, and add / 2. That fuxes the divide by 2.

Please post a link to the datasheet of the digital pot.

1PCS High Quality X9C104 Digital Potentiometer Module for Arduino Module CK | eBay

thinking of this one?

[code]
#include <SPI.h>
#include <mcp2515.h>
#include <MCUFRIEND_kbv.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
MCUFRIEND_kbv tft;

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>

#define BLACK       0x0000      /*   0,   0,   0 */
#define NAVY        0x000F      /*   0,   0, 128 */
#define DARKGREEN   0x03E0      /*   0, 128,   0 */
#define DARKCYAN    0x03EF      /*   0, 128, 128 */
#define MAROON      0x7800      /* 128,   0,   0 */
#define PURPLE      0x780F      /* 128,   0, 128 */
#define OLIVE       0x7BE0      /* 128, 128,   0 */
#define LIGHTGREY   0xC618      /* 192, 192, 192 */
#define DARKGREY    0x7BEF      /* 128, 128, 128 */
#define BLUE        0x001F      /*   0,   0, 255 */
#define GREEN       0x07E0      /*   0, 255,   0 */
#define CYAN        0x07FF      /*   0, 255, 255 */
#define RED         0xF800      /* 255,   0,   0 */
#define MAGENTA     0xF81F      /* 255,   0, 255 */
#define YELLOW      0xFFE0      /* 255, 255,   0 */
#define WHITE       0xFFFF      /* 255, 255, 255 */
#define ORANGE      0xFDA0      /* 255, 180,   0 */
#define GREENYELLOW 0xB7E0      /* 180, 255,   0 */
#define PINK        0xFC9F

void setup() {
  Serial.begin(115200);
  tft.begin(0x9486);
  tft.fillScreen(BLACK);
  tft.setRotation(1);
  tft.setTextColor(WHITE, BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.setCursor(135, 245);
  tft.setTextColor(WHITE, BLACK);
  tft.print("   v      %");
  tft.fillRect(100, 290, 285, 30, BLACK);
  tft.fillRect(100, 290, 10, 30, RED);
  tft.fillRect(375, 290, 10, 30, GREEN);
  tft.fillRect(240, 290, 10, 30, WHITE);
  tft.fillRect(240, 290, 10, 30, ORANGE);
  tft.drawRect(70, 47, 20, 70, WHITE); // tyre car
  tft.fillRect(200, 55, 50, 50, BLACK);
  tft.drawRect(200, 55, 50, 50, WHITE);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.setCursor(200, 61);
  tft.print("=)");
  tft.fillRect(200, 110, 50, 50, BLACK);
  tft.drawRect(200, 110, 50, 50, WHITE);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.setCursor(215, 127);
  tft.print("~ ");

  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();

}

void loop() {

  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    //start of voltage meter
    if (canMsg.can_id == 0x789 && canMsg.data[canMsg.can_dlc - 5] == 0x42) {
      decodevolts();
    }
    //end of voltage meter

    //start of SoC
    if (canMsg.can_id == 0x789 && canMsg.data[canMsg.can_dlc - 5] == 0x46) {
      decodesoc();
    }
    //end of SoC

    //start of tyre pressure
    if (canMsg.can_id == 0x72C) {
      decodeTyre();
    } //end of tyre pressure

    //start of odometer
    if (canMsg.can_id == 0x768) {
      decodeOdometer();
    }
    //end of odometer

    //start of doors
    if (canMsg.can_id == 0x748 && canMsg.data[canMsg.can_dlc - 5] == 0x12)  {
      decodeDoors();
    }
    //end of doors

    //start of lights
    if (canMsg.can_id == 0x748 && canMsg.data[canMsg.can_dlc - 5] == 0x17)  {
      decodelights();
    }
    //end of lights

    //start of temp
    if (canMsg.can_id == 0x7EB && canMsg.data[canMsg.can_dlc - 5] == 0x1B)  {
      decodecharge();
    }
    //end of temp

    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" ");
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");

    for (int i = 0; i < canMsg.can_dlc; i++)  {
      if (canMsg.data[i] < 0x10) Serial.print('0');
      Serial.print(canMsg.data[i], HEX);
      Serial.print(' ');
    }
    Serial.println();
  }
}

void decodeOdometer() {
  /* decode Odometer
      id = 0x0768
      dlc = 7
      example data[] = 06 62 B1 01 00 04 75 00
     data byte values 04 75
       24 bit value
  */
  uint32_t Odo =  (uint32_t) canMsg.data[canMsg.can_dlc - 3] * 256  + (uint32_t) canMsg.data[canMsg.can_dlc - 2];
  tft.fillRect(20, 195, 150, 40, BLACK);
  tft.setCursor(30, 200);
  tft.setTextSize(3);
  tft.setTextColor(ORANGE, BLACK);
  tft.print(Odo / 1.609344, 1);
}


void decodeTyre() {
  /* decode Tyre pressure
      id = 0x072C
      dlc = 8
      example data[] = 07 62 B0 01 41 36 41 41
     data byte values (last 4 bytes)
      byte 3 = front left
      byte 4 = front right
      byte 5 = rear right
      byte 6 = rear left
  */

  byte RL = canMsg.data[7];
  byte RR = canMsg.data[6];
  byte FR = canMsg.data[5];
  byte FL = canMsg.data[4];
  float pressrl = RL * 0.5823 - 0.15294;
  float pressrr = RR * 0.5823 - 0.15294;
  float pressfr = FR * 0.5823 - 0.15294;
  float pressfl = FL * 0.5823 - 0.15294;
  tft.fillRect(20, 47, 40, 70, BLACK); // tyre car
  tft.fillRect(98, 47, 40, 70, BLACK); // tyre car
  tft.setTextColor(BLUE, BLACK);
  tft.setTextSize(2);
  tft.setCursor(18, 96);
  tft.print(pressrl, 1);
  tft.setCursor(100, 96);
  tft.print(pressrr, 1);
  tft.setCursor(100, 52);
  tft.print(pressfr, 1);
  tft.setCursor(18, 52);
  tft.print(pressfl, 1);
}

void decodeDoors() {
  /*  decode open doors
      id = 0x0748
      dlc = 8
      example data[] = 01 62 D1 12 80 00 00 00
     data byte values (last 4 bytes)
      0x80 = no doors open
      0x01 = Driver door
      0x02 = passenger door
      0x04 = rear left door
      0x05 = rear right door
      0x10 = bonnet
      0x20 = boot
  */

  for ( int i = 4; i < canMsg.can_dlc; ++i ) {
    switch ( canMsg.data[i] ) {
      case 0x80:
        tft.fillRect(345, 27, 70, 100, BLACK);
        return; // nothing so no need to continue
      case 0x01:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED, BLACK);
        tft.setTextSize(3);
        tft.setCursor(398, 52);
        tft.print(">");
        break;
      case 0x02:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED, BLACK);
        tft.setTextSize(3);
        tft.setCursor(342, 52);
        tft.print("<");
        break;
      case 0x04:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED, BLACK);
        tft.setTextSize(3);
        tft.setCursor(342, 96);
        tft.print("<");
        break;
      case 0x05:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED, BLACK);
        tft.setTextSize(3);
        tft.setCursor(398, 96);
        tft.print(">");
        break;
      case 0x10:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED);
        tft.setTextSize(3);
        tft.setCursor(372, 32);
        tft.print("^");
        break;
      case 0x20:
        tft.drawRect(370, 47, 20, 70, WHITE); // door car
        tft.setTextColor(RED);
        tft.setTextSize(3);
        tft.setCursor(372, 105);
        tft.print("^");
        break;
    }
  }
}

void decodevolts() {
  /* decode Volts
      id = 0x0789
      dlc = 8
      example data[] = 05 62 B0 42 06 8C 00 00
     data byte values are 06 and 8C
       24 bit value
  */
  uint32_t battvolt =  (uint32_t) canMsg.data[canMsg.can_dlc - 4] * 256  + (uint32_t) canMsg.data[canMsg.can_dlc - 3];
  tft.fillRect(50, 240, 140, 40, BLACK);
  tft.setCursor(100, 245);
  tft.setTextSize(4);
  tft.setTextColor(ORANGE, BLACK);
  tft.print(battvolt / 4);
  tft.fillRect(100, 290, 285, 30, BLACK);
  tft.fillRect(100, 290, 10, 30, RED);
  tft.fillRect(375, 290, 10, 30, GREEN);
  tft.fillRect(240, 290, 10, 30, ORANGE);
  if (battvolt >= 8 && battvolt < 1676) {
    tft.fillRect(110, 300, battvolt / 8, 10, WHITE);
  } else if (battvolt == 1676)
    tft.fillRect(110, 300, 265, 10, WHITE);
}

void decodesoc() {
  /* decode Volts
      id = 0x0789
      dlc = 8
      example data[] = 05 62 B0 46 06 8C 00 00
     data byte values (last 3 bytes)
       24 bit value
  */

  uint32_t Soc =  (uint32_t) canMsg.data[canMsg.can_dlc - 4] * 256  + (uint32_t) canMsg.data[canMsg.can_dlc - 3];
  if (Soc >= 1000) {
    tft.fillRect(235, 240, 125, 40, BLACK);
    tft.setCursor(255, 245);
    tft.setTextSize(4);
    tft.setTextColor(ORANGE, BLACK);
    tft.print(100);
  }
  tft.fillRect(235, 240, 125, 40, BLACK);
  tft.setCursor(245, 245);
  tft.setTextSize(4);
  tft.setTextColor(ORANGE, BLACK);
  tft.print(" ");
  tft.print(Soc / 10);
}

void decodelights() {
  /*  decode lights
      id = 0x0748
      dlc = 8
      example data[] = 07 62 D1 17 04 20 00 00 - lights on
                       07 62 D1 17 00 20 00 00 - lights off
    data byte values
      0x04 = lights on
      0x00 = lights off
  */

  for ( int i = 4; i < canMsg.can_dlc; ++i ) {
    switch ( canMsg.data[4] ) {
      case 0x00:
        tft.fillRect(200, 55, 50, 50, BLACK);
        tft.drawRect(200, 55, 50, 50, WHITE);
        tft.setTextColor(WHITE);
        tft.setTextSize(5);
        tft.setCursor(200, 61);
        tft.print("=)");
        break; 
      case 0x04:
        tft.fillRect(200, 55, 50, 50, GREEN);
        tft.setTextColor(RED);
        tft.setTextSize(5);
        tft.setCursor(200, 61);
        tft.print("=)");
        break;
    }
  }
}

void decodecharge() {
  /*  decode charger
      id = 0x07EB
      dlc = 8
      example data[] = 04 62  B7  1B  00  00  00  00 -
                       04 62  B7  1B  01  00  00  00 -
    data byte values
      0x04 = charger connected
      0x00 = charger disconnected
  */

  for ( int i = 4; i < canMsg.can_dlc; ++i ) {
    switch ( canMsg.data[4] ) {
      case 0x00:
        tft.fillRect(200, 110, 50, 50, BLACK);
        tft.drawRect(200, 110, 50, 50, WHITE);
        tft.setTextColor(WHITE);
        tft.setTextSize(5);
        tft.setCursor(215, 127);
        tft.print("~ ");
        break; // nothing so no need to continue
      case 0x01:
        tft.fillRect(200, 110, 50, 50, GREEN);
        tft.setTextColor(BLACK);
        tft.setTextSize(5);
        tft.setCursor(215, 127);
        tft.print("~ ");
        break;
    }
  }
}
[/code]

this is my entire code, where would i program the arduino output to the pot?

Is that the dig pot You intend to use?
What variable holds that "value" to be sent to the dig pot?

yes i think so if you think it will work,

battvolt is the canbus data converted to a value.

so i think i need to divide battvolt by 2 then output the 2 values to the pot then sent each resistance from the pot to pins 28 and 29 of the wiring diagram?

You sent a link to the sales sight, not to the datasheet. I'll have to come back home, using the Pc.
Sending what "2 values"?

X9C102, X9C103, X9C104, X9C503 Datasheet (renesas.com)

when value is 1676 it means the battery is full, the gauge in the car has 2 inputs of resistance from pin 28 and 29, if i was to just send it to one pin then it wouldnt be a accurate representation of power.

On the pot there are Rl and Rh. Connect them to 27 resp. 28 as I figure.

and the code?

If You're not familiar with SPI then search for an example code for testing and learning. Once You know how it works You incorporate those lines into Your code.

/*
  Digital Pot Control

  This example controls an Analog Devices AD5206 digital potentiometer.
  The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
  A - connect this to voltage
  W - this is the pot's wiper, which changes when you set it
  B - connect this to ground.

 The AD5206 is SPI-compatible,and to command it, you send two bytes,
 one with the channel number (0 - 5) and one with the resistance value for the
 channel (0 - 255).

 The circuit:
  * All A pins  of AD5206 connected to +5V
  * All B pins of AD5206 connected to ground
  * An LED and a 220-ohm resistor in series connected from each W pin to ground
  * CS - to digital pin 10  (SS pin)
  * SDI - to digital pin 11 (MOSI pin)
  * CLK - to digital pin 13 (SCK pin)

 created 10 Aug 2010
 by Tom Igoe

 Thanks to Heather Dewey-Hagborg for the original tutorial, 2005

*/


// include the SPI library:
#include <SPI.h>


// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

void setup() {
  // set the slaveSelectPin as an output:
  pinMode(slaveSelectPin, OUTPUT);
  // initialize SPI:
  SPI.begin();
}

void loop() {
  // go through the six channels of the digital pot:
  for (int channel = 0; channel < 6; channel++) {
    // change the resistance on this channel from min to max:
    for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, level);
      delay(10);
    }
    // wait a second at the top:
    delay(100);
    // change the resistance on this channel from max to min:
    for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, 255 - level);
      delay(10);
    }
  }

}

void digitalPotWrite(int address, int value) {
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);
  delay(100);
  // send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  delay(100);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin, HIGH);
}

found this example, but my anbus is using pin 10 can i use any pin or are they specific?

Good!

Yes, You can. Search for this line in the code:

const int slaveSelectPin = 10;

As it's only a select pin any digital pin will work.

So if I change level to battvolt within my decodevolts section it should work to output the value to to pot?

Am I on the right track?

Sorry, no. That's not my territory. Finish the first issue before adding more questions.

Sorry I missed the arrival of Your post.

Please post the entire code. Snippets are seldom enough.

No, that's not the way. You just send one value based on the CAN data. This way the indication will be flying up and down. Why don't You run the code and see what happens?

You need to scale the CAN buss data range into a value that matches the range of the dig pot. You need to consider both offset and gain to get the proper reading.

My digipot has 129 steps.
My canbus data ranges from 4 to 1676 where do I put this information?

The datasheet is not available for the moment but I think the pot has 128 steps.
Scale that CAN bus data, 4 to 1676 into 0 to 127. Check arduino/referense and the MAP function.

Edited: Now reading the datasheet I find this: "• 100 Wiper Tap Points". I look at the first of the devices, the X9C102-datasheet.

Quoted: Am I on the right track? Yes, likely You are. Sorry for my negative reply earlier. The setting of the pot is a step process, increase or decrease.