Reading lots of sensors from analog pins PROBLEM

Im makeing a digital dash for an older truck and using an arduino mega as the sensor interface. Most of the truck sensors change resitance to ground 0 to 1k ohms for all of the sensors. There are a few voltages to read to like battery voltage. So i have 6 analog pins puled high with either a 150 or 510ohm resistor for a voltage devider and the sensor being the other resistor. And a 3.3k resistor to analog pin. Im just trying to get the first voltage devider to work and it is a 22k to battery, 6.8k to ground and 3.3k to analog pin. All analog pins read high no mater what. I pit 22ohm resistors in for all sensors and no pin is over 1v and all are pulled down. When i unplug my board from it all works fine on a few pins i tested.



Nice photos, can you draw it out as a schematic?

1 Like

Welvome to the forum.

It wouldn't hurt to post the code that gives these odd results.

Use the IDE Autofotomat tool so your code is pretti(er).

Then use the IDE Copy for Forum tool and paste it in your next post.

TIA

a7

When you switch ADC pins the circuitry needs a settling-time before reading. The datasheet recommends to read the pin twice and ignore the first one after switching ADC pins. IIRC a 50 micros delay will cover the settling time, maybe even 40 while a default read takes 109.

I thought that so i read it twice and added a delay in the code of 100 still the same. Ill add the code in here

don't salt in delays you don't have to

unsigned int digitalPins = 0;

unsigned int rpm = 760;
unsigned int voltage;
unsigned int speed = 1275;
unsigned int cruisespeed = 56;
unsigned int oil = 453;
unsigned int ct = 1825;
unsigned int fuel = 40;
unsigned int fuelpress = 620;
unsigned int boost = 107;
unsigned int exhpress = 1000;
unsigned int egt = 7800;
unsigned int transtemp = 1150;
char gear[] = "1";

int brightness = 255;


void setup()
{
  Serial.begin(115200);
  pinMode(8, OUTPUT);
  pinMode(A0, INPUT);
  delay(50);
}


void loop()
{
  ReadDigitalStatuses();
  SendCANFramesToSerial();

  analogWrite(8, brightness);

delay(100);
int dummy = analogRead(A0);
int sensorValue = analogRead(A0);
 voltage = sensorValue * .207;


 rpm = rpm + 1;

 if (rpm > 780){
  rpm = 750;
 }

 SendTextExtensionFrameToSerial(3204, gear);

}

void ReadDigitalStatuses()
{
  digitalPins = 0;

  int bitposition = 0;
  for (int i=13; i<28; i++)
  {
    if (digitalRead(i) == HIGH) digitalPins |= (1 << bitposition);
    bitposition++;
  }
}


void SendCANFramesToSerial()
{
  byte buf[8];

  memcpy(buf, &rpm, 2);
  memcpy(buf + 2, &voltage, 2);
  memcpy(buf + 4, &ct, 2);
  memcpy(buf + 6, &oil, 2);

  // write first CAN frame to serial
  SendCANFrameToSerial(3200, buf);

  // build 2nd CAN frame, Arduino digital pins and 2 analog values
  memcpy(buf, &digitalPins, 2);
  memcpy(buf + 2, &speed, 2);
  memcpy(buf + 4, &cruisespeed, 2);
  memcpy(buf + 6, &fuel, 2);

  // write 2nd CAN frame to serial
  SendCANFrameToSerial(3201, buf);

  // build 3rd CAN frame, rest of Arduino analog values
  memcpy(buf, &fuelpress, 2);
  memcpy(buf + 2, &boost, 2);
  memcpy(buf + 4, &exhpress, 2);
  memcpy(buf + 6, &egt, 2);

  // write 4th CAN frame to serial
  SendCANFrameToSerial(3202, buf);

  memcpy(buf, &transtemp, 2);

  // write 4th CAN frame to serial
  SendCANFrameToSerial(3203, buf);

}


void SendCANFrameToSerial(unsigned long canFrameId, const byte* frameData)
{
  const byte serialBlockTag[4] = { 0x44, 0x33, 0x22, 0x11 };
  Serial.write(serialBlockTag, 4);
  Serial.write((const byte*)&canFrameId, 4);
  Serial.write(frameData, 8);
}

  void SendTextExtensionFrameToSerial(unsigned long canFrameId, const char* text)
{
  if (text)
  {
    const byte textExtensionBlockTag[4] = { 0x55, 0x33, 0x22, 0x11 };
    Serial.write(textExtensionBlockTag, 4);
    Serial.write((const byte*)&canFrameId, 4);
    Serial.write(text, strlen(text) + 1);
  }
}

its messy I was just testing everything and wanted to get one sensor reading the battery voltage. everything works when its just the Arduino and pin 8 to the screen so i can see it. the Arduino is controlling the screen brightness. pulling A0 to 0 3.3 and 5 works perfectly on the dash screen. only is pulled high when plugged into a bunch of voltage dividers.

If I need to ill probably use a timer later I'm just trying to figure this issue out. Very true though.

SINCE you posted your code, you changed it?
gee thanks, I won't bother reading it again

No no this is the code that I've been testing with. I didn't change it. here is the part i was talking about

delay(100);
int dummy = analogRead(A0);
int sensorValue = analogRead(A0);
 voltage = sensorValue * .207;`

I will wait for the schematic, the nice pictures did show the OP will probably have a lead dress problem.

How do I make a schematic?

1 Like

unsigned long millivolts = (unsigned long) read * 5000UL /1023UL;

when you print for the needs-volts human,
Serial.print( ((float) millivolts / 1000.0 ), 3 ); // show 3 places

Otherwise, floats are slow and get dirty in the low-order digits when operated on. Long ago (80's), x = 2.0 and print( x ) got 1.999999999...

I not quite following, is it be cause I multiply by .207, I don't want to convert to millivolts its just the voltage divider simplified and the output value is /10 later in software. so I need an int for the decimal. so its A0 * 10 *5 /1023 *((22000 + 6800)/6800) for a 22k 6.8k voltage divider to read car voltage.

That is a good question. These links should answer your question, if not ask more.
What is a schematic What Is a Schematic Diagram?
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic/all
HOW to READ a Schematic: https://www.youtube.com/watch?v=9cps7Q_IrX0&t=15s
There is a great program called KiCad that will take you from idea through a complete printed circuit board creating all of the needed files.

alright thanks

I just also tested it changing nothing except pulling the board off the Arduino and juppering pin 8 and A0 from the mega to my board. worked perfectly. gave the battery pin 5v and the dash read 5v. its got to be an issue when there all pugged in. like goforsmoke says with the code reading them all slow enough or gilshultz saying noise from running next to each other.

Found it. Accidentally grounded the aref pin.

I always start out printing out the raw counts from the A/D.

Your dividers, where are the sourced, the Mega 5V, another 5V or the vehicle battery? I don't think the mega can supply enough power for a sensor pulled to its 5v pin and a 150 pullup on a sensor that can go to below 100 ohms.

A 12 volt battery (ignoring the voltage when charging):
12V *6.8k/(6.8k+3.3k) = 8 volts this will drive the analog input well beyond its limits.