Temp Sensor does not get reasonable result

Hi All,

I am totally new to arduino, even hardware, could someone help me out with below problem:

I have an arduino UNO and DFROBOT ethernet shield which connected with each other. Then, I connect a temperature sensor (lm 35 dz) to the ethernet shield, 5 v power supply. Additionally, I use a 9 volts AC power supply for the entire unit. I use following formula to calculate centigrade from pin output: signalvoltage100/1023, voltage here is 4.98 volts measured manually.

However, result is near 40 degree while real temperature is only 24 degree. Also I measure the voltage between power supply and GND of this temp sensor, it is about 0.25 volts which multiple by 100 is close to real temperature.

I do not know what's wrong? Could anyone give me some idea or clues about it?

Regards,
Jie

Can you post your sketch (use the #button for appropiate tagging) and a drawing how you connected the LM35?

my code as following:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,10,102,160 };
unsigned int localPort = 8012;

int sensorPin_Light = A0;
int sensorValue_Light = 0;

int sensorPin_Temp = A1;
int sensorValue_Temp = 0;
char sensorValue_Temp_Char[2] ; 

byte remoteIP[] = { 10,10,102,133 };
int remotePort = 58121;	// the destination port

void setup() {
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  
  Serial.begin(9600);
 }

void loop() {
  
  sensorValue_Light = analogRead(sensorPin_Light);
  sensorValue_Temp = analogRead(sensorPin_Temp);
  
  //udp packet can have max 160 bits for data.
  char udp_content[20] ; 
  //four char is used for every integer's digital whose max value is 1024, and end with ','
  char sensorValue_Light_Char[5] ; 
  itoa (sensorValue_Light, sensorValue_Light_Char, 10);
  
  char sensorValue_Temp_Char[5] ; 
  itoa (sensorValue_Temp, sensorValue_Temp_Char, 10);


  strcpy (udp_content,sensorValue_Light_Char);
  strcat (udp_content,",");
  strcat (udp_content,sensorValue_Temp_Char);
  strcat (udp_content,",");

  Serial.println(udp_content);
  
  Udp.sendPacket(udp_content, remoteIP, remotePort );
  delay(2000);
}

how i connect the lm35:
5v for power, GND line connect with GND pin and analog in 1 for temperature.
PS. meanwhile, I also use a light intensity sensor with A0.

This:

sensorValue_Light = analogRead(sensorPin_Light);
sensorValue_Temp = analogRead(sensorPin_Temp);

may be problematic. The analog pins share a single A2D converter. If you try to read from multiple pins in rapid succession, the later results tend to be inaccurate. Recent posts have suggested a couple of remedies: use a small delay between readings or take two readings for each input and ignore the first. In your case, just commenting out the analogRead for the light should show you if that is the issue.

Please modify your post, select the code and use the # button , thank you

Q:

Udp.sendPacket(udp_content, remoteIP, remotePort ); ?? How does sendPacket know the length of the packet to be send?

robtillaart:
Q:

Udp.sendPacket(udp_content, remoteIP, remotePort ); ?? How does sendPacket know the length of the packet to be send?

According to my understanding, for every UDP packet in IP v4, the size of data in one packet is always 160 bits. In other words, no matter how real size of data is, the method always send 160 bits data every time.

wildbill:
This:

sensorValue_Light = analogRead(sensorPin_Light);

sensorValue_Temp = analogRead(sensorPin_Temp);




may be problematic. The analog pins share a single A2D converter. If you try to read from multiple pins in rapid succession, the later results tend to be inaccurate. Recent posts have suggested a couple of remedies: use a small delay between readings or take two readings for each input and ignore the first. In your case, just commenting out the analogRead for the light should show you if that is the issue.

I have tried both ways: only read temperature and read twice when there is light sensor. fortunately, both of them works well for me. Thank you very much. :slight_smile:

According to my understanding, for every UDP packet in IP v4, the size of data in one packet is always 160 bits. In other words, no matter how real size of data is, the method always send 160 bits data every time.

Dived into the library myself, and the function is overloaded. for a byte string you need to supply the length and for a char array it sends until it meets a '\0' char.

/* Send packet contained in buf of length len to peer at specified ip, and port */
/* Use this function to transmit binary data that might contain 0x00 bytes*/
/* This function returns sent data size for success else -1. */
uint16_t UdpClass::sendPacket(uint8_t * buf, uint16_t len,  uint8_t * ip, uint16_t port){
  return sendto(_sock,(const uint8_t *)buf,len,ip,port);
}

/* Send  zero-terminated string str as packet to peer at specified ip, and port */
/* This function returns sent data size for success else -1. */
uint16_t UdpClass::sendPacket(const char str[], uint8_t * ip, uint16_t port){	
  // compute strlen
  const char *s;
  for(s = str; *s; ++s);
  uint16_t len = (s-str);
  // send packet
  return sendto(_sock,(const uint8_t *)str,len,ip,port);
}

You might also consider using a resistor