Analog read

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 ...

Get Mastering Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.