Power supply and corrupt data


I have a problem with power supply.

When I connect my arduino and ethernet shield to my PC and router I can access and get data to my Mysql server, but when I disconnect the computer and connects external power supply the values send to the database is completely wrong.

Has anyone got a clue why?

Has anyone got a clue why?

What voltage power supply? What amperage? AC or DC?

How is it connected?

What data are you sending to the database? How is the data wrong when connected to an external power supply?

How about you supply some more clues?

Hi PaulS

DC adapter marked with 5[V], 2[A] connected to the external power supply on arduino

I´m sending values between 1000 to 4000 and it works fine when the PC is connected to ardiuno without external supply.

But when i disconnect the PC and external supply is being connected the data locks like this

2010-02-17 13:23:02 -14493
2010-02-17 13:21:40 -14628
2010-02-17 13:20:18 -14718

Shouldn't the DC power input be 7..12V dc? (on board regulator needs headroom to work)

The question was where does the data come from? The suggestion that the voltage may be too low is possibly relevant, if the data comes from analog sensors that are in any way voltage dependent.

However the negative numbers in place of positive numbers seems really strange. Can you share your code?

It sounds like you haven't connected the ground between your arduino and what ever you are sending the data to.

Blarp is right, if the PSU is 5V then it should be connected to the 5V line and not Vin on the arduino.

Here is my code:

//Modification Espens wattwatcher www.GMX.no
#include <Ethernet.h>

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168, 1, 2 };
byte gw = {192,168,1,1};
byte server = { 195, 47, 247, 206 }; // Hjeng.se
byte subnet = { 255, 255, 255, 0 };

int avgwatt = 0; //gjennomsnitt totalwatt på hovedteller pr cycle
int teller_tot = 7; //pinne for lyssensor
unsigned long period_start;
unsigned long period_length = 57000;
unsigned long period_stop;
unsigned long run = 0;
unsigned long period_total = 0;
int periodaverage = 0; //snitt pr min
int period_counter = 0;

void setup()

pinMode(teller_tot, INPUT);

void loop()
period_total = 0;
period_counter = 0;
period_start = millis();
period_stop = period_start + period_length;

while( millis() <= period_stop)

avgwatt = getAvgWatt(teller_tot, 90); // hvilken pinne, pulsfaktor (pulser pr kWh)
period_total += avgwatt;
// Serial.print("Watt hittil: ");
// Serial.println(period_total);
period_counter += 1;

} // Etter at period- er utløpt. Dvs 1min. Vi regner ut avg og skriver til Linje2 på lcd.

periodaverage = period_total / period_counter;


run +=1;

void sendWatt()
Ethernet.begin(mac, ip, gw, subnet);
Client client(server, 80);
//Serial.println(“Initiates connection?”);

if (client.connect())
// Serial.println(“Connected!”);
client.print(“GET http://hjeng.se/MYPHPFILE.php?periodaverage=”);
client.println(" HTTP/1.0");
client.println(“Host: www.hjeng.se”);

// Serial.println(“Connection failed”);

//stopper client

while(client.status() != 0)


int getAvgWatt(int pinne,float pulsfaktor)
int watt = 0;
int teller = 0;
int old_val = 0;
unsigned long endtime = millis() + 40000;
int val = LOW;
while(millis() != endtime)
val = (digitalRead(pinne)); // Reads value high/low and saves it
if ((val == HIGH) & (old_val == LOW))
{ // We have light, lets read the time, and value.

// Serial.println(teller);
teller +=1;
old_val = val;
else {
old_val = val;
} // 10 sek er gått, summer og regn ut watt

//Serial.print("Antall pulser pr 10sek : ");
//Serial.print("Tilsvarer i watt: ");

watt = teller * pulsfaktor;
return watt;

Negnelct the line: client.print("GET http://hjeng.se/MYPHPFILE.php?periodaverage=")

It should be: client.print("GET http://hjeng.se/myphpfile.php?=");

Next time post your code using the # button in the message editor.

unsigned long endtime = millis() + 40000;
int val = LOW;
     while(millis() != endtime)

I don't think is a good idea. If millis() is near where it will overflow, adding 40000 will overflow, and result in a small value in endtime.

Using subtraction with millis() always works:

unsigned long startTime = millis();
while(millis() - startTime > 40000)
   // Do something, quick.

Additionally, suppose your code is called right after start up. The call to millis will get a small number, say 250. Then, endtime will be 40250. Suppose that, after 39+ seconds, millis() returns 40238, 40245, 40249, 40253, etc. It will take a long time for this function to end.

Hi, I did as you suggested PaulS.

That did not help in this case but I think thats a better way to write the code.

I have made some findings:

  1. The negative values I found by coincident, I got the arduino running and connected power supply to my laptop and the vales get negative instantly.

  2. I need to have the line Serial.begin(9600) in the setup area. But I don´t need to send something to the port. But I have to open the serial monitor otherwise it will not send anything to the server.

I don´t know what to do know next....