Data Sheet
80 : circuit 4b
CODE TO NOTE
VOLTAGE
CONVERSION
ALGORITHMS
Many of the sensors that you will use with your microcontroller
work by changing a voltage in some predictable way in response to
a property of the world (like temperature, light or magnetic fields).
Often, you will need to build an algorithm that converts these
voltages to the desired value and units. The temperature sensor is
a great example of this code. We use three equations to convert a
voltage value into degrees in C and F.
voltage = analogRead(A0) * 0.004882814;
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32.0;
CODING CHALLENGES
DISPLAY THE TEMPERATURE IN DEGREES KELVIN: Try adding an equation so
that the temperature is displayed in degrees Kelvin. (You will have to look up the formula
for converting from degrees Celsius or Fahrenheit to Kelvin.)
DISPLAY A BAR GRAPH: By changing the code you can display the temperature as a
bar graph instead of a number.
DISPLAY VALUES FROM ANOTHER SENSOR: You can swap out the TMP36 for a
potentiometer, photoresistor or other sensor and display the new set of values.
ADD AN RGB LED: Add an RGB LED that changes color based on the temperature.
SIK v4 Book Oct 13.indb 80 10/18/17 10:01 AM