We read the value from an analog pin using the analogRead() function. This function will return a value between 0 and 1023. This means that if the sensor is returning the full voltage of 5V, then the analogRead() function will return a value 1023, which results in a value of 0.0049V per unit (we will use this number in the sample code). The following code shows the syntax for the analogRead() function:
analogRead(pin);
The analogRead() function takes one parameter which is the pin to read from. The following code uses the analogRead() function with a tmp36 temperature sensor to determine the current temperature:
#define TEMP_PIN 5 void setup() { Serial.begin(9600); } void loop() { int pinValue = analogRead(TEMP_PIN); double voltage = pinValue * 0.0049; double tempC = (voltage - .5) * 100.0; double tempF = (tempC * 1.8) + 32; Serial.print(tempC); Serial...