Code not working when own power supply is used

Hi all,

i am reading an analog signal (fuel sensor) on my mega2560 mini. The signal is jumpy so I take the average, serial print it and use that value to write the code. All is working fine if the USB is connected. When I disconnect the laptop and let the Mega run on its own power supply, the code isn't working.
Is this because the laptop isn't connected and therefor the "average" in the serial print can't be read? could not find any thing on google that could help.

any help/ explanation regarding this is welcome

Niels`




int fuel = 4; //pwm pin
const int numReadings = 1000;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int inputPin = A12;

void setup() {
    Serial.begin(9600);
  
  Serial.println("Serial port");    
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  total = total - readings[readIndex];
  readings[readIndex] = analogRead(inputPin);
  total = total + readings[readIndex];
  readIndex = readIndex + 1;
  
  if (readIndex >= numReadings) {
    readIndex = 0;
  }
  average = total / numReadings;
  Serial.print("average: ");
  Serial.println(average);
  
    
  if (average <3) {
    analogWrite(fuel, 200);
      }else if (average ==3) { 
    analogWrite(fuel, 160);
      }else if (average ==4) { 
    analogWrite(fuel, 125);
      }else if (average ==5) { 
    analogWrite(fuel, 100);
      }else if (average ==6) { 
    analogWrite(fuel, 80);
      }else if (average ==7) { 
    analogWrite(fuel, 70);
      }else if (average ==8) { 
    analogWrite(fuel, 60);
      }else if (average ==9) { 
    analogWrite(fuel, 55);
      }else if (average ==10) { 
    analogWrite(fuel, 52);
      }else if (average ==11) { 
    analogWrite(fuel, 35);
      }else if (average ==12) { 
    analogWrite(fuel, 20);
      }else if (average >=13) { 
    analogWrite(fuel, 10); 
  }
}

What type of power supply? How do you have it connected?

Lab power supply set to 9v.
the posted code is a part of more code, which is working as it should.
What I am questioning is: can the Arduino work with the serial print data when its not connected with USB to IDE? or do I need to return that value an other way so it can be used when the Arduino is "standalone" ?

The Arduino will happily print to a disconnected Serial port. The mistake that I have seen several times on the forum is for a sketch to contain a line like

while (!Serial);

This will wait forever for Serial to connect which, if the project has no USB connection, then it never will.

1 Like

Arduino will not work with the serial print data when its not connected with USB to IDE. it need to connect to PC or laptop to use its USB to serial converter to communicate with the board to PC. if u want to use serial print data or any serial comunication u need to connect it to PC via USB. external power supply will run ur programme but fails to work with serial communication...

Thats only an issue for processors with Native USB ports, not those that use external UART to USB conversion.

2 Likes

See Post #6.

1 Like

+1 more for post #6. The Mega will happily send serial data out to a disconnected pin and never know the difference. It has no way of knowing if there is any receiver out there. The serial writes are probably not the issue.

I asked two questions.

You only answered one.

Why?

The mega runs on a LM 9v which is powered by the car battery. Battery is okay and I did check all grounds.

And to be more specific. Lm for power, ground and car ground to Arduino. 5v to the fuel sender and the return of the 2 wire sender to the analog pin. Pwm pin to the fuel gauge.

As I said all works as long the laptop is connected to the Arduino by usb.

So to me, with my limited understanding of Arduino, it’s either power or ground or the serial communication can’t be used if there is no pc connected

No, you OK.

Read

and make sure you aren't

 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }

waiting for this that will never happen.

Edit: which I see now has been 'splained already.

a7

It's not that. I've got tons of projects that run just fine while sending serial to nowhere.

How do you have the 9V connected to the Arduino. Are you using the barrel jack? The VIN pin? How do you have it connected? Can you show a schematic?

Yes. And if you'll show a schematic someone might be able to spot the problem.

While the car is running? That might be your problem. Using power from a car's 12V system usually requires a little extra care. That is some pretty dirty power and you'll need to smooth it out a little for the Arduino.

Again, a schematic would help.

Exactly what type of "sender" are you using?

thanks again for the quick reply.

Don't know how I can make a Schematic of all and post it.
But the car is not running and the 9v is on the VIN pin.
If serial com is possible without a pc connected, the problem needs to lay in the wiring/ connection. I did notice a couple of things: usb was still in the mega mini but not in pc, removed it and it seems it made a small improvement, gauge needle was doing something and then stopped. Other thing was, the usb connector on the mega mini is getting hot and so is the LM9V, which is getting even hotter. I know they can become, since the need to dissipate 4V.

i will put on a new mega mini and power it with a buck converter and check if this makes a difference.

Take a piece of paper and a pencil and draw a diagram that shows all the things and label the pins where everything connects. It isn't hard.

You can easily convince yourself that this is not the problem. There are only 4 lines in your code relating to serial communication. Remove those and try again as a test. I predict that you'll see the same behavior as before.

Sounds like the sender's resistance is so low that it's overloading the power supply, that's why I asked for a description of it and how its wired.

Interesting theory.

The resistance is from 10 to 60 ohm.

Would an additional resistor in the analog read line help? And by any change a value of that resistor? Maybe you have experience with it?

Btw the wiring is 5v from the Arduino to the sender and an analog read on the returning wire. Will make a sketch of it

No pulldown resistor? You will read 5V constantly. Post a wiring diagram.