Arduino Interface LM35.JPG

Code

  1. /*
  2. What is temperature there with LM35
  3.  
  4. Reads an Temperature with LM35 Sensor .
  5. Also prints the results to the serial monitor.
  6.  
  7. The circuit:
  8. * LM35 is connected to analog pin 0.
  9. Center pin of the LM35 goes to the analog pin.
  10. side pins of the LM35 go to +5V and ground
  11. * LED connected from digital pin 9 to ground(If required)
  12.  
  13. */
  14. // These constants won't change. They're used to give names
  15. // to the pins used:
  16. const int analogInPin = A0; // Analog input pin that the LM35 is attached to
  17. const int analogOutPin = 9; // Analog output pin that the LED is attached to
  18. int sensorValue = 0; // value read from the LM35
  19. int outputValue = 0; // value output to the PWM (analog out)
  20. void setup() {
  21. // initialize serial communications at 9600 bps:
  22. Serial.begin(9600);
  23. }
  24. void loop() {
  25. // read the analog in value:
  26. sensorValue = analogRead(analogInPin);
  27. // map it to the range of the analog out:
  28. outputValue = map(sensorValue, 0, 1023, 0, 255);
  29. // change the analog out value:
  30. analogWrite(analogOutPin, outputValue);
  31. // print the results to the serial monitor:
  32. Serial.print("Voltage in mV = " );
  33. Serial.print(sensorValue*5);
  34. Serial.print("\t Temperature in Degree Celsius = ");
  35. Serial.println(outputValue*2);
  36. // wait 2 milliseconds before the next loop
  37. // for the analog-to-digital converter to settle
  38. // after the last reading:
  39. delay(2);
  40. }